In this article, we will see the several ways in which one can configure and start a broker in ActiveMQ. We will first see how to do it programatically in Java. Next, we will see how to configure it in Spring, XBean or using the ActiveMQConnectionFactory.
BrokerService Example
A BrokerService manages the activeMQ broker’s lifecycle. In order to exchange messages, producers and consumers need to connect to the broker. If one wants to connect to the broker over the network then a transport connector is needed. If the client is within the same application as the broker sharing the same JVM then the broker can be connected using Virtual Machine Protocol. Based on the configured transport connector, broker knows how to accept
and listen to connections from clients. In the below example, we add a TCP connector which is listening on port 61616.
BrokerServiceExample:
package com.javarticles.activemq; import org.apache.activemq.broker.BrokerService; public class BrokerServiceExample { public static void main(String[] args) throws Exception { String brokerName = "myBroker"; BrokerService brokerService = new BrokerService(); brokerService.setBrokerName(brokerName); brokerService.addConnector("tcp://localhost:61616"); BrokerUtil.startBrokerSendReceiveMessage(brokerService); } }
- We first create BrokerService
BrokerService brokerService = new BrokerService();
- Set broker name. Broker name is important if we want to start more than one broker in a JVM. Also we want to look up a broker using its name.
brokerService.setBrokerName(brokerName);
- Add a TCP connector
brokerService.addConnector("tcp://localhost:61616");
- Start the broker
brokerService.start();
BrokerUtil:
package com.javarticles.activemq; import javax.jms.Connection; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.BrokerRegistry; import org.apache.activemq.broker.BrokerService; public class BrokerUtil { public static void startBrokerSendReceiveMessage(BrokerService brokerService) throws Exception { brokerService.start(); String brokerName = brokerService.getBrokerName(); System.out.println("Broker " + brokerName + " started? " + brokerService.isStarted()); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "vm://" + brokerService.getBrokerName() + "?create=false"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); try { Queue destination = session.createQueue("Q"); MessageProducer producer = session.createProducer(destination); Message message = session.createTextMessage("Hi!"); producer.send(message); MessageConsumer consumer = session.createConsumer(destination); System.out.println("Message received " + ((TextMessage) consumer.receive()).getText()); } finally { session.close(); connection.close(); BrokerRegistry.getInstance().lookup(brokerName).stop(); } } }
In order to connect to the broker, we need to create a connection factory first.
- If the client is within the same JVM as that of broker then we need to use ‘vm://brokerName’ virtual machine protocol scheme.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "vm://" + brokerService.getBrokerName() + "?create=false");
- Create and start connection
Connection connection = connectionFactory.createConnection(); connection.start();
- Create session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Create queue.
Queue destination = session.createQueue("Q");
- Create producer.
MessageProducer producer = session.createProducer(destination);
- Create and send Message.
Message message = session.createTextMessage("Hi!"); producer.send(message);
- Create consumer and receive message.
MessageConsumer consumer = session.createConsumer(destination); System.out.println("Message received " + ((TextMessage) consumer.receive()).getText());
Output:
INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\KahaDB] INFO | KahaDB is version 6 INFO | Recovering from the journal @1:12582 INFO | Recovery replayed 1 operations from the journal in 0.015 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62000-1480004081886-0:1) is starting INFO | Listening for connections at: tcp://127.0.0.1:61616 INFO | Connector tcp://127.0.0.1:61616 started INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62000-1480004081886-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\KahaDB only has 17712 mb of usable space - resetting to maximum available disk space: 17712 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\tmp_storage only has 17712 mb of usable space - resetting to maximum available 17712 mb. Broker myBroker started? true INFO | Connector vm://myBroker started Message received Hi! INFO | Connector vm://myBroker stopped INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62000-1480004081886-0:1) is shutting down INFO | Connector tcp://127.0.0.1:61616 stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62000-1480004081886-0:1) uptime 1.528 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62000-1480004081886-0:1) is shutdown
BrokerFactory Example using ‘broker’ Scheme
Instead of explicitly instantiating a broker service, one can use BrokerFactory to create a broker. All we need to do is pass the URI configuration. Supported
URI schemes are:
- broker
- xbean
- properties
We will first see example of ‘broker’ scheme.
BrokerUtil:
package com.javarticles.activemq; import javax.jms.Connection; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerRegistry; import org.apache.activemq.broker.BrokerService; public class BrokerUtil { public static void createBrokerSendReceiveMessage(String brokerSchemeUrl, String brokerName) throws Exception { BrokerService brokerService = BrokerFactory .createBroker(brokerSchemeUrl); startBrokerSendReceiveMessage(brokerService); } ... }
The URI passed is ‘broker://(tcp://localhost:61616)?brokerName=myBroker’. It will use the scheme to instantiate the specific BrokerFactory
. It uses the brokerURI to extract the configuration parameters for the broker service. It internally instantiates a BrokerService
and then directly configures the pojo model.
BrokerFactoryExample:
package com.javarticles.activemq; public class BrokerFactoryExample { public static void main(String[] args) throws Exception { String brokerName = "myBroker"; String brokerSchemeUrl = "broker://(tcp://localhost:61616)?brokerName=" + brokerName; BrokerUtil.createBrokerSendReceiveMessage(brokerSchemeUrl, brokerName); } }
Output:
INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\KahaDB] INFO | KahaDB is version 6 INFO | Recovering from the journal @1:15135 INFO | Recovery replayed 1 operations from the journal in 0.013 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62038-1480004226601-0:1) is starting INFO | Listening for connections at: tcp://127.0.0.1:61616 INFO | Connector tcp://127.0.0.1:61616 started INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62038-1480004226601-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\KahaDB only has 17709 mb of usable space - resetting to maximum available disk space: 17709 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\tmp_storage only has 17709 mb of usable space - resetting to maximum available 17709 mb. Broker myBroker started? true INFO | Connector vm://myBroker started Message received Hi! INFO | Connector vm://myBroker stopped INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62038-1480004226601-0:1) is shutting down INFO | Connector tcp://127.0.0.1:61616 stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\activemq-data\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62038-1480004226601-0:1) uptime 1.020 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62038-1480004226601-0:1) is shutdown
BrokerFactory Example using ‘xbean’ Scheme
The example will locate the broker.xml file in the classpath using the xbean: protocol. Base on the xbean scheme used, the specific BrokerFactory
will be instantiated that knows how to locate the broker configuration XML based on the file name passed in. ‘xbean’ tells the broker factory to search for the given XML configuration file in the classpath or somewhere on the file system.
XbeanBrokerFactoryExample:
package com.javarticles.activemq; public class XbeanBrokerFactoryExample { public static void main(String[] args) throws Exception { String brokerName = "myBroker"; String brokerSchemeUrl = "xbean:broker.xml"; BrokerUtil.createBrokerSendReceiveMessage(brokerSchemeUrl, brokerName); } }
Output:
INFO | Refreshing [email protected]: startup date [Thu Nov 24 21:49:21 IST 2016]; root of context hierarchy INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\KahaDB] INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | KahaDB is version 6 INFO | Recovering from the journal @1:5455 INFO | Recovery replayed 1 operations from the journal in 0.011 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62076-1480004362411-0:1) is starting INFO | Listening for connections at: tcp://127.0.0.1:61616 INFO | Connector tcp://localhost:61616 started INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62076-1480004362411-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\KahaDB only has 17709 mb of usable space - resetting to maximum available disk space: 17709 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage only has 17709 mb of usable space - resetting to maximum available 17709 mb. Broker myBroker started? true INFO | Connector vm://myBroker started Message received Hi! INFO | Connector vm://myBroker stopped INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62076-1480004362411-0:1) is shutting down INFO | Connector tcp://localhost:61616 stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62076-1480004362411-0:1) uptime 0.948 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62076-1480004362411-0:1) is shutdown
BrokerFactory Example using ‘properties’ Scheme
In the below example, we use a properties file to configure the broker’s various policies. Scheme starts with ‘properties’. We provide the properties file after the scheme.
PropertiesBrokerFactoryExample:
package com.javarticles.activemq; public class PropertiesBrokerFactoryExample { public static void main(String[] args) throws Exception { String brokerName = "myBroker"; String brokerSchemeUrl = "properties:broker.properties"; BrokerUtil.createBrokerSendReceiveMessage(brokerSchemeUrl, brokerName); } }
Output:
INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\KahaDB] INFO | KahaDB is version 6 INFO | Recovering from the journal @1:7612 INFO | Recovery replayed 1 operations from the journal in 0.014 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62111-1480004496476-0:1) is starting INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62111-1480004496476-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\KahaDB only has 17713 mb of usable space - resetting to maximum available disk space: 17713 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage only has 17713 mb of usable space - resetting to maximum available 17713 mb. Broker myBroker started? true INFO | Connector vm://myBroker started Message received Hi! INFO | Connector vm://myBroker stopped INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62111-1480004496476-0:1) is shutting down INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62111-1480004496476-0:1) uptime 1.014 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-62111-1480004496476-0:1) is shutdown
Using BorkerFactoryBean
Spring provides a factory bean org.apache.activemq.xbean.BrokerFactoryBean
to instantiate ActiveMQ broker.
We can provide all the configuration parameters as properties. It uses property named config
to point to the ActiveMQ XML configuration
file.
If one wants to start the broker as soon as the factory bean is initialized then property named start
should be with value as true.
brokerFactoryBean.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="broker.properties" /> </bean> <bean id="brokerViaFactory" class="org.apache.activemq.xbean.BrokerFactoryBean"> <property name="config" value="classpath:broker.xml" /> </bean> </beans>
BrokerFactoryBeanExample:
package com.javarticles.activemq; import org.apache.activemq.broker.BrokerService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BrokerFactoryBeanExample { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "brokerFactoryBean.xml"); BrokerService broker = (BrokerService) context.getBean("brokerViaFactory"); try { System.out.println("Started? " + broker.isStarted()); } finally { broker.stop(); context.close(); } } }
Output:
INFO | PListStore:[C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\KahaDB] INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | KahaDB is version 6 INFO | Recovering from the journal @1:2879 INFO | Recovery replayed 1 operations from the journal in 0.008 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63195-1479799650350-0:1) is starting INFO | Listening for connections at: tcp://127.0.0.1:61616 INFO | Connector tcp://localhost:61616 started INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63195-1479799650350-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\KahaDB only has 19299 mb of usable space - resetting to maximum available disk space: 19299 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\tmp_storage only has 19299 mb of usable space - resetting to maximum available 19299 mb. Started? true INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63195-1479799650350-0:1) is shutting down INFO | Connector tcp://localhost:61616 stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63195-1479799650350-0:1) uptime 0.890 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63195-1479799650350-0:1) is shutdown
Custom BrokerFactoryBean Example
Since BrokerFactoryBean
is a spring factory bean, further customization can be done simply by extending it and providing a method for initializing or overriding the afterPropertiesSet
to modify the broker XML itself.
customBrokerFactoryBean.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="broker.properties" /> </bean> <bean id="brokerViaFactory" class="com.javarticles.activemq.CustomBrokerFactoryBean" init-method="init"> <property name="config" value="classpath:broker.xml" /> </bean> </beans>
CustomBrokerFactoryBean:
package com.javarticles.activemq; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.xbean.BrokerFactoryBean; public class CustomBrokerFactoryBean extends BrokerFactoryBean { @Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); System.out.println("CustomBrokerFactoryBean.afterPropertiesSet called"); } public void init() throws Exception { System.out.println("CustomBrokerFactoryBean.init called"); BrokerService broker = this.getBroker(); setStart(true); broker.start(); System.out.println("Broker started"); } }
CustomBrokerFactoryBeanExample:
package com.javarticles.activemq; import org.apache.activemq.broker.BrokerService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CustomBrokerFactoryBeanExample { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "customBrokerFactoryBean.xml"); BrokerService broker = (BrokerService) context.getBean("brokerViaFactory"); try { System.out.println("Started? " + broker.isStarted()); } finally { broker.stop(); context.close(); } } }
Output:
INFO | PListStore:[C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\KahaDB] INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | KahaDB is version 6 INFO | Recovering from the journal @1:3473 INFO | Recovery replayed 1 operations from the journal in 0.007 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63350-1479800091594-0:1) is starting INFO | Listening for connections at: tcp://127.0.0.1:61616 INFO | Connector tcp://localhost:61616 started INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63350-1479800091594-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\KahaDB only has 19298 mb of usable space - resetting to maximum available disk space: 19298 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\tmp_storage only has 19298 mb of usable space - resetting to maximum available 19298 mb. CustomBrokerFactoryBean.afterPropertiesSet called CustomBrokerFactoryBean.init called Broker started Started? true INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63350-1479800091594-0:1) is shutting down INFO | Connector tcp://localhost:61616 stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQBrokerFactoryExample\.activemqdata\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63350-1479800091594-0:1) uptime 0.918 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-63350-1479800091594-0:1) is shutdown
Define broker using xbean or custom spring bean
XBean provides the ability to define and use a custom XML syntax that’s much more compact than the standard Spring XML syntax using <bean/>
like the BrokerFactoryBean
approach we have seen above. In the below broker.xml
, we have used explicit elements like <broker>
and <transportConnectors>
.
broker.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="broker.properties" /> </bean> <broker id="myBroker" xmlns="http://activemq.apache.org/schema/core" brokerName="${brokerName}" persistent="${persistent}" useJmx="${useJmx}" dataDirectory="${dataDirectory}"> <transportConnectors> <transportConnector uri="tcp://localhost:61616"/> </transportConnectors> </broker> </beans>
broker.properties:
brokerName=myBroker persistent=true useJmx=true dataDirectory=.\activemqdata
BrokerBeanExample:
package com.javarticles.activemq; public class XbeanBrokerFactoryExample { public static void main(String[] args) throws Exception { String brokerName = "myBroker"; String brokerSchemeUrl = "xbean:broker.xml"; BrokerUtil.createBrokerSendReceiveMessage(brokerSchemeUrl, brokerName); } }
Using custom spring namespace
Spring provides the ability to do define custom elements. ActiveMQ provides a custom XML schema to configure ActiveMQ via the spring configuration file.
spring_broker.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="broker.properties" /> </bean> <amq:broker id="myBroker" xmlns="http://activemq.apache.org/schema/core" brokerName="${brokerName}" persistent="${persistent}" useJmx="${useJmx}" dataDirectory="${dataDirectory}"> <amq:transportConnectors> <amq:transportConnector uri="tcp://localhost:61616"/> </amq:transportConnectors> </amq:broker> </beans>
SpringBrokerExample:
package com.javarticles.activemq; import org.apache.activemq.broker.BrokerService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringBrokerExample { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring_broker.xml"); BrokerService broker = (BrokerService) context.getBean("myBroker"); try { System.out.println("Started? " + broker.isStarted()); } finally { broker.stop(); context.close(); } } }
Output:
INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\KahaDB] INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | KahaDB is version 6 INFO | Recovering from the journal @1:16763 INFO | Recovery replayed 1 operations from the journal in 0.011 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-53955-1480057786253-0:1) is starting INFO | Listening for connections at: tcp://127.0.0.1:61616 INFO | Connector tcp://localhost:61616 started INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-53955-1480057786253-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\KahaDB only has 17490 mb of usable space - resetting to maximum available disk space: 17490 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage only has 17490 mb of usable space - resetting to maximum available 17490 mb. Started? true INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-53955-1480057786253-0:1) is shutting down INFO | Connector tcp://localhost:61616 stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQStartingBrokerExample\.activemqdata\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-53955-1480057786253-0:1) uptime 0.919 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-53955-1480057786253-0:1) is shutdown
Using ActiveMQConnectionFactory
An embedded broker can also be created using an ActiveMQConnectionFactory and using a vm connector as a uri.
connectionFactory.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="broker.properties" /> </bean> <!-- <bean id="localFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://${brokerName}" /> </bean> --> <amq:connectionFactory id="localFactory" brokerURL="vm://${brokerName}"/> </beans>
ActiveMQConnectionFactoryExample:
package com.javarticles.activemq; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.BrokerRegistry; import org.apache.activemq.broker.BrokerService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ActiveMQConnectionFactoryExample { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "connectionFactory.xml"); ActiveMQConnectionFactory connectionFactory = (ActiveMQConnectionFactory) context.getBean("localFactory"); ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection(); connection.start(); BrokerService broker = BrokerRegistry.getInstance().lookup("myBroker"); try { System.out.println("Started? " + broker.isStarted()); } finally { broker.stop(); context.close(); } } }
Output:
INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO | PListStore:[C:\javarticles_ws\ActiveMQBrokerFactoryExample\activemq-data\myBroker\tmp_storage] started INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[C:\javarticles_ws\ActiveMQBrokerFactoryExample\activemq-data\myBroker\KahaDB] INFO | KahaDB is version 6 INFO | Recovering from the journal @1:1097 INFO | Recovery replayed 1 operations from the journal in 0.007 seconds. INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-50874-1479804109622-0:1) is starting INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-50874-1479804109622-0:1) started INFO | For help or more information please see: http://activemq.apache.org WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: C:\javarticles_ws\ActiveMQBrokerFactoryExample\activemq-data\myBroker\KahaDB only has 19298 mb of usable space - resetting to maximum available disk space: 19298 mb WARN | Temporary Store limit is 51200 mb, whilst the temporary data directory: C:\javarticles_ws\ActiveMQBrokerFactoryExample\activemq-data\myBroker\tmp_storage only has 19298 mb of usable space - resetting to maximum available 19298 mb. INFO | Connector vm://myBroker started Started? true INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-50874-1479804109622-0:1) is shutting down INFO | Connector vm://myBroker stopped INFO | PListStore:[C:\javarticles_ws\ActiveMQBrokerFactoryExample\activemq-data\myBroker\tmp_storage] stopped INFO | Stopping async queue tasks INFO | Stopping async topic tasks INFO | Stopped KahaDB INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-50874-1479804109622-0:1) uptime 0.968 seconds INFO | Apache ActiveMQ 5.12.0 (myBroker, ID:INMAA1-L1005-50874-1479804109622-0:1) is shutdown
Download the source code
This was an example about the various ways of starting broker.