Easy to do JMS (c)--ACTIVEMQ simple HelloWorld Example

Source: Internet
Author: User

The first post is a basic concept of JMS (a) –JMS, we introduce two kinds of JMS message models: The point-to-point and the publish-subscribe model, and the two ways messages are consumed: synchronous and asynchronous, the JMS programming model object, and finally the advantages of JMS.

Second blog Brief introduction to JMS (ii) –ACTIVEMQ and installation, we introduced the message middleware ACTIVEMQ, installation, startup, and advantages and disadvantages.

In this blog post, we use ACTIVEMQ to implement a point-to-point message model for everyone. If you have a shallow understanding of point-to-point models, take a look at the introduction to the first blog post.

JMS is not really as big as you think, after reading this blog post, you will know what is simple, the following directly into the topic.

Development environment

We are using ACTIVEMQ 5.11.1 release of the Windows version, the latest version of the official website is ACTIVEMQ 5.12.0 release, we can download it by ourselves.

It is important to note that, at development time, the Activemq-all-5.11.1.jar package inside the Apache-activemq-5.11.1-bin.zip is added to the classpath below, which contains the implementation of all JMS interface APIs.

Build the development environment
    • Build a project
      We just need to build a Java project on it, import the jar package, project:

The point-to-point message model requires only a message generator and a message consumer, and we write the code below.

    • Writing producers

Package com.tgb.activemq;

Import javax.jms.Connection;
Import Javax.jms.ConnectionFactory;
Import javax.jms.Destination;
Import javax.jms.JMSException;
Import Javax.jms.MessageProducer;
Import javax.jms.Session;
Import Javax.jms.TextMessage;

Import org.apache.activemq.ActiveMQConnection;
Import Org.apache.activemq.ActiveMQConnectionFactory;
/**
* Producer of messages (sender)
* @author Liang
*
*/
public class JMSProducer {

Default connection user name private static final String USERNAME = activemqconnection.default_user;//default connection password private static final string PASSWORD = activemqconnection.default_password;//Default connection address private static final String Brokeurl = activemqconnection.default_broker_url;//number of messages sent private static final int sendnum = 10;public static void Main (string[]    args) {//connection factory connectionfactory ConnectionFactory;    Connection Connection Connection = null;    A thread session that accepts or sends a message;    The destination of the message Destination Destination;    Message producer MessageProducer MessageProducer; Instantiate the connection factory connectionfactory = new Activemqconnectionfactory (Jmsproducer.username, Jmsproducer.password,    Jmsproducer.brokeurl);        try {//Get connection via connection factory connection = Connectionfactory.createconnection ();        Start Connection Connection.start ();        Create Session session = Connection.createsession (true, Session.auto_acknowledge);    Create a message queue called HelloWorld destination = Session.createqueue ("HelloWorld");    Create message producer MessageProducer = Session.createproducer (destination);        Send Message SendMessage (session, MessageProducer);    Session.commit ();    } catch (Exception e) {e.printstacktrace ();            }finally{if (connection! = null) {try {connection.close ();            } catch (JMSException e) {e.printstacktrace (); }}}}/** * Send Message * @param session * @param messageproducer message producer * @throws Exception */public static void Sendm Essage (Session session,messageproducer messageproducer) throws exception{for (int i = 0; i < jmsproducer.sendnum; I        + +) {//Create a text message textmessage msg = session.createtextmessage ("ActiveMQ send Message" +i);        SYSTEM.OUT.PRINTLN ("Send message: ACTIVEMQ Send Message" + i);    A message is sent by the producer of the message messageproducer.send; }}

}

-Write Consumer

Package com.tgb.activemq;

Import javax.jms.Connection;
Import Javax.jms.ConnectionFactory;
Import javax.jms.Destination;
Import javax.jms.JMSException;
Import Javax.jms.MessageConsumer;
Import javax.jms.Session;
Import Javax.jms.TextMessage;

Import org.apache.activemq.ActiveMQConnection;
Import Org.apache.activemq.ActiveMQConnectionFactory;
/**
* Message to consumers (recipients)
* @author Liang
*
*/
public class Jmsconsumer {

private static final String USERNAME = activemqconnection.default_user;//Default connection user name private static final String PASSWORD = Act ivemqconnection.default_password;//default connection password private static final String Brokeurl = Activemqconnection.default_broker_ url;//Default connection address public static void main (string[] args) {connectionfactory connectionfactory;//connection factory Connection Connectio n = null;//Connection session session;//the thread that accepts or sends the message Destination the destination of the destination;//message Messageconsumer messageconsumer;/ /Message Consumer//instantiation of the connection factory connectionfactory = new Activemqconnectionfactory (Jmsconsumer.username, Jmsconsumer.password, JMSC Onsumer.    Brokeurl);        try {//Get connection via connection factory connection = Connectionfactory.createconnection ();        Start Connection Connection.start ();        Create Session session = Connection.createsession (false, Session.auto_acknowledge);        Create a message queue that connects HelloWorld destination = Session.createqueue ("HelloWorld"); Create message Consumer Messageconsumer = Session.createconsumer (destInation);            while (true) {TextMessage TextMessage = (textmessage) messageconsumer.receive (100000);            if (textmessage! = null) {SYSTEM.OUT.PRINTLN ("message Received:" + textmessage.gettext ());            }else {break;    }}} catch (JMSException e) {e.printstacktrace (); }}

}

Run
    1. First, start Activemq, how to start activemq How to start, see the second blog post. Enter: http://localhost:8161/admin/in the browser and start execution:
    2. Run the sender, Eclipse console output, such as:

      At this point, we first look at the ACTIVEMQ server, queues content as follows:

      We can see that a message queue called HelloWorld is created, and 10 messages in the queue are not consumed, and we can also see what messages are available through browse, such as:

      If the messages in these queues are deleted, consumers will not be able to consume them.

    3. We continue to run the consumer, the Eclipse console prints the message as follows:

      At this point, we first look at the ACTIVEMQ server, queues content as follows:

      We can see the HelloWorld message queue changes, one more message, the queue of 10 messages are consumed, click Browse to view, is already empty.
      Click on active consumers and we can see this consumer's details:

Our example to this end, you can own more points ACTIVEMQ server content, further familiar with the ACTIVEMQ.

Summarize

This blog post we implement the point-to-point message model and send a synchronization message, is not very simple?

In the following blog post, we will implement an example of ACTIVEMQ and spring consolidation.

SOURCE download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Easy to do JMS (c)--ACTIVEMQ simple HelloWorld Example

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.