...
The following Java code will create an embedded broker
Code Block |
BrokerService broker = new BrokerService();
// configure the broker
broker.addConnector("tcp://localhost:61616");
broker.start();
|
If you want to lazily bind the transport connector as part of start(), useful when start() will block pending a store lock (as in a slave start), you can use the following code
Code Block |
BrokerService broker = new BrokerService();
TransportConnector connector = new TransportConnector();
connector.setUri(new URI("tcp://localhost:61616"));
broker.addConnector(connector);
broker.start();
|
...
If you have more than one embedded broker, ensure that you give them a unique name and - e.g.
Code Block |
BrokerService broker = new BrokerService();
// configure the broker
broker.setBrokerName("fred");
broker.addConnector("tcp://localhost:61616");
broker.start();
|
...
It is possible to fully configure a broker through application code e.g.
Code Block |
BrokerService broker = new BrokerService();
broker.setBrokerName("fred");
broker.setUseShutdownHook(false);
//Add plugin
broker.setPlugins(new BrokerPlugin[]{new JaasAuthenticationPlugin()});
//Add a network connection
NetworkConnector connector = answer.addNetworkConnector("static://"+"tcp://somehost:61616");
connector.setDuplex(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
|
...
For more details on the available properties you can specify, see the BrokerService javadoc
Using the BrokerFactory
There is a helper class called BrokerFactory which can be used to create a broker via URI for configuration.
Code Block |
BrokerService broker = BrokerFactory.createBroker(new URI(someURI));
|
...
There is a factory bean that can refer to an external ActiveMQ XML configuration file
Code Block |
<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
<property name="config" value="classpath:org/apache/activemq/xbean/activemq.xml" />
<property name="start" value="true" />
</bean>
|
...
If you are already using XBean then you can just mix and match your Spring/XBean XML configuration with ActiveMQ's configuration.
Code Block |
<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"/>
<broker useJmx="true" xmlns="http://activemq.apache.org/schema/core">
<persistenceFactory>
<kahaDB directory="${basedir}/target" />
</persistenceFactory>
<transportConnectors>
<transportConnector uri="tcp://localhost:61636" />
</transportConnectors>
</broker>
</beans>
|
...
If you are using Spring 2.0 and ActiveMQ 4.1 or later (and xbean-spring 2.5 or later) you can embed the ActiveMQ broker XML inside any regular Spring.xml file without requiring the above factory bean. e.g. here is an example of a regular Spring XML file in Spring 2.0 which also configures a broker.
Code Block |
<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">
<!-- lets create an embedded ActiveMQ Broker -->
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:0" />
</amq:transportConnectors>
</amq:broker>
<!-- ActiveMQ destinations to use -->
<amq:queue id="destination" physicalName="org.apache.activemq.spring.Test.spring.embedded"/>
<!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
<amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost"/>
<!-- Spring JMS Template -->
<bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<!-- lets wrap in a pool to avoid creating a connection per send -->
<bean class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory">
<ref local="jmsFactory" />
</property>
</bean>
</property>
</bean>
<bean id="consumerJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"/>
</bean>
<!-- a sample POJO which uses a Spring JmsTemplate -->
<bean id="producer" class="org.apache.activemq.spring.SpringProducer">
<property name="template">
<ref bean="myJmsTemplate"></ref>
</property>
<property name="destination">
<ref bean="destination" />
</property>
<property name="messageCount">
<value>10</value>
</property>
</bean>
<!-- a sample POJO consumer -->
<bean id="consumer" class="org.apache.activemq.spring.SpringConsumer">
<property name="template" ref="consumerJmsTemplate"/>
<property name="destination" ref="destination"/>
</bean>
</beans>
|
...
An embedded broker can also be created using an ActiveMQConnectionFactory and using a vm connector as a uri. e.g.
Code Block |
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
|
...
You can turn off auto creation by setting the create property on the VM Transport to false:
Code Block |
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");
|