Search code examples
springspring-jmsjmstemplate

Bean definition example for JndiDestinationResolver using jndi look up


I'm working on sending and receiving messages to/from an IBM MQ queue using JmsTemplate. My application is installed on a WebSphere application server 8.0 and, in order to retrieve the connection, I use a jndi lookup.

I have 6 queues from where I need to pick/drop xmls depending on scenarios. Also I have added these queue in WAS. I need help to understand two things:

  1. I should use DynamicDestinationResolver or JndiDestinationResolver?

  2. As per my understanding I should use JndiDestinationResolver; if that is right how I can define that in my context file and refer jndi-lookup for each queue so that I can retrieve it from my code while using jmsTemplate send/receive?

Please see below my application context file:

<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> 
<property name="jndiName" value="jms/CPC.TapQueueConnCPC" /> 
<property name="lookupOnStartup" value="false" /> 
<property name="cache" value="true" /> 
<property name="proxyInterface" value="javax.jms.QueueConnectionFactory" /> 
</bean> 
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="receiveTimeout" value="10000" /> 
<property name="sessionAcknowledgeMode" value="1" /> 
<property name="destinationResolver" ref="jmsDestResolver"/>
</bean>
<bean id="fileTransferServiceImpl" class="org.kp.cpc.service.FileTransferServiceImpl" > 
<constructor-arg name="jmsTemplate" ref="jmsTemplate" />    
</bean>
<bean id="jmsDestResolver" class=" org.springframework.jms.support.destination.JndiDestinationResolver"/>
<jee:jndi-lookup id="drop278" jndi-name="jms/CPC.SEND.AUTHREQ278" />
<jee:jndi-lookup id="drop275" jndi-name="jms/CPC.SEND.AUTHREQ275" />
<jee:jndi-lookup id="recev278" jndi-name="jms/CPC.RECE.AUTHREQ278" />
<jee:jndi-lookup id="recev275" jndi-name="jms/CPC.RECE.AUTHREQ275" />
<jee:jndi-lookup id="preAuthStatus" jndi-name="jms/CPC.RECE.PREAUTH.STSUPD278"/>
<jee:jndi-lookup id="succ278" jndi-name="jms/CPC.RECE.SUCC.AUTHRESP278" />

Solution

  • The whole point of the JndiDestinationResolver is that you don't need to do manual lookups. In other words when using the JndiDestinationResolver you don't need the <jee:jndi-lookup /> as that is al handled by the DestinationResolver.

    The name of the destination would be the JNDI name. So in your JMS code you would use the following.

    jmsTemplate.convertAndSend("jms/CPC.SEND.AUTHREQ278", "Your-Message-Here");
    

    The JndiDestinationResolver will use the name of the destination to do a JNDI lookup.

    If you really want to keep the JNDI names out of your code and want to use the <jee:jndi-lookup /> then use the [BeanFactoryDestinationResolver]. This will use the name of the destination to lookup a bean from the BeanFactory (in this case the ApplicationContext). Your JMS code would then point to the bean name instead of the JNDI name.

    jmsTemplate.convertAndSend("drop278", "Your-Message-Here");
    

    So which one to use depends on your preferences.