Different from queue, a topic implements a publishing/subscription model,
In a publish/subscribe model-based application or product, the client subscribes to messages based on the topic, a little like a billboard. Publishers and subscribers are generally anonymous and can dynamically publish or subscribe to content. The message system will carefully process the distribution of messages to all subscribers that subscribe to a topic. messages will only be sent to the current subscriber, and then expire. new subscribers cannot receive messages that have just expired.
The message publishing and subscription mechanism has the following features:
1) Each message can have multiple consumers
2) the publisher and subscriber have time dependencies. Only the subscriber that has subscribed to a topic can receive the message. The subscriber must be active to obtain the message.
3) when a message may have more than one receiver, use the publish/subscribe message mechanism.
In the following example, two consumers are started to listen to a topic and then send multiple messages to the topic cyclically.
The specific code is as follows:
/*** @ Author administrator * @ description different from queue, topic implements the publish/subscribe model. In the following example, two consumers are started to listen to a topic together, then, multiple messages are repeatedly sent to the topic. * The result shows that each message is consumed by all consumers */package COM. WL. JMS; import javax. JMS. connection; import javax. JMS. jmsexception; import javax. JMS. message; import javax. JMS. messageconsumer; import javax. JMS. messagelistener; import javax. JMS. messageproducer; import javax. JMS. session; import javax. JMS. textmessage; Import javax. JMS. topic; import Org. apache. activemq. activemqconnectionfactory; import Org. apache. activemq. command. activemqtopic; public class topictest {/*** @ Param ARGs * @ throws jmsexception */public static void main (string [] ARGs) throws jmsexception {// todo auto-generated method stubactivemqconnectionfactory factory = new activemqconnectionfactory ("VM: // localhost"); connection = factory. cr Eateconnection (); connection. start (); // create a topictopic topic = new activemqtopic ("testtopic"); Session session = connection. createsession (false, session. auto_acknowledge); // register consumer 1 messageconsumer consumer1 = session. createconsumer (topic); consumer1.setmessagelistener (New messagelistener () {public void onmessage (message m) {try {system. out. println ("consumer1 get:" + (textmessage) M ). gettext ();} catch (jmsexcep Tion E) {e. printstacktrace () ;}}); // register consumer 2 messageconsumer consumer2 = session. createconsumer (topic); consumer2.setmessagelistener (New messagelistener () {public void onmessage (message m) {try {system. out. println ("consumer2 get:" + (textmessage) M ). gettext ();} catch (jmsexception e) {e. printstacktrace () ;}}); // create a producer and send multiple messages. Messageproducer producer = session. createproducer (topic); For (INT I = 0; I <10; I ++) {producer. send (Session. createtextmessage ("message:" + I ));}}}
The running result is as follows:
The result shows that each message is consumed by all consumers.