JMS sending and receiving instance-publishing/subscription Mode

Source: Internet
Author: User

Send message

No matter whether a message is sent to a queue or published to a topic, the programming steps are the same. The difference is that different JMS objects are used. The specific definitions are shown in the following table:

The process of sending messages is roughly divided into the following steps;

1. Get a reference to the Weblogic server context;

2. Create a connection factory;

3. Use the connection factory to create a connection;

4. Use a connection to create a session;

5. Obtain a purpose;

6. Create a message producer using sessions and purposes;

7. Create a message object;

8. Create an instance of the message type to be sent using the connection;

9. Use a connected queue sender or topic publisher to send messages using the sender or publisher.

Before coding, import the required jar package and configure the JMS server.

 

Note:

Wlfullclient. jar is generated by entering the Weblogic installation directory, for example, C: \ oracle \ middleware \ wlserver_10.3 \ Server \ Lib. run Java-jar wljarbuilder. jar to generate the wlfullclient. jar file.

For details about how to configure a Message Server in WebLogic Server, refer to "Learn how to configure a Message Server in WebLogic Server ".
"

 

Message sending code:

Package COM. xu. pub2sub; import Java. util. properties; import javax. JMS. jmsexception; import javax. JMS. session; import javax. JMS. textmessage; import javax. JMS. topic; import javax. JMS. topicconnection; import javax. JMS. topicconnectionfactory; import javax. JMS. topicpublisher; import javax. JMS. topicsession; import javax. naming. context; import javax. naming. initialcontext; import javax. naming. namingexception; Public Cl Ass msgtopicproducer {/*** @ function: topic sender * @ Author: * @ Date: 2012-10-18 */private topicpublisher publisher; private textmessage MSG; Public msgtopicproducer (string [] ARGs) throws namingexception, jmsexception {/* initialize the context object */string url = "T3: // localhost: 7001"; properties P = new properties (); p. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); p. put (context. provider_url, URL); Context CTX = New initialcontext (p);/* create a connection factory */topicconnectionfactory tconfactory = (topicconnectionfactory) CTX. lookup ("weblogic. JMS. connectionfactory ");/* Create A Topic */topic messagetopic = (topic) CTX. lookup ("JMS/mytopic");/* create a connection */topicconnection tcon = tconfactory. createtopicconnection ();/* Create a session */topicsession session = tcon. createtopicsession (false, session. auto_acknowledge);/* Create a topic publisher and send messages */publisher = Se Ssion. createpublisher (messagetopic); MSG = session. createtextmessage ();} public void runclient () throws jmsexception {MSG. settext ("hello"); Publisher. publish (MSG); MSG. settext ("Welcome to JMS! "); Publisher. Publish (MSG); system. Out. println (" successful! ");} Public static void main (string [] ARGs) throws namingexception, jmsexception {msgtopicproducer MP = new msgtopicproducer (ARGs); MP. runclient ();}}

Synchronous receiver code:

Package COM. xu. pub2sub; import Java. util. properties; import javax. JMS. jmsexception; import javax. JMS. session; import javax. JMS. textmessage; import javax. JMS. topic; import javax. JMS. topicconnection; import javax. JMS. topicconnectionfactory; import javax. JMS. topicsession; import javax. JMS. topicsubscriber; import javax. naming. context; import javax. naming. initialcontext; import javax. naming. namingexception; public class syncmessagetopicreceiver {/*** @ function: publish-subscribe Message Service synchronization receiver * @ Author: * @ Date: 2012-10-18 */private topicsubscriber subscriber; private textmessage MSG; public syncmessagetopicreceiver () throws namingexception, jmsexception {/* initialize the context object */string url = "T3: // localhost: 7001"; properties P = new properties (); p. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); p. put (context. provider_url, URL); Context CTX = new initialcontext (p);/* Create a topic connection factory */topicconnectionfactory tconfactory = (topicconnectionfactory) CTX. lookup ("weblogic. JMS. connectionfactory ");/* Create A Topic */topic messagetopic = (topic) CTX. lookup ("JMS/mytopic");/* create a connection */topicconnection tcon = tconfactory. createtopicconnection ();/* Create session */topicsession session = tcon. createtopicsession (false, session. auto_acknowledge);/* Create receiver */subscriber = session. createsubscriber (messagetopic); tcon. start ();} public void runclient () throws jmsexception {MSG = (textmessage) subscriber. receive (); system. out. println ("er Er:" + MSG. gettext (); MSG = (textmessage) subscriber. receive (); system. out. println ("er Er:" + MSG. gettext ();} public static void main (string [] ARGs) throws namingexception, jmsexception {syncmessagetopicreceiver extends ER = new syncmessagetopicreceiver (); extends er. runclient ();}}

Asynchronous Receiver code:

Package COM. xu. pub2sub; import Java. util. properties; import javax. JMS. jmsexception; import javax. JMS. message; import javax. JMS. messagelistener; import javax. JMS. session; import javax. JMS. textmessage; import javax. JMS. topic; import javax. JMS. topicconnection; import javax. JMS. topicconnectionfactory; import javax. JMS. topicsession; import javax. JMS. topicsubscriber; import javax. naming. context; import javax. naming. initialcontext; import javax. naming. namingexception; public class asyncmessagetopicreceiver implements messagelistener {/*** @ function: publish-subscribe to message service to receive messages asynchronously * @ Author: * @ Date: 2012-10-18 */private int expected_message_count = 10; private int messagecount = 0; private topicsubscriber subscriber; Public asyncmessagetopicreceiver () throws namingexception, jmsexception {/* initialize the context object */string url = "T3: // localhost: 7001 "; properties P = new properties (); p. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); p. put (context. provider_url, URL); Context CTX = new initialcontext (p);/* Create a topic connection factory */topicconnectionfactory tconfactory = (topicconnectionfactory) CTX. lookup ("weblogic. JMS. connectionfactory ");/* Create A Topic */topic messagetopic = (topic) CTX. lookup ("JMS/mytopic");/* create a connection */topicconnection tcon = tconfactory. createtopicconnection ();/* Create session */topicsession session = tcon. createtopicsession (false, session. auto_acknowledge);/* Create receiver */subscriber = session. createsubscriber (messagetopic);/* Set listener */subscriber. setmessagelistener (this); tcon. start ();} public Boolean expectmoremessage () {return messagecount <expected_message_count;} @ overridepublic void onmessage (message) {system. out. println ("onmessage"); try {textmessage MSG = (textmessage) message; system. out. println ("er Er:" + MSG. gettext ();} catch (jmsexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} messagecount ++;} public static void main (string [] ARGs) throws namingexception, jmsexception {int max_tries = 30; int trycount = 0; asyncmessagetopicreceiver extends ER = new asyncmessagetopicreceiver (); While (extends er. expectmoremessage () & (trycount <max_tries) {try {system. out. println (trycount); thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}trycount ++ ;}}}

 

 

 

 

 

 

 

 

 

 

 

 

 

Wlfullclient. jar is generated by entering the Weblogic installation directory, for example, C: \ oracle \ middleware \ wlserver_10.3 \ Server \ Lib. run Java-jar wljarbuilder. jar to generate the wlfullclient. jar file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.