+

Search Tips   |   Advanced Search

JMS and the Spring Framework

A Spring Framework application can use the JMSTemplate class to send JMS messages or receive synchronous JMS messages.

The JMSTemplate can locate JMS destinations from their JNDI name that we configure in an application resource reference.

Alternatively, for Spring Framework v2.5 and later, the JMSTemplate can locate JMS destinations through dynamic resolution, which looks up the administrative name of the destination configured in WebSphere Application Server

We use a Spring JndiObjectFactoryBean as a proxy for a ConnectionFactory to ensure that JMS resources are managed correctly. For example:

<bean id="jmsConnectionFactory" 
   class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="java:comp/env/jms/myCF"/>
      <property name="lookupOnStartup" value="false"/>
      <property name="cache" value="true"/>
      <property name="proxyInterface" value="javax.jms.ConnectionFactory"/>
</bean>

The following example shows the configuration of a resource reference for a ConnectionFactory. During application deployment, this resource reference is mapped to a configured, managed Connection Factory stored in the JNDI namespace of the application server. The ConnectionFactory is required to undertake messaging and should be injected into the Spring JMSTemplate object.

<resource-ref>
  <res-ref-name>jms/myCF</res-ref-name>
  <res-type>javax.jms.ConnectionFactory</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

After there is a defined JNDI name for the ConnectionFactory in the application, that JNDI name can be looked up and injected into the JMSTemplate. For example:

<jee:jndi-lookup id="jmsConnectionFactory" jndi-name=" jms/myCF "/>

<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
  <property name="connectionFactory">
    <ref bean="jmsConnectionFactory"/>
  </property>
  <property name="destinationResolver">
    <ref bean="jmsDestResolver"/>
  </property>

  ...
</bean>

<!-- A dynamic resolver --> 
<bean id="jmsDestResolver" 
  class=" org.springframework.jms.support.destination.DynamicDestinationResolver"/>

<!-- A JNDI resolver -->  
<bean id="jmsDestResolver" 
  class=" org.springframework.jms.support.destination.JndiDestinationResolver"/>

At run time, the JMSTemplate object can locate a destination based on its JNDI name that was configured in an application resource reference. Alternatively, the JMSTemplate object can locate a destination using dynamic resolution, based on the administrative name of the destination configured in WAS.

The following example shows how to use JNDI resolution to locate the JMS queue myQueue that is bound to a JNDI reference of jms/myQueue:

jmsTemplate.send("java:comp/env/jms/myQueue", messageCreator);

The following example shows how to use dynamic resolution to locate the JMS queue myQueue that is bound to a JNDI reference of jms/myQueue:

jmsTemplate.send("myQueue", messageCreator);

  • Configure a JMS destination for a third-party non-JCA messaging provider
  • Spring Documentation