ActiveMQ from entry to proficient in graphic explanation

This is a series of special articles about the messaging middleware ActiveMQ, which will cover the initial entry of JMS and ActiveMQ and detailed API use, two classic messaging modes (PTP and Pub / Sub), integration with Spring, ActiveMQ clustering, monitoring and configuration Optimization, etc. Without further ado, let's take a look!


First of all, JMS said earlier, that is, when there was no JMS, many application systems had some defects:

1. Synchronization of communication

After the client initiates the call, it must wait for the server to complete processing and return the result before continuing execution

2. The life cycle coupling of client and server is too high

Both the client process and the server service process must be available. If there is a problem with the server or a network failure, the client will receive an exception

3. Point-to-point communication

A call on the client side can only be sent to a single service object, not one-to-many

From entry to proficient ActiveMQ (1)

JMS

It should be noted that JMS only defines the interface for Java to access the message middleware. In fact, it is in the package javax.jms. You will find that except for exception definitions, other interfaces are all interfaces. We can glance at, such as Message:

From entry to proficient ActiveMQ (1)

Message interface

I think you should find out that JMS only provides the interface, and then implement it by specific middleware. For example, ActiveMQ is a Provider that implements JMS, and Alibaba's RocketMQ (I will introduce it to you in the subsequent topics). These message middlewares conform to the JMS specification. Speaking of norms, it is natural to define some terms:

Provider / MessageProvider: Producer

Consumer / MessageConsumer: Consumer

PTP: Point To Point, point-to-point communication message model

Pub / Sub: Publish / Subscribe, publish and subscribe message model

Queue: Queue, one of the target types, combined with PTP

Topic: Topic, one of the target types, combined with Pub / Sub

ConnectionFactory: connection factory, JMS uses it to create connections

Connnection: JMS Client to JMS Provider connection

Destination: message destination, created by Session

Session: Session, created by Connection, is essentially a thread to send and receive messages, so producers and consumers are created by Session

At first glance, the Session is very core, because many things are created by it, in the following text can be used to further understand these terms.

ActiveMQ QuickStart
ActiveMQ is produced by Apache, a very popular message middleware. It can be said that to master the message middleware, you need to start with ActiveMQ. To master the more powerful RocketMQ, you also need the foundation of ActiveMQ. Official website address: http://activemq.apache.org/ , the latest version is 5.14.4, I will explain the latest version here. This article is mainly the preliminary of ActiveMQ, so I use the windows version temporarily, and later use Linux.

ActiveMQ directory structure

Stored under the bin is the ActiveMQ startup script activemq.bat, pay attention to 32 and 64 bits

Inside the conf is the configuration file, the focus is on activemq.xml, jetty.xml, jetty-realm.properties. The user name and password information is required to log in to the ActiveMQ Web console; what kind of protocol connection and port are used between JMS CLIENT and ActiveMQ can be reflected in the above configuration file.

webapps, note that ActiveMQ comes with Jetty to provide a web control console

ActiveMQ in lib provides us with JAR packages with sub-functions, and of course activemq-all-5.14.4.jar

When there is no problem with the JDK installation, start it directly with activemq.bat and access the web console!

From entry to proficient ActiveMQ (1)

ActiveMQ Start

At this point, ActiveMQ has been started, So easy ~

Where are the user name and password to access the ActiveMQ web console? Where is the port in the URL configured?

From entry to proficient ActiveMQ (1)

username/pwd 4 access web

From entry to proficient ActiveMQ (1)

port 4 web console

Write Code 4 ActiveMQ
is a HelloWorld level example to experience ActiveMQ. Specifically, I will write a producer for sending messages and a consumer for receiving messages. In fact, JMS has a "routine", I will use the producer as an example to explain in detail below.

Step 1: Create a ConnectionFactory

From entry to proficient ActiveMQ (1)

ConnectionFactory

In fact, there is a security risk here, that is, once anyone knows the MQ address, they can connect to access. We can configure the specified user and password in activemq.xml to access ActiveMQ.

About broker_bind_url, the default is tcp: // localhost: 61616, indicating that the TCP protocol is used, port 61616. In fact, ActiveMQ not only supports the TCP protocol, but also other protocols, and has opened multiple ports.

From entry to proficient ActiveMQ (1)

Step 2: Create Connection

From entry to proficient ActiveMQ (1)

Connection

Connection represents the communication link between the application and the message server. After obtaining the connection factory, you can create a Connection.

In fact, ConnectionFactory has overloaded methods:

Connection createConnection(String username,String password)

In other words, we can also specify the user name and password here for verification

Step 3: Create Session

From entry to proficient ActiveMQ (1)

Session

Session, used to send and receive messages, and is single-threaded, transaction-supported. If Session enables transaction support, then Session will save a set of information, either commit to MQ, or roll back these messages. Session can create MessageProducer / MessageConsumer.

Step 4: Create Destination

From entry to proficient ActiveMQ (1)

Destination

The so-called message target is the place where the message is sent and received, either queue or topic.

Step 5: Create MessageProducer

From entry to proficient ActiveMQ (1)

MessageProducer

Step 6: Set the persistence method

From entry to proficient ActiveMQ (1)

Persistence mode settings

Step 7: Define the message object and send

From entry to proficient ActiveMQ (1)

Message

The objects passed between producers and consumers are composed of 3 main parts:

Message header (routing) + message attribute (message selector, introduced later) + message body (5 types of messages in the JMS specification)

From entry to proficient ActiveMQ (1)

Message type

Step 8: Release the connection

From entry to proficient ActiveMQ (1)

release resource

The connection must be closed, and only then will ActiveMQ release resources!

The code of the consumer is very similar to the above, except that it is to create a MessageConsumer to receive it. Pay attention to receive () / receive (long) / receiveNoWait (), which shows that consumers can accept messages in blocking mode and non-blocking mode.

After the program runs, let's take a look at the control console:

From entry to proficient ActiveMQ (1)

ActiveMQ Web Info

Messages Enqueued: indicates how many messages have been produced, recorded as P

Messages Dequeued: indicates how many messages were consumed, recorded as C

Number Of Consumers: indicates how many consumers are waiting to receive messages on the queue

Number Of Pending Messages: indicates how many messages have not been consumed, in fact, indicates the backlog of messages, that is, PC

When talking about the Session
when creating a Session through Connection, you need to set two parameters, one is whether to support transactions, and the other is the signing mode. We focus on the signing mode:

From entry to proficient ActiveMQ (1)

Sign-off mode

What is signing? In layman's terms, after receiving the message, the consumer needs to tell the message server that I have received the message. When the message server receives the receipt, this message will be invalid. Therefore, the signing will have a great impact on the PTP model. If the consumer does not sign for the message after receiving it, then this message will continue to be valid and will likely be consumed by other consumers!

AUTO_ACKNOWLEDGE: indicates automatic signing when the consumer receives the message

CLIENT_ACKNOWLEDGE: indicates that the consumer must manually call the acknowledge () method to sign after receiving the message

DUPS_OK_ACKNOWLEDGE: It doesn't matter whether the sign is signed or not, as long as consumers can tolerate the acceptance of repeated messages, of course, this will reduce the cost of Session

In practice, which signing mode should we adopt? CLIENT_ACKNOWLEDGE, the manual method may be better than the automatic method, because the message is received, it does not mean that the message is successfully processed, assuming that we use the manual signing method, only sign on the premise of successful message processing, then As long as the message processing fails, the message is still valid and will continue to be consumed until it is successfully processed!

Regarding the priority / ttl / deliveryMode
message, there are priority and survival time. When MessageProducer sends, there are multiple overloading methods. Let's take a look:

From entry to proficient ActiveMQ (1)

send

In the above code, when we created the producer, we specified the Destination and set the persistence method. In fact, these can be specified without sending, but by the time of sending. And in actual business development, it is often based on various judgments to decide which Queue to send this message to, so it is often not specified when the MessageProducer is created.

TTL, the survival time of a message, one sentence: the producer produced the message, if the consumer does not come to consume, then how long is the message valid

priority, message priority, 0-9. 0-4 is a normal message, 5-9 is an urgent message, and the default level of the message is 4. Note that message priority is only a theoretical concept, and there is no guarantee that messages with high priority will be consumed by consumers first! In other words, ActiveMQ does not guarantee the order of consumption!

deliveryMode, if not specified, the default is persistent messages. If the loss of messages can be tolerated, then using a non-persistent approach will improve performance and reduce storage overhead.

Guess you like

Origin www.linuxidc.com/Linux/2020-04/162977.htm