ActiveMQ (XI) -- how to start Broker

Posted by something on Sat, 04 Jan 2020 07:19:07 +0100

Broker: equivalent to an ActiveMQ server instance

Command line start parameter instance:
1. / ActiveMQ start: use the default activemq.xml to start
2. / ActiveMQ start xbean: File:.. / conf / activemq-2.xml use the specified configuration file to start
3. If file is not specified, that is, xbean:activemq-2.xml, then XML must be under classpath

Using ActiveMQ to build Java applications

Here we mainly talk about using ActiveMQ Broker as an independent message server to build Java applications. ActiveMQ also supports communication in vm based on embedded Broker, which can integrate other Java applications seamlessly.

Embedded Broker startup
1.Broker service starts broker

import org.apache.activemq.broker.BrokerService;

public class InnerBroker {
    public static void main(String[] args) throws Exception {
        BrokerService broker = new BrokerService();
        broker.setUseJmx(true);
        broker.addConnector("tcp://localhost:61616");
        broker.start();
    }
}

2. Broker factory starts broker

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;

import java.net.URI;

public class InnerBroker {
    public static void main(String[] args) throws Exception {
//        BrokerService broker = new BrokerService();
//        broker.setUseJmx(true);
//        broker.addConnector("tcp://localhost:61616");
//        broker.start();

        String uri = "properties:broker.properties";
        BrokerService broker1 = BrokerFactory.createBroker(new URI(uri));
        broker1.addConnector("tcp://localhost:61616");
        broker1.start();

    }
}

Content of broker.properties

useJmx=true
persistent=false
brokerName=Cheese

3. Integrating broker with spring

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <bean id="broker" 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>

</beans>

Startup file

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InnerBroker {
    public static void main(String[] args) throws Exception {
//        BrokerService broker = new BrokerService();
//        broker.setUseJmx(true);
//        broker.addConnector("tcp://localhost:61616");
//        broker.start();


//        String uri = "properties:broker.properties";
//        BrokerService broker1 = BrokerFactory.createBroker(new URI(uri));
//        broker1.addConnector("tcp://localhost:61616");
//        broker1.start();

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");


    }
}

Just load the spring configuration file and the embedded broker starts.

 

Start ActiveMQ:

1. The broker can be started in an encoded way in the application, for example: borker.start();

If you need to start more than one broker, you need to set a name for the broker. For example:

BrokerService broker = new BrokerService();

broker.setName("fred");

broker.addConnector("tcp://localhost:61616");

broker.start();

2. It can also be started through spring.

Topics: xml Spring Java Apache