Comparison between topic and queue in two message transmission modes of JMS

Source: Internet
Author: User
Tags publish subscribe

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 does not guarantee that each piece of data published by publisher can be accepted by subscriber. queue ensures that each piece of data is received by the receiver.
whether messages are lost generally, when publisher publishes a message to a topic, only sub listening to 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 messages from 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 describes how to use queue to transmit messages in compliance with jms1.1 specifications:Code, Use active MQ as the implementation of JMS. For more clean code, consider implementing di of AMQ:

 

Java code
  1. Public VoidTestmysend ()ThrowsJmsexception {
  2. Connection connection =NewActivemqconnectionfactory ("Admin","Admin","TCP: // localhost: 61616"). Createconnection ();
  3. Session session =Null;
  4. Queue queue =Null;
  5. Messageproducer producer =Null;
  6. Session = connection. createsession (False, Session. auto_acknowledge );
  7. Queue = session. createqueue ("Mytest");
  8. Producer = session. createproducer (Queue );
  9. Message MSG = session. createtextmessage ("Hello");
  10. Producer. Send (MSG );
  11. Producer. Close ();
  12. Session. Close ();
  13. Connection. Close ();
  14. }
  15. Public VoidTestmyreceive ()ThrowsJmsexception {
  16. Connection connection =NewActivemqconnectionfactory ("Admin","Admin","TCP: // localhost: 61616"). Createconnection ();
  17. Session session = connection. createsession (False, Session. auto_acknowledge );
  18. Queue queue = session. createqueue ("Mytest");
  19. Messageconsumer consumer = session. createconsumer (Queue );
  20. Consumer. setmessagelistener (NewMylistener ());
  21. Connection. Start ();
  22. Consumer. Close ();
  23. Session. Close ();
  24. Connection. Close ();
  25. }
Public void testmysend () throws jmsexception {connection = new activemqconnectionfactory ("admin", "admin", "TCP: // localhost: 61616 "). createconnection (); Session session = NULL; queue = NULL; messageproducer producer = NULL; Session = connection. createsession (false, session. auto_acknowledge); 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 = new activemqconnectionfactory ("admin", "admin", "TCP: // localhost: 61616 "). createconnection (); Session session = connection. createsession (false, session. auto_acknowledge); 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
  1. Private ClassMylistenerImplementsMessagelistener {
  2. Public VoidOnmessage (message ){
  3. System. Out. println ("MSG start! ----------------");
  4. Try{
  5. System. Out. println (""+ (Textmessage) Message). gettext ());
  6. }Catch(Jmsexception e ){
  7. E. printstacktrace ();
  8. }
  9. System. Out. println ("MSG end! ----------------");
  10. }
  11. }
Private class mylistener implements messagelistener {public void onmessage (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 code uses topic to send messages in compliance with jms1.1 specifications:

 

Java code
  1. Public VoidTestmytopicpublisher ()ThrowsJmsexception {
  2. Connection connection =NewActivemqconnectionfactory ("Admin","Admin","TCP: // localhost: 61616"). Createconnection ();
  3. Session session =Null;
  4. Topic topic =Null;
  5. Messageproducer producer =Null;
  6. Session = connection. createsession (False, Session. auto_acknowledge );
  7. Topic = session. createtopic ("Mytopictest");
  8. Producer = session. createproducer (topic );
  9. Producer. setdeliverymode (deliverymode. non_persistent );
  10. Message MSG = session. createtextmessage ("Hello and whatever u say");
  11. Producer. Send (MSG );
  12. Producer. Close ();
  13. Session. Close ();
  14. Connection. Close ();
  15. }
Public void testmytopicpublisher () throws jmsexception {connection = new activemqconnectionfactory ("admin", "admin", "TCP: // localhost: 61616 "). createconnection (); Session session = NULL; Topic topic = NULL; messageproducer producer = NULL; Session = connection. createsession (false, session. auto_acknowledge); topic = session. createtopic ("mytopictest"); Producer = session. createproducer (topic); producer. setdeliverymode (deliverymode. non_persistent); message MSG = session. createtextmessage ("hello and whatever u say"); producer. send (MSG); producer. close (); Session. close (); connection. close ();}

The following code uses topics to receive messages in compliance with jms1.1 specifications:

 

Java code
  1. Public Static VoidTestmytopicconsumer ()ThrowsJmsexception, interruptedexception {
  2. Connection connection =NewActivemqconnectionfactory ("Admin","Admin","TCP: // localhost: 61616"). Createconnection ();
  3. Session session = connection. createsession (False, Session. client_acknowledge );
  4. Topic topic = session. createtopic ("Mytopictest");
  5. Messageconsumer consumer = session. createconsumer (topic );
  6. Consumer. setmessagelistener (NewMylistener ());
  7. Connection. Start ();
  8. }
Public static void testmytopicconsumer () throws jmsexception, interruptedexception {connection = new activemqconnectionfactory ("admin", "admin", "TCP: // localhost: 61616 "). createconnection (); Session session = connection. createsession (false, session. client_acknowledge); Topic topic = session. createtopic ("mytopictest"); messageconsumer consumer = session. createconsumer (topic); consumer. setmessagelistener (New mylistener (); connection. start ();}

 

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:

 

Java code
    1. Session session = connection. createsession (False, Session. client_acknowledge );
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.