Using Java to write ACTIVEMQ queue patterns and theme patterns

Source: Internet
Author: User
Tags gettext sessions

Message presentation for queue mode

This section briefly demonstrates how to use the JMS interface specification to connect ACTIVEMQ, first creating a Maven project, and adding ACTIVEMQ dependencies in the Pom.xml file:

<dependencies>    <dependency>        <groupId>org.apache.activemq</groupId>        <artifactId>activemq-all</artifactId>        <version>5.9.0</version>    </dependency></dependencies>

Create a Appproducer class that demonstrates how to use the ACTIVEMQ queue pattern under the JMS interface specification. The code is as follows:

Package Org.zero01.jms.queue;import Org.apache.activemq.activemqconnectionfactory;import javax.jms.*;/** * @program : Jms-test * @description: Message producer * @author: $ * @create: 2018-05-26 16:44 **/public class Appproducer {//ACTIVEMQ server    The URL address of the default communication port is 61616 private static final String URL = "tcp://192.168.190.129:61616";    The name of the queue private static final String queue_name = "Queue-test"; public static void Main (string[] args) throws JMSException {//1. Creating a Connection factory object (connectionfactory) connectionfact        Ory connectionfactory = new Activemqconnectionfactory (URL);        2. Create Connection object (Connection) Connection Connection = Connectionfactory.createconnection ();        3. Start the connection Connection.start (); 4. Create session sessions, the first parameter means the transaction is enabled, and the second parameter indicates which answer mode to start, which is enabled by auto answer session session = Connection.createsession (False, session.        Auto_acknowledge);        5. Create destination (destination) Destination Destination = Session.createqueue (queue_name); 6. Create a producer MessagEproducer producer = Session.createproducer (destination);  Loop send message for (int i = 0; i < i++) {//7. Create a message, here is a simple text message body TextMessage textmessage            = Session.createtextmessage ("Test" + i);            8. Use message producers to send messages to destinations producer.send (destination, textmessage);        SYSTEM.OUT.PRINTLN ("Message sent successfully:" + textmessage.gettext ());    }//9. Close connection Connection.close (); }}

After writing the code, log in to Activemq's administration page, click on the tab "Queues" to enter the following interface, you can see here now what data are not:

After we run the code written above, we refresh the page and see that we have the data now:

Then we write a consumer to consume the messages in the queue. The code is as follows:

Package Org.zero01.jms.queue;import Org.apache.activemq.activemqconnectionfactory;import javax.jms.*;/** * @program : Jms-test * @description: Message consumer * @author: $ * @create: 2018-05-26 17:08 **/public class Appconsumer {//ACTIVEMQ server    The URL address of the default communication port is 61616 private static final String URL = "tcp://192.168.190.129:61616";    The name of the queue private static final String queue_name = "Queue-test"; public static void Main (string[] args) throws JMSException {//1. Creating a Connection factory object (connectionfactory) connectionfact        Ory connectionfactory = new Activemqconnectionfactory (URL);        2. Create Connection object (Connection) Connection Connection = Connectionfactory.createconnection ();        3. Start the connection Connection.start (); 4. Create session sessions, the first parameter means the transaction is enabled, and the second parameter indicates which answer mode to start, which is enabled by auto answer session session = Connection.createsession (False, session.        Auto_acknowledge);        5. Create destination (destination) Destination Destination = Session.createqueue (queue_name); 6. Create Consumer MessagEconsumer Messageconsumer = Session.createconsumer (destination); 7. Create a listener messageconsumer.setmessagelistener (message), {textmessage TextMessage = (textmessage            ) message;            try {System.out.println ("Receive message:" + textmessage.gettext ());            } catch (JMSException e) {e.printstacktrace ();    }        }); }}

After writing the code, click on the tab "Connections" to enter the following interface, you can see here now a connection is not:

After we run the code written above, refresh the page and see that there is now a consumer connected:

After the consumer runs, it is a thread-blocking state, which is to keep the connection with the ACTIVEMQ server. Now we're going to start a consumer, and there are two consumers:

After starting two consumers, run the code of the producer. Let's look at a phenomenon in the queue pattern, as follows:

Console printing information as above, it has been found that the consumer 1 of the messages consumed is even, while the consumer 2 consumption of the message is odd. This is a phenomenon of the queue pattern, consumers will evenly, as far as possible, the average consumption of messages in the queue.

Message presentation in topic mode

The code for the theme mode is very similar to the queue pattern, except that the method for creating the destination is different. The message Publisher code is as follows:

Package Org.zero01.jms.topic;import Org.apache.activemq.activemqconnectionfactory;import javax.jms.*;/** * @program : Jms-test * @description: Message Publisher * @author: $ * @create: 2018-05-26 16:44 **/public class Apppublisher {//ACTIVEMQ services    The default communication port is 61616 private static final String URL = "tcp://192.168.190.129:61616";    The name of the queue private static final String topic_name = "Topic-test"; public static void Main (string[] args) throws JMSException {//1. Creating a Connection factory object (connectionfactory) connectionfact        Ory connectionfactory = new Activemqconnectionfactory (URL);        2. Create Connection object (Connection) Connection Connection = Connectionfactory.createconnection ();        3. Start the connection Connection.start (); 4. Create session sessions, the first parameter means the transaction is enabled, and the second parameter indicates which answer mode to start, which is enabled by auto answer session session = Connection.createsession (False, session.        Auto_acknowledge);        5. Create destination (destination) Destination Destination = Session.createtopic (topic_name); 6. Create a producer MessaGeproducer producer = Session.createproducer (destination);  Loop send message for (int i = 0; i < i++) {//7. Create a message, here is a simple text message body TextMessage textmessage            = Session.createtextmessage ("Test" + i);            8. Use message producers to send messages to destinations producer.send (destination, textmessage);        SYSTEM.OUT.PRINTLN ("Message sent successfully:" + textmessage.gettext ());    }//9. Close connection Connection.close (); }}

The

Message Subscriber code is as follows:

Package Org.zero01.jms.topic;import Org.apache.activemq.activemqconnectionfactory;import javax.jms.*;/** * @program : Jms-test * @description: Message subscribers * @author: * @create: 2018-05-26 17:08 **/public class Appsubscriber {//ACTIVEMQ clothing    The default communication port is 61616 private static final String URL = "tcp://192.168.190.129:61616";    The name of the queue private static final String topic_name = "Topic-test"; public static void Main (string[] args) throws JMSException {//1. Creating a Connection factory object (connectionfactory) connectionfact        Ory connectionfactory = new Activemqconnectionfactory (URL);        2. Create Connection object (Connection) Connection Connection = Connectionfactory.createconnection ();        3. Start the connection Connection.start (); 4. Create session sessions, the first parameter means the transaction is enabled, and the second parameter indicates which answer mode to start, which is enabled by auto answer session session = Connection.createsession (False, session.        Auto_acknowledge);        5. Create destination (destination) Destination Destination = Session.createtopic (topic_name); 6. Create Consumer MessAgeconsumer Messageconsumer = Session.createconsumer (destination); 7. Create a listener messageconsumer.setmessagelistener (message), {textmessage TextMessage = (textmessage            ) message;            try {System.out.println ("Receive message:" + textmessage.gettext ());            } catch (JMSException e) {e.printstacktrace ();    }        }); }}

But the theme mode and queue mode consumption is not the same, the queue mode is to start the message producer to send messages to the queue, and then consumers to spend. The theme mode is to start the message subscribers to make the subscription, and then start the message Publisher to publish the message, so that the message subscribers can receive messages published by the message Publisher. So we start the message subscribers first, and then start the message Publisher. When you are finished booting, you should see the following information on the "Topics" page of ACTIVEMQ:

In addition to the above differences, let's start two subscribers and then launch the Publisher to see what the subscribers are receiving:

Console printing information as above, you can see that two subscribers each received the same message. That is, if there are two subscribers, there will be two copies of the message and multiple subscribers.

Using Java to write ACTIVEMQ queue patterns and theme patterns

Related Article

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.