Deploy a JMS destination queue with Spring
Abstract
Case: we have to send JMS messages to a third-party server, using Spring. Of course, we have to discriminate production, UAT and developments environments. We can decide to use one Spring configuration file per environment, but it is complex to maintain.
Here is the way I proceded:
Declare the factory
<bean id="myJmsQueueConnectionFactory"> <property name="jndiName" value="my.jms.QueueConnectionFactory"/> <property name="jndiTemplate" ref="myJmsJndiTemplate"/> </bean>
Declare a Jndi Template
<bean id="myJmsJndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> <prop key="java.naming.provider.url"><strong>${my.jms.host}</strong></prop> </props> </property> </bean>
The variable ${my.jms.host}
is used to indicate the actual destination host. The value is given in a property file myConfig.properties
, for instance:
my.jms.host=t3://127.0.0.1:1234
Obviously, each environnement needs its ad hoc property file!
Declare a JMS Template
It gathers the factory and the queue name.
<bean id="myJmsTemplate"> <property name="connectionFactory" ref="myJmsQueueConnectionFactory"/> <property name="defaultDestination" ref="myJmsQueue"/> </bean>
Declare the destination queue
<bean id="myJmsQueue"> <property name="jndiName" value="my.jms.destination.queue.name"/> </bean>
Ensure the property file is in classpath
<bean id="conf"> <property name="locations"> <list> <value>classpath*:myConfig.properties</value> </list> </property> </bean>
Restart WebLogic, deploy your EAR. Now it should work!
Notice: in case you’re not sure the distant host is up or not, you may add this attribute to your beans: lazy-init="true"