Spring and ActiveMQ (JMS 1.1)
I had a difficult time getting a Spring JmsTemplate configured and connected to my OpenJMS server. So I ended up writing a document about it. More recently, I've switched from OpenJMS (JMS 1.0.2) to ActiveMQ (JMS 1.1). Below is a sample configuration that should provide a seamless migration from the one in my previous document.
This configuration does not use JNDI, as it's not necessary with ActiveMQ.
Pass the jmsQueueTemplate or jmsTopicTemplate beans into your own objects as a property, you
can use them to send and receive from any ActiveMQ queue or topic.
Spring JmsTemplate Warning
Do not use Spring's JMS Template to read messages from a topic (queues are OK), you will lose messages:
Another gotcha I've seen folks do is to create a MessageConsumer inside one of the SessionCallback methods then wonder why messages are not being received. After the SessionCallback is called, the session will be closed; which will close your consumers too . So if you want to create a MessageConsumer you should create a connection, session and consumer yourself. (BTW it might be nice to make the createConsumer & createSession methods public on JmsTemplate).
(quoted from ActiveMQ's JmsTemplate Gotchas Page)
Application Context XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//Spring//DTD Bean//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Application Context -->
<beans>
<!-- Property Placeholder Post-Processor (Handles ${variables}) -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>classpath:/bootstrap.properties</value></property>
</bean>
<!-- ActiveMQ Pooling JMS Connection Factory -->
<bean id="jmsConnectionFactory" class="org.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory">
<bean class="org.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>${jms.brokerUrl}</value>
</property>
</bean>
</property>
</bean>
<!-- JMS Queue Template -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsConnectionFactory"/>
</property>
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="receiveTimeout">
<value>2000</value>
</property>
</bean>
<!-- JMS Topic Template -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsConnectionFactory"/>
</property>
<property name="pubSubDomain">
<value>true</value>
</property>
</bean>
</beans>
