This type of message actually refers to the message that uses the publish and subscribe message model in JMS. The following is a simple example.
Message publisher
Package COM. googlecode. garbagecan. jmsstudy. activemq. topic; </P> <p> Import Org. apache. activemq. activemqconnectionfactory; </P> <p> Import javax. JMS. *; </P> <p> public class topicpublisher {<br/> Public static void main (string [] ARGs) throws jmsexception {<br/> activemqconnectionfactory factory = new activemqconnectionfactory ("TCP: // localhost: 61616"); <br/> connection = factory. createconnection (); <br/> connection. start (); </P> <p> session = connection. createsession (false, session. auto_acknowledge); <br/> topic = session. createtopic ("mytopic. messages "); </P> <p> messageproducer producer = session. createproducer (topic); <br/> producer. setdeliverymode (deliverymode. non_persistent); </P> <p> while (true) {<br/> textmessage message = session. createtextmessage (); <br/> message. settext ("message _" + system. currenttimemillis (); <br/> producer. send (Message); <br/> system. out. println ("sent message:" + message. gettext (); </P> <p> try {<br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> // session. close (); <br/> // connection. stop (); <br/> // connection. close (); <br/>}< br/>}
message subscriber (Message consumer)
Package COM. googlecode. garbagecan. jmsstudy. activemq. topic; </P> <p> Import Org. apache. activemq. activemqconnectionfactory; </P> <p> Import javax. JMS. *; </P> <p> public class topicsubscriber {<br/> Public static void main (string [] ARGs) throws jmsexception {<br/> activemqconnectionfactory factory = new activemqconnectionfactory ("TCP: // localhost: 61616"); <br/> connection = factory. createconnection (); <br/> connection. start (); </P> <p> session = connection. createsession (false, session. auto_acknowledge); <br/> topic = session. createtopic ("mytopic. messages "); </P> <p> messageconsumer consumer = session. createconsumer (topic); <br/> consumer. setmessagelistener (New messagelistener () {<br/> Public void onmessage (message) {<br/> textmessage TM = (textmessage) message; <br/> try {<br/> system. out. println ("received message:" + TM. gettext (); <br/>} catch (jmsexception e) {<br/> E. printstacktrace (); <br/>}< br/>}); <br/> // session. close (); <br/> // connection. stop (); <br/> // connection. close (); <br/>}< br/>
Run the two classes respectively, and you can see the published classes of publisher, which can be accepted by subscriber.