Explain in simple terms a simple HelloWorld instance of JMS-ActiveMQ (3)

 

Article from: http://www.aijava.cn/13513.html

 

In this blog post, we use ActiveMQ to implement a peer-to-peer messaging model for everyone. If your understanding of the peer-to-peer model is relatively low, you can take a look at the introduction of the first blog post.

In fact, JMS is not as tall as imagined. After reading this blog post, you will know what simplicity is. Let’s go directly to the topic below.

development environment

We are using the Windows version of ActiveMQ 5.11.1 Release. The latest version of the official website is ActiveMQ 5.12.0 Release. You can download it yourself. Download address .

It should be noted that during development, the activemq-all-5.11.1.jar package in the decompressed apache-activemq-5.11.1-bin.zip should be added to the classpath. This package contains all jms interface APIs. accomplish.

Build a development environment

  • To build a project 
    , we only need to build a java project, import the jar package, and take a screenshot of the project: 

The point-to-point message model requires only one message producer and message consumer. Let's write the code below.

  • write producer

 

package com.yusj.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;
/**
* The producer (sender) of the message
*
*/
public class JMSProducer {

//default connection username
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;
    //connect
    Connection connection = null;
    //The thread that the session accepts or sends messages
    Session session;
    // 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 the connection through the connection factory
        connection = connectionFactory.createConnection();
        // start the connection
        connection.start();
        //create session
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //Create a message queue named HelloWorld
        destination = session.createQueue("HelloWorld");
        //create message producer
        messageProducer = session.createProducer(destination);
        //send messages
        sendMessage(session, messageProducer);
        session.commit();
    } catch (Exception e) {
        e.printStackTrace ();
    }finally{
        if(connection != null){
            try {
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace ();
            }
        }
    }
}
/**
 * send messages
 * @param session
 * @param messageProducer message producer
 * @throws Exception
 */
public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
    for (int i = 0; i < JMSProducer.SENDNUM; i++) {
        //create a text message
        TextMessage message = session.createTextMessage("ActiveMQ 发送消息" +i);
        System.out.println("Send message: Activemq sends message" + i);
        // Send a message through the message producer
        messageProducer.send(message);
    }
}
}

 

 

- write consumers

 

package com.yusj.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;
/**
* The consumer (receiver) of the message
*
*/
public class JMSConsumer {

private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//Default connection username
private static final String PASSWORD = ActiveMQConnection.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 connection = null;//Connection
    Session session;//The thread that the session accepts or sends messages
    Destination destination;//The destination of the message
    MessageConsumer messageConsumer;//Message consumer
    //Instantiate the connection factory
    connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
    try {
        //Get the connection through the connection factory
        connection = connectionFactory.createConnection();
        // start the connection
        connection.start();
        //create session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //Create a message queue connected to 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("Received message: " + textMessage.getText());
            }else {
                break;
            }
        }
    } catch (JMSException e) {
        e.printStackTrace ();
    }
}
}

 

 

run

  1. First, start ActiveMQ, how to start ActiveMQ, please see the second blog post. Enter: http://localhost:8161/admin/ in the browser and start executing:
  2. Run the sender, eclipse console output, as shown below: 

     
    At this point, let's take a look at the ActiveMQ server, the content of Queues is as follows: 


    We can see that a message queue named HelloWorld has been created, and 10 messages in the queue have not been consumed. We can also view which messages are through Browse, as shown in the following figure: 


    If the messages in these queues are deleted, consumers cannot consume them.

  3. Let's continue to run the consumer, and the eclipse console prints the message as follows: 

     
    At this point, let's take a look at the ActiveMQ server. The content of the Queues is as follows: 


    We can see that the message queue of HelloWorld has changed, there is one more messager, and there are 10 in the queue. The message has been consumed, click Browse to view it, it is already empty. 
    Click Active Consumers, we can see the details of this consumer: 


This is the end of our example. You can click on the content of the ActiveMQ server yourself to become more familiar with ActiveMQ.

Summarize

In this blog post, we implemented a peer-to-peer message model and sent a synchronous message. Is it very simple?

In the following blog post, we will implement an example of ActiveMQ and Spring integration.

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326776762&siteId=291194637