Comparison between JMS topics and queue

Source: Internet
Author: User
Tags publish subscribe amq

The two message transmission modes Topic and Queue in the Jms specification are compared as follows ():

  Topic Queue
Summary Publish Subscribe messaging Publish and Subscribe messages Point-to-Point
Status or not Topic data is stateless by default. By default, Queue data is stored as files on the mq server. For example, Active MQ is generally stored under $ AMQ_HOME/data/kr-store/data. You can also configure it to DB storage.
Integrity assurance It is not guaranteed that each piece of data published by publisher can be accepted by Subscriber. Queue ensures that each piece of data can be received by the receiver.
Will messages be lost? Generally, when publisher publishes a message to a topic, only the sub that is listening for the topic address can receive the message. If no sub is listening, the topic is lost. The Sender sends a message to the target Queue, which can receive the message on this Queue asynchronously. Messages on the Queue will not be lost if there is no receiver.
Message publishing and receiving policy One-to-multiple message publishing and receiving policies. When multiple sub listening to the same topic address can receive messages sent by publisher. The Sub receives the notification to the mq server. One-to-one message publishing and receiving policy. Only one receiver can receive messages sent by one sender. After the receiver receives the message, it notifies the mq server that it has received the message. the mq server deletes the message in the queue or performs other operations.

The following code uses Queue to transmit messages in accordance with Jms1.1 specifications. Amq is used as the implementation of Jms. To make the code more clean, you can consider implementing DI of Amq:
 
Public void testMySend () throws JMSException {<br/> Connection connection = new ActiveMQConnectionFactory ("admin", "admin", "tcp :/// localhost: 61616 "). createConnection (); <br/> Session session = null; <br/> Queue queue = null; <br/> MessageProducer producer = null; <br/> session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE); <br/> queue = session. createQueue ("myTest"); <br/> produce R = session. createProducer (queue); <br/> Message msg = session. createTextMessage ("hello"); <br/> producer. send (msg); <br/> producer. close (); <br/> session. close (); <br/> connection. close (); <br/>}< br/> <br/> public void testMyReceive () throws JMSException {<br/> Connection connection = new ActiveMQConnectionFactory ("admin ", "admin", "tcp: // localhost: 61616 "). createConnection (); <br/> Session Session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE); <br/> Queue queue = session. createQueue ("myTest"); <br/> MessageConsumer consumer = session. createConsumer (queue); <br/> consumer. setMessageListener (new MyListener (); <br/> connection. start (); <br/> consumer. close (); <br/> session. close (); <br/> connection. close (); <br/>}< br/> <br/> the consumer must set a MessageListener: <br/> <B R/> private class MyListener implements MessageListener {<br/> public void onMessage (Message message) {<br/> System. out. println ("msg start! ---------------- "); <Br/> try {<br/> System. out. println ("" + (TextMessage) message ). getText (); <br/>} catch (JMSException e) {<br/> e. printStackTrace (); <br/>}< br/> System. out. println ("msg end! ---------------- "); <Br/>}< br/>}

The following code uses topic to send messages in compliance with jms1.1 specifications: </P> <p> Public void testmytopicpublisher () throws jmsexception {<br/> connection = new activemqconnectionfactory ("admin", "admin", "TCP: // localhost: 61616 "). createconnection (); <br/> session = NULL; <br/> topic = NULL; <br/> messageproducer producer = NULL; <br/> session = connection. createsession (false, session. auto_acknowledge); <br/> topic = session. createtopic ("mytopictest"); <br/> producer = session. createproducer (topic); <br/> producer. setdeliverymode (deliverymode. non_persistent); <br/> message MSG = session. createtextmessage ("hello and whatever u say"); <br/> producer. send (MSG); <br/> producer. close (); <br/> session. close (); <br/> connection. close (); <br/>}< br/> the following code uses the topic to receive messages in compliance with jms1.1 specifications: </P> <p> Public static void testmytopicconsumer () throws jmsexception, interruptedexception {<br/> connection = new activemqconnectionfactory ("admin", "admin", "TCP: // localhost: 61616 "). createconnection (); <br/> session = connection. createsession (false, session. client_acknowledge); <br/> topic = session. createtopic ("mytopictest"); <br/> messageconsumer consumer = session. createconsumer (topic); <br/> consumer. setmessagelistener (New mylistener (); <br/> connection. start (); <br/>}< br/>   

Jms can have two parameters when creating a Session. The first parameter is whether a transaction is used, and the second parameter is the method in which the consumer confirms to the sender that the message has been received:
Session session = connection. createSession (false, Session. CLIENT_ACKNOWLEDGE );
There are three methods to confirm messages:
AUTO_ACKNOWLEDGE (automatic notification)
CLIENT_ACKNOWLEDGE (the client determines the notification time)
DUPS_ OK _ACKNOWLEDGE (latency // batch notification)
If the client determines the notification time, you must explicitly call message. acknowledge () in MessageListener to notify the server. The server takes corresponding actions after receiving the notification.

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.