TCP communication mechanism of ACTIVEMQ

Source: Internet
Author: User

  

  ActiveMQ supports a variety of communication protocol TCP/UDP , we select the most commonly used TCP to analyze the ActiveMQ communication mechanism. Let's start by defining a concept :
  Customer (client):  the producers and consumers of the news are called customers for ACTIVEMQ.
  Message broker: A consumer who receives a message and sends it to the message after it has been processed.

  

In order to clearly describe the core communication mechanism of ACTIVEMQ, we select 3 sections to illustrate, which are links, closing links, Heartbeat .

One, theClient and activeMQ TCP The initial process of communication is analyzed as follows:
   (1) when ActiveMQ is initialized, the TCP Listening port is opened by the Tcptransportserver class According to the configuration , the customer initiates the action of establishing a link through the port.
(2) Put the received socket into the blocking queue .
(3) Another thread socket handler blocked , monitor whether there is a new socket, if any, take it out.
(4) generate an instance of transportconnection.  The main function of the Transportconnection class is to process the state information of the link and implement the commandvisitor interface to complete the processing of all kinds of messages.
(5) transportconnection uses a message processing chain consisting of multiple transportfilter instances to handle the various messages received and to send corresponding responses. The typical sequence of the chain:

Mutextransport->wireformatnegotiator->inactivitymonitor->tcptransport. The last link in this chain is the tcptransport class, which is where the actual and Client gets and sends the data, and the important method of the class is run ( ) and oneway (), one responsible for reading, one responsible for sending.
(6) The chain is completed, can be used for communication operations.


Second, close the link
ActiveMQ Discover the closure of TCP links, the most critical code in the tcpbufferedinputstream class

int n = in.read (buffer, position, buffer.length-position);


Third, Heartbeat
   in order to better maintain the use of TCP link,ActiveMQ uses the heartbeat mechanism as a way to judge the health of the link between the two sides. ActiveMQ uses a two-way heartbeat, that is, ActiveMQ 's Broker and Client both have mutual heartbeat, but either the specific handling of Broker or Client Heartbeat is exactly the same, all in inactivitymonitor class, which is described in detail below.
   the heartbeat produces two threads " Inactivitymonitor  readcheck " and " Inactivitymonitor writecheck " They're all . timer readcheck Span style= "Font-family:consolas;" >readcheck () true writecheck Span style= "Font-family:consolas;" >writecheck () .

This is a little trick, you can refer to, that is, when the writecheck thread sleeps, any data sent successfully, then the thread is awakened, do not Send the other side by TCP Heartbeat message, This reduces the amount of data transmitted over the network to a certain extent.

TCP communication mechanism of ACTIVEMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.