It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
The difference between topic and queue:
Version One:
The Jms,java Message Service is one of the most important specifications of the Java EE platform, and is also a frequently used asynchronous technology in enterprise development. The JMS specification currently supports two message models: Point-to-point,queue and publish/subscribe (PUBLISH/SUBSCRIBE,TOPIC). Point-to-point:
Message producer production messages are sent to the queue, and then the message consumer takes out the queue and consumes the message. Note here: After the message is consumed, there is no more storage in the queue, so the message consumer cannot consume the message that has been consumed. Queue support exists for multiple consumers, but for a message, only one consumer can consume it. Publish/Subscribe
The message producer (release) publishes the message to topic and has multiple message consumers (subscriptions) consuming the message. Unlike point-to-point methods, messages posted to topic are consumed by all subscribers. =============================================================================================================== ===
Version two:
In JMS, topic implements publish and subscribe semantics. When a message is publish, it will be sent to all interested subscribers, so 0 to multiple subscriber will receive a copy of the message. However, when the message agent receives a message, only the Subscriber that activates the subscription can get a copy of the message.
The JMS queue executes the load balancer semantics. A message can only be received by one consumer. If there is no consumer available when the message is sent, it will be saved until the consumer that can handle the message is available. If a consumer receives a message and does not respond to it, then the message will be transferred to another consumer. A queue can have many consumer and load balancing in multiple available consumer
The two message transmission modes in the JMS specification are topic and queue, which are compared in the following table ():
|
Topic |
Queue |
Overview |
Publish Subscribe Messaging Publish subscription message |
Point-to-Point point-to-point |
There is no status |
Topic data is not landed by default and is stateless. |
Queue data is saved as a file by default on the MQ server, such as Active MQ, which is typically stored under $amq_home\data\kr-store\data. Can also be configured as DB storage. |
Integrity Assurance |
There is no guarantee that every piece of data published by publisher will be accepted by subscriber. |
The queue guarantees that each piece of data can be received by receiver. |
whether the message will be lost |
In general, when Publisher publishes a message to a topic, only the sub that is listening to the topic address can receive the message, and if no sub is listening, the topic is lost. |
Sender sends a message to the target Queue,receiver can receive the message on this queue asynchronously. Messages on the queue are not lost if they are not currently being picked up by receiver. |
Message Publishing Receive policy |
A one-to-many message publishing receive policy that listens to multiple sub-topic addresses to receive messages from Publisher. Sub receives notification MQ server |
One message to the receiving policy, a sender sends a message, only one receiver receives. After receiver is received, the MQ server is notified that the MQ server has deleted or otherwise acted on the messages in the queue. |
The following is the code that conforms to the Jms1.1 specification for using the queue to transmit messages, using active MQ as the implementation of JMS, and for cleaner code, consider the implementation of the Amq di:
Java Code public void testmysend () throws jmsexception { connection connection = new activemqconnectionfactory ( "admin" , "admin" , "tcp://localhost:61616" ). CreateConnection (); Session session = null ; Queue queue = null ; MessageProducer producer = null ; session = Connection.createsession (False , session.auto_acknowledge); &nBsp;queue=session.createqueue ("MyTest" ); producer= session.createproducer (queue); message msg=session.createtextmessage ("Hello" ); producer.send (msg); producer.close (); Session.close (); Connection.close (); } public void testmyreceive () throws jmsexception{ connection connection = new Activemqconnectionfactory ( "admin" , "admin" , "tcp://localhost:61616" ). CreateConnection (); session session = connection.createsession (False , session.auto_acknowledge); queue queue = session.createqueue ("MyTest" ); messageconsumer consumer= session.createconsumer ( Queue); consumer.setmessagelistener (new MyListener ()); connection.start (); consumer.close (); session.close (); connection.close (); }
The consumer needs to set a MessageListener:
Java Code private class mylistener implements messagelistener{ public void onmessage (message message) { System.out.println ("Msg start!----------------" ); try { system.out.println ("" + ((textmessage) message). GetText ()); } catch (jmsexception e) { e.printstacktrace (); } system.out.println ("msg End!----------------" ); } }
The following is the code that complies with the Jms1.1 specification to send messages using topic:
Java code public void Testmytopicpublisher () throws JMSException {