In the previous article, I have already written about the PTP (point-to-point) mode of the JMS message mechanism. Today I will write about my personal publishing-subscription mode of the JMS message mechanism.
Before reading the text, I would like to explain the features and differences between PTP and publishing-subscription.
Features:
Point-to-Point message model: this model is implemented through a Server Message Queue. The message sender writes messages to the queue, and the message receiver extracts messages from the queue.
Publishing-subscription mode: Send a message to a topic. The Message Server publishes the message to each subscriber of the topic. For example, a magazine (equivalent to a message sender) sends a pile of magazines (equivalent to messages) to a post (equivalent to a topic ), then the post will send the magazine to every reader who has subscribed to the magazine (equivalent to the message recipient ).
Differences:
Point-to-Point model each message has only one receiver.
In the publish-subscribe message mode, each message can have multiple recipients.
Before reading the code, let me first talk about it. Because the previous JMS example is in PTP mode, it uses the server's Queue (Queue) for storage, the publishing-subscription mode applies to topics for storage. Therefore, you must create a topic under the WebLogic Server. Otherwise, an error is reported .....
Okay, let's talk a little bit about it ~~, Next, let's look at the specific code in the publishing-subscription mode.
Let's take a look at my entire project structure.
There are Java. JMS. jar and wlfullclient. Jar packages. If you need them, you can download them from my resources .....
Let's take a look at the staticfunction class under the staticfunction package.
Public class staticfunction {public Static Context getcontext () {context CTX = NULL; string url = "T3: // localhost: 7001"; properties P = new properties (); p. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); p. put (context. provider_url, URL); try {CTX = new initialcontext (p);} catch (namingexception e) {// todo auto-generated catch blocke. printstacktrace ();} return CTX;} public static topicsession getsession (context CTX) throws namingexception, jmsexception {// create a connection factory topicconnectionfactory tconfactory = (topicconnectionfactory) CTX. lookup ("weblogic. JMS. connectionfactory "); // create a connection topicconnection tcon = tconfactory. createtopicconnection (); // create a session topicsession session = tcon. createtopicsession (false, session. auto_acknowledge); Return session ;}}
Two static methods are encapsulated. One is to get the Weblogic context and the other is to get the session. These two methods will be used in the sender class and receiver class.
Next, let's take a look at the code in the sender class.
Public class msgtopicproducer {// publish-subscription mode private topicpublisher publisher = NULL; private textmessage MSG = NULL; Public msgtopicproducer () throws namingexception, jmsexception {// constructor context CTX = staticfunction. getcontext (); // obtain the context // create a topic messagetopic = (topic) CTX. lookup ("JMS/mytopic"); // creates a session topicsession session = staticfunction. getsession (CTX); // create a topic publisher and send the message publisher = session. createpublisher (messaget OPIC); // create the message body MSG = session. createtextmessage ();} public void runclient () throws jmsexception {If (MSG! = NULL) {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 (); MP. runclient ();}}
I believe that all the people who have read my previous article probably know the meaning of the code, and it is only a small part, because the previous JMS mechanism server uses queue to store messages, therefore, the sender uses queuesender. Because the server uses topic storage, the sender uses topicpublisher. The other is to create a queue, and the other is to create a topic... Note the differences between the two modes.
Nothing else has changed.
Next, let's take a look at the code of the receiver class, and there are two types of recipients: Synchronous receiver and Asynchronous Receiver.
Let's take a look at the synchronous receiver: syncmessagetopicreceiver
Public class failed {/*** @ Param ARGs */private topicsubscriber subscriber = NULL; private textmessage MSG = NULL; Public syncmessagetopicreceiver () throws namingexception, jmsexception {context CTX = staticfunction. getcontext (); // obtain the Context Topic messagetopic = (topic) CTX. lookup ("JMS/mytopic"); // create a connection factory topicconnectionfactory tconfactory = (topicconnectionfactory) CTX. lookup ("weblogic. JMS. connectionfactory "); // create a connection topicconnection tcon = tconfactory. createtopicconnection (); // create a session topicsession session = tcon. createtopicsession (false, session. auto_acknowledge); subscriber = session. createsubscriber (messagetopic); tcon. start (); // The Code serves exactly the same purpose as in PTP mode} public void runclient () throws jmsexception {system. out. println ("OK"); MSG = (textmessage) subscriber. receive (); system. out. println ("syncreceiver:" + MSG. gettext (); MSG = (textmessage) subscriber. receive (); system. out. println ("syncreceiver:" + MSG. gettext ();} public static void main (string [] ARGs) throws namingexception, jmsexception {// todo auto-generated method stubsyncmessagetopicreceiver extends ER = new syncmessagetopicreceiver (); extends er. runclient ();}}
Asynchronous Receiver class asyncmessagetopicreceiver
Public class asyncmessagetopicreceiver implements messagelistener {/*** @ Param ARGs */private int expected_message_count = 2; private int messagecount = 0; private topicsubscriber subscriber = NULL; // note that the receiver in PTP mode compares private textmessage MSG = NULL; Public asyncmessagetopicreceiver () throws namingexception, jmsexception {context CTX = staticfunction. getcontext (); Topic messagetopic = (topic) CTX. lookup ("JMS/mytopic"); // create a connection factory topicconnectionfactory tconfactory = (topicconnectionfactory) CTX. lookup ("weblogic. JMS. connectionfactory "); // create a connection topicconnection tcon = tconfactory. createtopicconnection (); // create a session topicsession session = tcon. createtopicsession (false, session. auto_acknowledge); subscriber = session. createsubscriber (messagetopic); subscriber. setmessagelistener (this); // register a listener tcon. start ();} public Boolean expectmoremessage () {return messagecount <expected_message_count;} public static void main (string [] ARGs) {// todo auto-generated method stubint max_tries = 30; int trycount = 0; try {asyncmessagetopicreceiver recognition = new asyncmessagetopicreceiver (); While (recognition. expectmoremessage () & (trycount <max_tries) {system. out. println (trycount); try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} trycount ++;} catch (namingexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (jmsexception e) {// todo auto-generated catch blocke. printstacktrace () ;}@ overridepublic void onmessage (message m) {// todo auto-generated method stubsystem. out. println ("onmessage"); try {textmessage MSG = (textmessage) m; system. out. println ("er Er:" + MSG. gettext ();} catch (jmsexception e) {// todo auto-generated catch blocke. printstacktrace ();} messagecount ++ ;}}
The rest is lost, and the rest is the same as the code in PTP mode. The key is the difference between the transmitter and the receiver.
Okay, run WebLogic, run the receiver first, and run the transmitter. Of course, you can run multiple receivers at the same time, and you will see the result. This program has been tested, you can run it.
Haha ~~~~~.