Monday, May 14, 2012

Spring - Websphere - JNDI - JMS configuration

Define a JNDI template that other beans will be using for retrieving JNDI objects.
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.provider.url">smqp://localhost:4001/timeout=10000</prop>
<prop key="java.naming.factory.initial">com.swiftmq.jndi.InitialContextFactoryImpl</prop>
</props>
</property>
</bean>

Create a connection factory. I used Topic instead of Queue for my experiment.
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref ="jndiTemplate"/>
<property name="jndiName" value="TopicConnectionFactory"/>
</bean>
Create a Spring specific JMS template that will be used for sending JMS messages. Note that we are providing connection factory and a destination to the template. Destination is the message destination which is in our case named ‘testtopic’.
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="destination"/>
<property name="pubSubDomain" value="true"/>
<property name="deliveryPersistent" value="true"/>
<property name="deliveryMode" value="2"/>
</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"/>
<property name="jndiName" value="testtopic"/>
</bean>
I create a MsgSender class and inject jmsTemplate and destination to it. This class simply sends a number of JMS messages to the provided destination using the template.
<bean id="sender" class="myexp.spring.MsgSender">
<property name="destination" ref="destination"/>
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
After sending the messages, we need to receive it right? :) So here we are defining a Spring specific message listener container and providing a message listener to the container. For each message received, the onMessage method of the message listener will be called. In my experiment I simply printout the message in console.
<bean id="messageListener" class="myexp.spring.ExampleListener"/>
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="messageListener"/>
<property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
</bean>
My message sender is simple, it sends Text Messages a number of times, like this -

jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
String msgText = "Message " + messageIndex;
System.out.println("Publishing - " + msgText);
Message message = session.createTextMessage(msgText);
message.setLongProperty("startTime", System.currentTimeMillis());
return message;
}
});

I an addition to a String message, I am also adding additional property into the message. The listener class must implement javax.jms.MessageListener and implement the following method -

public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
System.out.println("Reading - " + msg.getText());
}

see :

No comments:

Post a Comment