ActiveMQ learning note 5 -- the transmission protocol of ActiveMQ

Keywords: Session Apache network xml

1. Introduction

In the previous receiving ActiveMQ and code cases, you have configured:

broker-url: tcp://192.168.106.131:61616

See the default protocol of ActiveMQ for TCP. However, ActiveMQ processing supports TCP thank you. It also supports client broker communication protocols: TCP, NIO, UDP, SSL, HTTP(s), VM.

Please refer to the official website for details: http://activemq.apache.org/configuring-version-5-transports.html

The file location where ActiveMQ configures these protocols: ActiveMQ installation path / config/activemq.xml

In the < transportconnectors > tag, there are some protocols supported by ActiveMQ.

In the above configuration,

The header of uri description information uses the name of the protocol, such as:

When describing the listening port of AMQP protocol, the uri description information used is“ amqp://...... ”,

When describing the listening port of stomp protocol, the uri description information used is“ stomp://...... ”.

However, when describing openwire protocol, uri adopts“ tcp://...... ”.

This is because the default protocol of ActiveMQ is openwire

2. Detailed introduction of various agreements

Transmission Control Protocol

(1) This is the default Broker configuration. The listening port of tcp is 61616;

(2) Before the network transmission of data, the data must be serialized first, and the transmitted messages are sequenced into byte stream through wire protocol;

(3) URI form of Tcp link:

tcp://HostName:port?key=value&key=value

The following parameters are optional.

(4) Advantages of TCP transmission:

- tcp protocol has high reliability and stability;

- transfer by byte stream, high efficiency;

- it is widely used and supports any platform. Message, high availability.

NIO protocol (New I/O API Protocol)

(1) NIO protocol is similar to TCP protocol, but NIO focuses on the underlying access operations. It allows developers to use more client calls and more server-side load for the same resource;

(2) NIO protocol usage scenarios:

When a large number of Client segments are linked to the Broker, the operating system limits the number of threads connected. At this time, the implementation of NIO can require fewer threads than that of TCP, so NIO protocol is recommended.

When the network transmission of Broker is slow, NIO has better performance than TCP;

(3) URI form of NIO connection:

nio://hostname:port?key=value&key=value

AMQP (Advanced Message Queuing Protocol)

AMQP protocol is an application level mark advanced message queuing protocol that provides the proposed message service. It is an open standard of application level protocol and is designed for message oriented middleware.

Yu Xiaoxiao middleware based on this protocol can deliver messages, which is not limited by different products and development languages of client / middleware.

STOMP protocol (streaming text oriented message protocol)

It is a stream oriented message protocol. It is a simple text protocol designed for MOM (Message Oriented Middleware).

Other protocols can be studied through the official website, which is skipped here.

3.NIO agreement cases

ActiveMQ uses the IO model of BIO network by default. NiO's IO model is used only when we specify NiO.

(1) Modify profile activemq.xml

- modify the configuration file and add the following under the < transportconnectors > node:

<transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/>

- restart the activemq service:

./activemq restart

- check the activemq management background, and you can see the new nio protocol:

(2) Code implementation

In fact, the implemented code is consistent with the code implemented through tcp except that the parameter of the transmission protocol has changed.

Specific code:

consumer

package com.jzt.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author sj
 */
public class NIO_QueueProducer {
    public static String MQ_NAME = "admin";

    public static String MQ_PASSWORD = "admin";

    public static String MQ_BROKETURL = "nio://192.168.106.131:61616";

    public static void main(String[] args) throws JMSException {

        //Connection factory
        ConnectionFactory factory;
        // Connection instance
        Connection connection = null;
        //conversation
        Session session = null;
        // Message sending destination address
        Destination destination;
        // Message Creator
        MessageProducer messageProducer = null;

        try {
            factory = new ActiveMQConnectionFactory(MQ_NAME, MQ_PASSWORD, MQ_BROKETURL);
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("nio_queue");
            messageProducer = session.createProducer(destination);
            for (int i = 0; i < 9; i++) {
                TextMessage textMsg = session.createTextMessage("NIO: This is the first" + i + "Messages");
                //textMsg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
                textMsg.setJMSPriority(i);
                messageProducer.send(textMsg);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            messageProducer.close();
            session.close();
            connection.close();
        }
    }
}

Start the producer and you can see that the message was sent successfully:

consumer:

package com.jzt.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author sj
 */
public class NIO_QueueComsumer {
    public static String MQ_NAME = "admin";

    public static String MQ_PASSWORD = "admin";

    public static String MQ_BROKETURL = "nio://192.168.106.131:61616";

    public static void main(String[] args) throws JMSException {

        ConnectionFactory factory;
        Connection connection = null;
        Session session = null;
        Destination destination;
        MessageConsumer messageConsumer = null;

        try {
            factory = new ActiveMQConnectionFactory(MQ_NAME, MQ_PASSWORD, MQ_BROKETURL);
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("nio_queue");
            messageConsumer = session.createConsumer(destination);
            messageConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    if(message != null && message instanceof  TextMessage){
                        TextMessage textMessage = (TextMessage)message;
                        System.out.println("Get to NIO Protocol message:"+textMessage);
                    }
                }
            });
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            messageConsumer.close();
            session.close();
            connection.close();
        }
    }
}

Start the consumer, you can see the message that has been consumed:

4.NIO protocol enhancement case

In addition to NiO, an efficient protocol, ActiveMQ optimizes all protocols to provide enhanced NiO. Through this mode, other protocols can be used to transport the IO model of NiO network. The way to realize the enhanced NIO is to use the auto keyword, which is equivalent to encapsulating all the protocols and exposing only one port, making it more flexible to use. (1) How to use

auto keyword + nio

auto: for all protocols, that is to say, ActiveMQ will automatically identify what protocol we are using;

NiO: IO model using NiO network.

(2) Modify profile activemq.xml

<transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/>
            <transportConnector name="auto+nio" uri="auto+nio://0.0.0.0:61608?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600&amp;org.apache.activemq.transport.nio.SelectorManager.corePoolSize=20&amp;org.apache.activemq.transport.nio.Se1ectorManager.maximumPoo1Size=50"/>
        </transportConnectors>

(3) Code implementation

Producer of tcp protocol using nio model. Other codes are the same as before, except that the agreement needs to be modified as follows:

private static final String ACTIVEMQ_URL = "tcp://118.24.20.3:61608";

In this way, the ActiveMQ server can identify our protocol, and the bottom layer is through the nio model.

Posted by bigger on Sat, 27 Jun 2020 02:27:07 -0700