comparison of technical features between queue and topic
|
Topic |
Queue |
Profile |
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 biggest difference between the topic and the queue is that the topic is in the form of a broadcast that notifies all online listeners that there is a new message, that the client without the listener will not receive the message, and that the queue notifies more than one client in the listening state in the form of a point-to-point.
Queue communication:
Message provider:
public static void Main (string[] args) throws JMSException {//ConnectionFactory: Connection Factory, JMS uses it to create a connection Conn Ectionfactory connectionfactory = new Activemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password, "tcp://localhost:61616"); JMS Client to JMS Provider connection Connection Connection = Connectionfactory.createconnection (); Connection.start (); Session: A thread that sends or receives a message session session = Connection.createsession (Boolean.true, Session.auto_acknowledge); Destination: The destination of the message, to whom the message is sent. Get session attention parameter value My-queue is the name of the query Destination Destination = Session.createqueue ("My-queue"); Destination Destination = session.createtopic ("My-topic"); MessageProducer: Message producer MessageProducer producer = Session.createproducer (destination); Set non-persistent producer.setdeliverymodE (deliverymode.non_persistent); Send a message sendmsg (session, producer); Session.commit (); Connection.close (); } public static void Sendmsg (Session session, MessageProducer producer) throws JMSException {//Create a text message TextMessage message = Session.createtextmessage ("Hello activemq!"); A message is sent by the producer of the message producer.send; System.out.println (Message.tostring ()); }
Message recipients:
public static void Main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection connectionfactory connectionf Actory; CONNECTION:JMS client-to-JMS Provider connection Connection Connection = null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; Consumer, message recipient Messageconsumer consumer; ConnectionFactory = new Activemqconnectionfactory (Activemqconnection.default_user, ActiveMQ Connection.default_password, "tcp://localhost:61616"); try {//Construct get Connection object from factory connection = Connectionfactory.createconnection (); Start Connection.start (); Get operation Connection session = Connection.createsession (Boolean.false, Session.auto_acknowledge); Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in ACTIVEMQ console destination = Session.createqueue ("M Y-queUE "); Consumer = session.createconsumer (destination); while (true) {TextMessage message = (textmessage) consumer.receive (1000); if (null! = message) {SYSTEM.OUT.PRINTLN ("Receive Message" + Message.gettext ()); } else {break; }}} catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = connection) connection.close (); } catch (Throwable ignore) {}}}
Topic Communication:
Broadcast provider:
private static final int send_number = 5; public static void SendMessage (Session session, MessageProducer producer) throws Exception {for (int i = 1; I <=SEND_NUMBER; i++) {TextMessage message = session. Createtextmessage ("Messages sent by ACTIVEMQ" + i); Send message to Destination place System.out.println ("Send message:" + "ActiveMq message sent" + i); Producer.send (message); }} public static void Main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection connectionfactory ConnectionFactory; CONNECTION:JMS client-to-JMS provider connection Connection Connection = null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; MessageProducer: Message sender MessageProducer producer; TextMessage message; Constructs the ConnectionFactory instance object, which is implemented here with the Activemq jar connectionfactory = new Activemqconnectionfactory (activemqconnEctionfactory.default_user, Activemqconnectionfactory.default_password,activemqconnectionfactory.default_broker _bind_url); try {//Construct get Connection object from factory connection = Connectionfactory.createconnection (); Start Connection.start (); Get operation Connection Session = Connection.createsession (true, Session.auto_acknowledge); Get session Note parameter value Firsttopic is a server topic (this is the only difference compared to the sending of a queue message) Destination = Session.createtopic ("Firsttopic"); Get the message Generator "sender" producer = Session.createproducer (destination); Set not to persist, learn here, actually according to the project decision Producer.setdeliverymode (Deliverymode.persistent); Constructs the message, writes here to die, the project is the parameter, or the method obtains SendMessage (session, producer); Session.commit (); } catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = connection) connection.close (); } catch (Throwable ignore) {}}}
Broadcast recipient:
Private String ThreadName; Receivetopic (String threadname) {this.threadname = ThreadName; public void Run () {///ConnectionFactory: Connection Factory, JMS uses it to create a connection connectionfactory connectionfactory; CONNECTION:JMS Client to JMS provider connection Connection Connection =null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; Consumer, message recipient Messageconsumer consumer; ConnectionFactory = new Activemqconnectionfactory (Activemqconnectionfactory.default_user, ActiveMQConnect Ionfactory.default_password,activemqconnectionfactory.default_broker_bind_url); try {//Construct get Connection object from factory connection = Connectionfactory.createconnection (); Start Connection.start (); Gets the operation connection, which automatically sends a successful response to the server by default session = Connection.createsession (false, Session.auto_acknowledge); Get SESsion Note The parameter value Firsttopic is a server topic destination = Session.createtopic ("Firsttopic"); Consumer = session.createconsumer (destination); while (true) {//sets the time at which the receiver receives the message, for testing purposes, this is set to 100s textmessage message = (TextMessage) c Onsumer. Receive (1 * 1000); if (null! = message) {System.out.println ("thread" +threadname+ "received message:" + message.gettext ()); } else {continue; }}} catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = connection) connection.close (); } catch (Throwable ignore) {}}} public static void Main (string[] args) { Here, 3 threads are started to listen for firsttopic messages, not the same way as the queue three thread can receive the same message receivetopic receive1=new RECEIVEtopic ("Thread1"); Receivetopic receive2=new receivetopic ("Thread2"); Receivetopic receive3=new receivetopic ("Thread3"); Thread thread1=new thread (RECEIVE1); Thread thread2=new thread (receive2); Thread thread3=new thread (RECEIVE3); Thread1.start (); Thread2.start (); Thread3.start (); }
If transferring Chinese: You can set the encoding format of the string new string (Msg.getbytes ("Utf-8"), "iso-8859-1"); New String (Message.tostring (). GetBytes ("Iso-8859-1"), "UTF-8");
ACTIVEMQ Demo Guide