Activemq practices topic publishing and subscription messages

Source: Internet
Author: User

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.

 

 

 

 

 

 

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.