Enterprise JavaBeans Distilled

zhaozj2021-02-16  60

Enterprise

JavaBeans Distilled

Author: Tnk Luo

Seventh time:

Message driver bean (continued)

JMS application client

To get a better idea of ​​how JMS is used, we can create a Java application whose sole purpose is receiving and processing reservation messages. We will develop a very simple JMS client that simply prints a description of each ticket as it receives the messages. We 'Ll Assume That The TraveAgent EJB IS Using The TextMessage To Send A Description of The Ticket To The JMS Clients. The Following Code Shows How The JMS Application Client Might Look:

To better understand how to use JMS, you can develop a Java application that is the only use of receiving and processing a predetermined message. Here, a very simple JMS customer will develop. When the customer receives the message, the description information of each ticket is printed. Assume that TraveLagent EJB uses TextMessage to send a ticket to the JMS customer. The following code shows the possible look of the JMS application customer:

Import javax.jms.message;

Import javax.jms.TextMessage;

Import javax.jms.topicConnectionFactory;

Import javax.jms.topicConnection;

Import javax.jms.topics;

Import javax.jms.topic;

Import javax.jms.session;

Import javax.jms.topicsubscriber;

Import javax.jms.jmsexception;

Import javax.naming.initialcontext;

Public class jmsclient_1 imports javax.jms.MESSAGELISTENER {

Public static void main (string [] args) throws exception {

IF (args.length! = 2)

Throw New Exception ("WRONG NUMBER OF ARGUMENTS");

NEW JMSCLIENT_1 (Args [0], Args [1]);

While (True) {thread.sleep (10000);

}

PUBLIC JMSCLIENT_1 (String TopicName) throws exception {

InitialContext JNDICONTEXT = GetInitialContext ();

TopicConnectionFactory Factory = (TopicConnectionFactory)

JNDICONTEXT.LOOKUP ("TopicFactoryNameGoes);

Topic Topic = (TOPIC) JNDICONTEXT.LOOKUP ("TopicNameGoeshere");

TopicConnection connect = factory.createtopicConnection ();

TopicSession session = connect.createtopicSession (false, session.auto_acknowledge);

TopicsUbscriber subscriber = session.createSubscriber (Topic);

Subscriber.setMessageListener (this);

Connect.Start ();

}

Public void onMessage (Message message) {

Try {

TextMessage Textmsg = (TextMessage) Message;

String text = textmsg.gettext ();

System.out.println ("/ n reservation recieve: / n" text);

} catch (jmsexception jmse) {

JMse.PrintStackTrace ();

}

}

Public static initialcontext getinitialcontext () {

// Create a concrete product manufacturer's JNDI context

}

}

The builder of JMSClient_1 contains TopicConnectionFactory and Topic from JNDI INITIALCONTEXT. These objects are created using Properties using specific vendor products, allowing customers to connect to TRAVELAGENT EJB suppliers used by the TRAVELAGENT EJB. For example, the code for the GetInitialContext () method in the WebLogic application server is as follows: (JNDI also allows Properties to be placed in the jndi.properties file, which contains the Property value for the initialContext, and can be dynamically found during run. This book , Explicitly give the Properties value.)

Public static initialcontext getinitialcontext () {

Properties env = new property ();

Env.put (Context.Security_Principal, "Guest");

Env.put (Context.security_credentials, "guest");

env.put (Context.Initial_Context_Factory,

"WebLogic.jndi.wlinitialContextFactory");

Env.put (Context.Provider_URL, "T3: // localhost: 7001");

Return New InitialContext (ENV);

}

Once the customer gets TopicConnectionFactory and Topic, you can create TopicConnection and TopicSession as you can take the same manner as TroELAgent EJB. Their main difference is that the TopicSession object here has created a TopicsUbscriber object instead of topicpublisher. Where Topicsubscriber is explicitly designed to handle messages from a specific Topic:

TopicSession session =

Connect.createtopicSession (false, session.auto_acknowledge);

TopicsUbscriber subscriber = session.createSubscriber (Topic);

Subscriber.setMessageListener (this);

Connect.Start ();

Topicsubscriber can receive messages directly, or can delegate the message to the interface javax.jms.MessageLister. In the example, JMSClient_1 implements the MessageListener interface so that it can process messages. The MESSAGELISTENER object implements a single method, onMessage (), which is called whenever a new message is sent to the subscriber's Topic. Here, each TRAVELAGENT EJB sends a predetermined message to Topic, the OnMessage () method in the JMS client is called so that it can receive a copy of the message and handle it: public void onmessage (Message Message) {

Try {

TextMessage Textmsg = (TextMessage) Message;

String text = textmsg.gettext ();

System.out.println ("/ n reservation recieve: / n" text);

} catch (jmsexception jmse) {

JMse.PrintStackTrace ();

}

}

to be continued. . . . . . . .

(Author Other article: http://www.9cbs.net/develop/author/netauthor/WorldHeart/)

转载请注明原文地址:https://www.9cbs.com/read-17608.html

New Post(0)