Stephen Nimmo

3Nov/092

ActiveMQ Clustered and Bulletproof

It's easy to create a clustered instance of ActiveMQ and have your clients connect via multicast. This gives you the ability to create ActiveMQ instances all over the place, have them autodiscover each other and failover seamlessly and automatically. Here's the ActiveMQ configuration.


<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
  		http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

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

	<broker xmlns="http://activemq.apache.org/schema/core" brokerName="MyBroker" dataDirectory="${activemq.base}/data">

		<destinations>
			<queue physicalName="MyQueue" />
		</destinations>

		<transportConnectors>
			<transportConnector name="openwire" uri="tcp://localhost:0" discoveryUri="multicast://default"/>
		</transportConnectors>

		<networkConnectors>
      		<networkConnector uri="multicast://default"/>
    	</networkConnectors>

	    <persistenceAdapter>
	      	<memoryPersistenceAdapter/>
	    </persistenceAdapter>

	</broker>

	<jetty xmlns="http://mortbay.com/schemas/jetty/1.0">
        <connectors>
            <nioConnector port="8161"/>
        </connectors>

        <handlers>
            <webAppContext contextPath="/admin" resourceBase="${activemq.base}/webapps/admin" logUrlOnStart="true"/>
        </handlers>
    </jetty>

</beans>

And here is the Spring code for the client.


<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
 		<property name="brokerURL">
 			<value>discovery:(multicast://default)</value>
 		</property>
 	</bean>

 	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
 		<constructor-arg index="0">
 			<value>MyQueue</value>
 		</constructor-arg>
 	</bean>

 	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
 		<property name="connectionFactory">
 			<bean class="org.springframework.jms.connection.SingleConnectionFactory">
 				<property name="targetConnectionFactory" ref="jmsConnectionFactory" />
 			</bean>
 		</property>
 	</bean>

If you will notice, the ActiveMQ client uses the discovery mechanisms for finding the queue, which allows it to connect to any piece of hardware without actually having to know physical locations. You can replace your multicast://default (which happens to be multicast://239.255.2.3:6155) with whatever multicast address you would like. You could even DNS-it-up and have a url look like multicast://MyBroker.mydomain.com:6166.

Enjoy.

http://activemq.apache.org/multicast-transport-reference.html

Tagged as: 2 Comments