The introduction

In the actual project development process, ActiveMQ is started by an independent process, but it is not excluded that it needs to be embedded in the program and used together with the business system in the way of co-process. Therefore, in this chapter, we mainly introduce several startup methods of Broker

Introduction to Boot Mode

This section describes the common boot modes

  • Broker: An ActiveMQ server program
  • Regular command startup mode:
  1. Activemq Start starts using the default Activemq.xml
  2. activemq start xbean:file:.. /conf/activemq-2.xml starts with the specified configuration file
  3. If file (that is, XBean: Activemq-2.xml) is not specified, then the corresponding XML must be under the CLASSPath
  • Use ActiveMQ to build Java applications

The main focus here is to build Java applications using ActiveMQ Broker as a stand-alone message server. ActiveMQ also supports communication within the VM based on embedded brokers, enabling seamless integration with other Java applications

Embed ActiveMQ in your Java program

Hard coded start

Start Mode 1(Broker Service starts)

    BrokerService brokerService = new BrokerService();
    brokerService.setUseJmx(true);
    brokerService.addConnector("TCP: / / 127.0.0.1:61616");
    brokerService.start();
    System.out.println("Broker started successfully!");
Copy the code

BrokerFactory Startup 2

String uri = "properties:activemq.properties";
BrokerService brokerService =BrokerFactory.createBroker(new URI(uri));
brokerService.addConnector("TCP: / / 127.0.0.1:61616");
brokerService.start();
System.out.println("Broker started successfully!");
Copy the code

activemq.properties

userJms=true
persistent=false
brokerName=MyBroker
Copy the code

Spring based startup

Integrate Spring startup mode 1

Start with BrokerService via Spring

<bean id="activemqBroker" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop">
    <property name="brokerName" value="MyBroker"/>
    <property name="persistent" value="false"/>
    <property name="transportConnectorURIs">
    <list>
        <value>tcp://localhost:61616</value>
    </list>
    </property>
</bean>
Copy the code

Java code

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    System.out.println("Broker is startd!");
}
Copy the code

Integrate Spring startup mode 2

Start Broker with BrokerFactoryBean via Spring to introduce the factory class in the Bean file

<bean id="activemqBrokderFactory" class="org.apache.activemq.xbean.BrokerFactoryBean">
    <property name="config" value="activemq-properties.xml"/>
    <property name="start" value="true"/>
</bean>
Copy the code

The main contents of activemq-properties.xml are as follows


      
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
    <broker useJmx="false" persistent="false"
    xmlns="http://activemq.apache.org/schema/core">
        <transportConnectors>
            <transportConnector uri="tcp://localhost:61616"/>
        </transportConnectors>
    </broker>
</beans>
Copy the code

Integrate Spring startup mode 3

Start with the AMQ: Broker label configuration

<amq:broker brokerName="MyBroker" start="true" persistent="false">
    <amq:transportConnectors>
        <amq:transportConnector uri="tcp://localhost:61616"/>
    </amq:transportConnectors>
</amq:broker>
Copy the code

Conclusion:

This paper mainly introduces several startup methods of ActiveMQ: 1. Independent process startup and embedded application startup. For embedded application startup, two methods of hard coding startup and integrated Spring framework startup are respectively introduced. BrokerSerivce and Factory startup, the principle is similar, depending on your needs can be appropriate