wiki:VirtualHostingActiveMQ

Virtual Hosting ActiveMQ

OK, so virtual hosting is a bit strong of a term, but here is a strategy to use a single ActiveMQ broker to be used by multiple instances of system installation. By system installation I mean a set of producers and consumers that should essentially be isolated.

The way I propose to achieve this is to template the names of the topics and queues, in a way that is easily configurable. This is a Spring+ActiveMQ+Servlet solution.

Step 1

Setup a context parameter that will contain the ActiveMQ virtual host name

<context-param>
  <param-name>activemq.vh</param-name>
  <param-value>default</param-value>
</context-param>

Step 2

Setup the ActiveMQ queues in spring to use the

<beans>
...
<!-- get the property placeholders from the context params -->
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" />

        <bean id="newEntityTopic" class="org.apache.activemq.command.ActiveMQTopic"
                autowire="constructor">
                <constructor-arg>
                        <value>myapp.${activemq.vh}.NewEntity</value>
                </constructor-arg>
        </bean>
</beans>

Step 3

Override the context param in an actual deploying, using the context.xml file for the given webapp.

<Context>

  <Parameter name="activemq.vh" value="install1" override="false" />
</Context>

Conclusion

Now you can dynamically set the topics/queues used by individual webapps. A similar approach could be used for non-servlet applications by using one of the other property placeholder processors.