ActiveMQ: a message middleware based on JMS (Java Message Service)

Time:2019-11-8

Message middleware uses efficient and reliable message passing mechanism for platform independent data communication, and integrates distributed system based on data communication. By providing the model of message delivery and message queuing, it can extend the communication between processes in the distributed environment. The message middleware in Java is JMS – Java message service. ActiveMQ is a good choice in the common type of message middleware. Next, we will briefly introduce what is JMS and ActiveMQ, and then we will introduce two modes in ActiveMQ — queue model and topic mode.

JMS introduction

  1. Basic concepts of JMS
    JMS (Java Message Service) is a standard API for accessing the enterprise message system. It is convenient for Java applications in the message system to exchange messages, and simplifies the development of enterprise applications by providing standard interfaces for generating, sending and receiving messages.

JMS application consists of the following parts:
JMS provider: is a message system, which implements the JMS interface and provides management and control functions.
JMS clients: programs and components written in the Java language that generate and use messages.
Messages: is the object of messages passed between JMS clients.
Managed objects: a pre selected set of JMS objects generated by people using JMS clients. There are two types of objects:
Destinations and connection factories.

  1. JMS basic functions
    JMS is an application program interface used to communicate with message oriented middleware. It supports not only point-to-point domains, but also publish / subscribe domains, and supports the following types: approved messaging, transactional messaging, consistent messaging, and durable subscriber support. JMS also provides another way to integrate your application with the old back-end system.

  2. Message service type
    1) point to point (PTP) mode: point-to-point model. Messages are sent by a JMS client (publisher) to a destination on the server, a queue. Another JMS client (subscriber) can access the queue and get the message from the server.
    2) publish / subscribe (Pub / sub) method: publish subscribe model. Here, a JMS client still publishes a message to a destination on the server, but this destination is called a topic, which can be accessed by multiple subscribers. The message will remain in the topic until all subscribers to the topic have taken a copy of the message. The message also includes a parameter that defines the durability of the message (how long it can wait for subscribers on the server).

Introduction to acticemq:

ActiveMQ is an open source message system provided by Apache, which is fully implemented in Java. Therefore, it can well support the JMS (Java Message Service) specification proposed by J2EE. JMS is a set of Java application program interfaces, which provides a series of services such as message creation, sending and reading. JMS provides a set of common application program interface and response syntax, similar to the unified access interface JDBC of Java database. It is a kind of vendor independent API, which enables Java programs to communicate well with message components of different vendors.

JMS supports two message sending and receiving models. One is called P2P (ponit to point) model, which uses point-to-point method to send messages. P2P model is based on queue. Message producers send messages to the queue, and message consumers receive messages from the queue. The existence of queue makes asynchronous transmission of messages possible. P2P model is used in the case of point-to-point message delivery. The following two figures describe the two models:
ActiveMQ: a message middleware based on JMS (Java Message Service)
ActiveMQ: a message middleware based on JMS (Java Message Service)

Two models of ActiveMQ implemented in Java

[1] queue model

  • Producer side of queue model

    package com.imooc.jms.queue;
    import java.awt.font.TextMeasurer;
    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.ActiveMQConnectionFactory;

    public class AppProducer {

    private static final String url = "tcp://192.168.133.1:61616";
       private static final String queueName = "queue-test";
       
       public static void main(String[] args) throws JMSException {
           
           //1. Create connectionfactory
           ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
           
           //2. Create connection
           Connection connection = connectionFactory.createConnection();
           
           //3. Start link
           connection.start();
           
           //4. Create a session
           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
           //5. Create goals
           Destination destination =  session.createQueue(queueName);
           
           //6. Create producer to send message to target
           MessageProducer  producer=  session.createProducer(destination);
           
           for(int i = 0 ;i<100;i++){
               //7. Create message
               TextMessage textMessage = session.createTextMessage("test"+i);
               //8. Publish news
               producer.send(textMessage);
               System. Out. Println ("send message:" + textmessage. Gettext());
           }    
           //9. Close connection
           connection.close();
       }

    }

Description: This is the producer side in the queue model. The following describes the end code:
New a connectionfactory — “new a connection –” start this connection — “create a session –” create a message sending destination — “create a message producer sending a message to the destination –” send a message “–” close the connection “

  • Consumer side of queue model

    package com.imooc.jms.queue;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import org.apache.activemq.ActiveMQConnectionFactory;

    public class AppConsumer {

    private static final String url = "tcp://192.168.133.1:61616";
       private static final String queueName = "queue-test";
       
       public static void main(String[] args) throws JMSException {
           
           //1. Create connectionfactory
           ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
           
           //2. Create connection
           Connection connection = connectionFactory.createConnection();
           
           //3. Start link
           connection.start();
           
           //4. Create a session
           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
           //5. Create goals
           Destination destination =  session.createQueue(queueName);
           
          //6. Create a consumer
           MessageConsumer consumer =  session.createConsumer(destination);
           
           //7. Create a listener
           consumer.setMessageListener(new MessageListener() {
               
               public void onMessage(Message message) {
                   
                   TextMessage textMessage = (TextMessage)message;
                   try {
                       System. Out. Println ("accept message:" + textmessage. Gettext());
                   } catch (JMSException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
                   
               }
           });
           
           //9. Close connection
           //connection.close();
       }

    }

Description: This is the consumer end in the queue model. The code of this end is described below:
New a connectionfactory — “new a connection –” start this connection — “create a session –” create a message sending destination — “create a message consumer –” create a message listener listening to messages — “close the connection”

Theme model

  • Producer side of theme model

    package com.imooc.jms.topic;
    import java.awt.font.TextMeasurer;
    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.ActiveMQConnectionFactory;

    public class AppProducer {

    private static final String url = "tcp://192.168.133.1:61616";
       private static final String topicName = "topic-test";
       
       public static void main(String[] args) throws JMSException {
           
           //1. Create connectionfactory
           ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
           
           //2. Create connection
           Connection connection = connectionFactory.createConnection();
           
           //3. Start link
           connection.start();
           
           //4. Create a session
           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
           //5. Create goals
           Destination destination =  session.createTopic(topicName);
           
           //6. Create producer to send message to target
           MessageProducer  producer=  session.createProducer(destination);
           
           for(int i = 0 ;i<100;i++){
               //7. Create message
               TextMessage textMessage = session.createTextMessage("test"+i);
               //8. Publish news
               producer.send(textMessage);
               System. Out. Println ("send message:" + textmessage. Gettext());
           }
           
           //9. Close connection
           connection.close();
       }

    }

  • Consumer side of theme model

    package com.imooc.jms.topic;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import org.apache.activemq.ActiveMQConnectionFactory;

    public class AppConsumer {

    private static final String url = "tcp://192.168.133.1:61616";
       private static final String topicName = "topic-test";
       
       public static void main(String[] args) throws JMSException {
           
           //1. Create connectionfactory
           ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
           
           //2. Create connection
           Connection connection = connectionFactory.createConnection();
           
           //3. Start link
           connection.start();
           
           //4. Create a session
           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
           //5. Create goals
           Destination destination =  session.createTopic(topicName);
           
          //6. Create a consumer
           MessageConsumer consumer =  session.createConsumer(destination);
           
           //7. Create a listener
           consumer.setMessageListener(new MessageListener() {
               
               public void onMessage(Message message) {
                   
                   TextMessage textMessage = (TextMessage)message;
                   try {
                       System. Out. Println ("accept message:" + textmessage. Gettext());
                   } catch (JMSException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
                   
               }
           });
           
           //9. Close connection
           //connection.close();
       }

    }

Finally, create the project source address:
Link: http://pan.baidu.com/s/1bo1psib password: KHYC

Recommended Today

The use of progressbarcontrol, a progress bar control of devexpress – Taking ZedGraph as an example to add curve progress

scene WinForm control – devexpress18 download installation registration and use in vs: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 When using ZedGraph to add curves, the number of curves is slower if there are many cases. So in the process of adding curve, the progress needs to be displayed, and the effect is as follows     Note: Blog home page:https://blog.csdn.net/badao_liumang_qizhi […]