The ActiveMQ project site, 2004–2007 · independent archive
Independent historical archive. This site preserves the ActiveMQ project pages that were published at activemq.org in 2004–2007. It is not the project’s website and is not affiliated with or endorsed by the Apache Software Foundation. ActiveMQ and Apache are trademarks of the ASF. For current downloads and documentation go to activemq.apache.org.

Archive › Documentation › Using ActiveMQ › How do I embed a Broker inside a Connection

Archived page

How do I embed a Broker inside a Connection

Archived from http://www.activemq.org/site/how-do-i-embed-a-broker-inside-a-connection.html, captured 2006-08-19 · view the capture · Added by James Strachan, last edited by James Strachan on Aug 08, 2006

In many messaging topologies there are JMS Brokers (server side) and a JMS client side. Often it makes sense to deploy a broker within your JVM. This allows you to optimise away a network hop; making the networking of JMS as efficient as pure RMI, but with all the usual JMS features of location independence, reliability, load balancing etc.

In ActiveMQ 4.x

There are various ways to embed a broker in ActiveMQ 4.x depending on if you are using Java, Spring, XBean or using the ActiveMQConnectionFactory .

Using Java code

The following Java code will create an embedded broker

BrokerService broker = new BrokerService();

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

broker.start();

Using Spring

There is a factory bean that can refer to an external ActiveMQ XML configuration file

<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>

In this case the usual Spring 'classpath:org/apache/activemq/xbean/activemq.xml' resource mechanism is being used so that the activemq.xml file would be found on the classpath by looking inside all the directories on the classpath then looking for 'org/apache/activemq/xbean/activemq.xml'. You can of course change this to any value you like. e.g. use classpath:activemq.xml if you just want to drop it in a directory that is in the classpath; like WEB-INF/classes in a web application.

If you wish you can use a URL instead using the file:* or *http: prefixes. For more details see how Spring deals with resources

Using XBean

If you are already using XBean then you can just mix and match your Spring/XBean XML configuration with ActiveMQ's configuration.

<beans>

  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

  <broker useJmx="true" xmlns="http://activemq.org/config/1.0">

    <persistenceAdapter>
      <journaledJDBC journalLogFiles="5" dataDirectory="${basedir}/target/foo" />
      
      <!-- To use a different dataSource, use the following syntax : -->
      <!-- 
      <journaledJDBC journalLogFiles="5" dataDirectory="${basedir}/activemq-data" dataSource="#mysql-ds"/>
       -->
    </persistenceAdapter>

    <transportConnectors>
      <transportConnector uri="tcp://localhost:61636" />
    </transportConnectors>

    <networkConnectors>
      <!-- 
      <networkConnector uri="multicast://default?initialReconnectDelay=100" />
      <networkConnector uri="static://(tcp://localhost:61616)" />
       -->
    </networkConnectors>
  </broker>

  <!-- MySql DataSource Sample Setup -->
  <!-- 
  <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
    <property name="username" value="activemq"/>
    <property name="password" value="activemq"/>
    <property name="poolPreparedStatements" value="true"/>
  </bean>
  -->  
</beans>

Using Spring 2.0

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.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.org/config/1.0"
  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.xsd 
  http://activemq.org/config/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.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>

  <!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
  <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="vm://localhost" />
  </bean>

  <!-- 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>

  <!-- 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 bean="myJmsTemplate"></ref>
    </property>

    <property name="destination">
      <ref bean="destination" />
    </property>
  </bean>

  <!--  TODO lets use the better element... -->
  <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">
    <constructor-arg>
      <value>org.apache.activemq.spring.Test.spring.embedded</value>
    </constructor-arg>
  </bean>
</beans>

Using ActiveMQConnectionFactory

An embedded broker can also be created using an ActiveMQConnectionFactory and using a vm connector as a uri. e.g. ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

Use the query "broker.<property>" to configure the broker.

The broker will be created upon creation of a connection.

In ActiveMQ 3.x

You can embed a JMS Broker inside your JMS client via a simple property on the ActiveMQConnectionFactory class. Just set the useEmbeddedBroker property.

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setUseEmbeddedBroker(true);
Connection connection = factory.createConnection();

If you wish to customize the broker using the Xml Configuration and normal Java properties then you can do the following

Or you could configure the factory above using JNDI.

Using Spring

If you are using Spring then you can deploy an ActiveMQ broker directly into Spring as follows...

<!-- lets deploy an embedded broker in this spring.xml -->
  <bean id="broker" class="org.activemq.spring.BrokerFactoryBean">
    <property name="config" value="classpath:foo/bar/broker.xml"/>
  </bean>

Using Java code

If you want to you could just configure the broker directly in Java code.

BrokerContainer broker = new BrokerContainerImpl("TestLargeMessages");
broker.addConnector("tcp://localhost:61616");
broker.start();

More from the archive