[ActiveMQ] simple use of ActiveMQ

Preface

There are two ways to use activemq, as shown in the following figure

Add jar package

Add the jar package of ActiveMQ all to the project.

queue - point-to-point mode

A producer corresponds to a consumer, and the message produced by the producer disappears after being consumed by the consumer.

Producer code

public void queueProducer() throws Exception{
    //1. To create a connection factory object, you need to specify the ip and port of the service
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.161:61616");
    //2. Create a Connection object using the factory object
    Connection connection = connectionFactory.createConnection();
    //3. To open a Connection, call the start method of the Connection object
    connection.start();
    //4. Create a Session object
    //The first parameter: whether to start transaction or not. Generally, transaction is not opened. If transaction is opened, the second parameter is meaningless
    //The second parameter: response mode, auto response or manual response, general auto response
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //5. Use Session object to create a Destination object in two forms, queue and topic. Now use queue
    Queue queue = session.createQueue("test-queue");
    //6. Create a Producer object using the Session object
    MessageProducer producer = session.createProducer(queue);
    //7. Create a Message object to use TextMessage maliciously
    //The first way
    //TextMessage textMessage = new ActiveMQTextMessage();
    //textMessage.setText("hello activemq");
    //The second way
    TextMessage textMessage = session.createTextMessage("hello activemq");
    //8. Send message
    producer.send(textMessage);
    //9. Close resources
    producer.close();
    session.close();
    connection.close();
}

Consumer code

public void queueConsumer() throws Exception {
    //Create a ConnectionFactory object to connect to the MQ server
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.161:61616");
    //Create a connection object
    Connection connection = connectionFactory.createConnection();
    //Open connection
    connection.start();
    //Create a Session object using the Connection object
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //Create a Destination object, a queue object
    Queue queue = session.createQueue("test-queue");
    //Using Session object to create a consumer object
    MessageConsumer consumer = session.createConsumer(queue);
    //receive messages
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            //Print results
            TextMessage textMessage = (TextMessage) message;
            String text;
            try {
                text = textMessage.getText();
                System.out.println(text);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
    //Waiting to receive message
    System.in.read();
    //close resource
    consumer.close();
    session.close();
    connection.close();
}

topic - Publish / subscribe mode

After a producer generates a message and sends it, it can be received by multiple consumers. If no consumer receives it, the message disappears.

Producer code

public void topicProducer() throws Exception{
    //1. To create a connection factory object, you need to specify the ip and port of the service
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.161:61616");
    //2. Create a Connection object using the factory object
    Connection connection = connectionFactory.createConnection();
    //3. To open a Connection, call the start method of the Connection object
    connection.start();
    //4. Create a Session object
    //The first parameter: whether to start transaction or not. Generally, transaction is not opened. If transaction is opened, the second parameter is meaningless
    //The second parameter: response mode, auto response or manual response, general auto response
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //5. Use Session object to create a Destination object in two forms, queue and topic. Now use topic
    Topic topic = session.createTopic("test-topic");
    //6. Create a Producer object using the Session object
    MessageProducer producer = session.createProducer(topic);
    //7. Create a Message object to use TextMessage maliciously
    //The first way
    //TextMessage textMessage = new ActiveMQTextMessage();
    //textMessage.setText("topic message");
    //The second way
    TextMessage textMessage = session.createTextMessage("topic message");
    //8. Send message
    producer.send(textMessage);
    //9. Close resources
    producer.close();
    session.close();
    connection.close();
}

In fact, the main difference lies in session.createQueue/Topic. The same is true for consumers.

Consumer code

public void topicConsumer() throws Exception {
    //Create a ConnectionFactory object to connect to the MQ server
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.161:61616");
    //Create a connection object
    Connection connection = connectionFactory.createConnection();
    //Open connection
    connection.start();
    //Create a Session object using the Connection object
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //Create a Destination object, topic object
    Topic topic = session.createTopic("test-topic");
    //Using Session object to create a consumer object
    MessageConsumer consumer = session.createConsumer(topic);
    //receive messages
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            //Print results
            TextMessage textMessage = (TextMessage) message;
            String text;
            try {
                text = textMessage.getText();
                System.out.println(text);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
    //Waiting to receive message
    System.in.read();
    //close resource
    consumer.close();
    session.close();
    connection.close();
}

summary

Message publishing in the form of topic can also be persistent, that is to say, after topic sends a message, if no consumer receives it, the message will be persistent. I will not elaborate here.

Tags: Session

Posted on Sun, 09 Feb 2020 10:35:28 -0500 by saiko