ActiveMQ Notes: Source Code Analysis

This article makes a brief analysis of the startup process of ActiveMQ and the code of several important modules such as BrokerService, TransportConnector and NetworkConnector.

Boot process

If you want to quickly understand the main modules of the system, the best way is to become familiar with the startup process of the system. This article first analyzes the startup process of ActiveMQ.

 

ActiveMQ can be run as a stand-alone Java program. ActiveMQ can also be embedded into other Java programs and run as part of that program.

 

When ActiveMQ runs as a standalone program, the actual execution is org.apache.activemq.console.Main in ${ActiveMQHome}/bin/activemq.jar. The Main class can execute the commands defined by the XXXCommand class in the activemq-console, and call the corresponding XXXCommand class command according to the XXX command passed into the Main class. To start ActiveMQ normally, "start" is used, so the BrokerFactory.createBroker() is called using the StartCommand class to create the BrokerService. And this function finally calls XBeanBrokerFactory.createBroker() to create XBeanBrokerService (a subclass of BrokerService).

 

If ActiveMQ is used as part of other Java applications, the common method is to use the Spring/XBean (XBean is based on Spring to further simplify the configuration required to start the Bean) mechanism. Through this mechanism, the bean of XBeanBrokerService is created using the configuration file based on Spring's XML and the namespace customized by XBean of ActiveMQ.

 

It can be seen that two different ways eventually create the same XBeanBrokerService. The start() function of this class completes the startup of the ActiveMQ system by creating modules such as Broker, TransportConnector, and NetworkConnector.

TransportConnector

TransportConnector is a module used by ActiveMQ to manage the communication and transfer messages between Client and Broker.

 

The Transport interface defines the interface between the Client and the Broker for communication. ActiveMQ provides implementation classes of various Transport interfaces, such as UDP, TCP, HTTP, etc.

 

The TransportServer interface defines the interface between the Server and the Broker for communication. Corresponding to Transport, ActiveMQ provides a variety of implementation classes of the TransportServer interface, such as UDP, TCP, HTTP, etc.

 

ActiveMQ defines the TransportFactory class. By using the abstract factory Pattern, ActiveMQ can create different Transport/TransportServer implementations for different message communication protocols. The specific TransportFactory implementation class is specified by the configuration file defined by "META-INF/services/org/apache/activemq/transport/" in the active-client, active-http and other jar files.

NetworkConnector

NetworkConnector is a module used by ActiveMQ to manage the communication and transfer messages between Brokers and Brokers. ActiveMQ defines several implementation classes of NetworkConnector, and the DiscoveryNetworkConnector class is used by default.

 

DiscoveryAgent is the interface used by DiscoveryNetworkConnector to discover interconnected Brokers. ActiveMQ defines a variety of DiscoveryAgent implementation classes, such as HTTPDiscoveryAgent, SimpleDiscoveryAgent, MulticastDiscoveryAgent and so on. Which one to use is determined by the type of URI defined by <networkConnector>.

 

The interaction of communication and control messages between Broker and Broker is realized through NetworkBridge. When the DiscoveryAgent discovers an interconnected broker, it will notify the DiscoveryNetworkConnector, and the DiscoveryNetworkConnector will create a NetworkBridge to communicate with the newly discovered broker to achieve message exchange. DiscoveryNetworkConnector, DiscoveryAgent and NetworkBridge implement state control through the mechanism of XXXListener.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324639723&siteId=291194637