Version 4

    The Spring JMSTemplate is designed to be used inside a system that provides adequate caching and is not designed to be used on its own.

     

    There are two ways to use the Spring JMSTemplate correctly.

     

    The first is inside the application server using the JCA managed connection factory (normally at /JmsXA) but that only works when you're sending messages.

     

    The JCA managed connection factory caches connections so they won't actually be created each time. However using the JCA managed connection factory won't resolve the issue with consumers since they are not cached.

     

    This is discussed more in this Should I cache JMS connections and JMS sessions?.

     

    The other way is to use the Spring JMSTemplate with Spring's CachingConnectionFactory. This wraps any ConnectionFactory and caches connections, consumers, and producers so it performant for both sending and receiving messages. However, even in this case consuming messages is considered a bad practices since the thread will block waiting for messages. A better option is to use Spring's SimpleMessageListenerContainer or DefaultMessageListenerContainer for asynchronous consuming of messages.

     

    In summary, the Spring JMSTemplate is not safe to use for consuming messages and should only be used to send messages if connections and producers are cached either with Spring's CachingConnectionFactory or with the JCA managed connection factory (/JmsXA).

     

    Please note that this problem is not specific to JBoss Messaging. Check out this ActiveMQ page for example http://activemq.apache.org/jmstemplate-gotchas.html, and here's another warning page from SwiftMQ http://www.swiftmq.com/developers/howto_jms_pooling/index.html

     

    Also exactly the same reasoning applies to JBoss MQ and JBoss Messaging.