Following is the example of JMS Queue listener using spring to initialize context.
Queue Listener
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.xml.messaging.JAXMException;
import org.springframework.jms.core.JmsTemplate;
public class QueueListener {
JmsTemplate jmsTemplate;
Destination destination;
public WorkQueueListener(JmsTemplate jmsTemplate,
Destination destination) {
this.jmsTemplate = jmsTemplate;
this.destination = destination;
}
private String receive(String msgSelector, long receiveTimeOut) throws JAXMException, JMSException {
jmsTemplate.setReceiveTimeout(receiveTimeOut);
Message msg = jmsTemplate.receiveSelected(destination, msgSelector);
if (msg != null) {
return unloadMessage(msg);
} else {
return null;
}
}
/**
* Transforms the JMS Message into a SOAP Message
*/
private String unloadMessage(Message msg) throws JAXMException, JMSException {
TextMessage txt = null;
if( msg instanceof TextMessage) {
txt = (TextMessage) msg;
return txt.getText();
}
return null;
}
}
Queue Listener
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.xml.messaging.JAXMException;
import org.springframework.jms.core.JmsTemplate;
public class QueueListener {
JmsTemplate jmsTemplate;
Destination destination;
public WorkQueueListener(JmsTemplate jmsTemplate,
Destination destination) {
this.jmsTemplate = jmsTemplate;
this.destination = destination;
}
private String receive(String msgSelector, long receiveTimeOut) throws JAXMException, JMSException {
jmsTemplate.setReceiveTimeout(receiveTimeOut);
Message msg = jmsTemplate.receiveSelected(destination, msgSelector);
if (msg != null) {
return unloadMessage(msg);
} else {
return null;
}
}
/**
* Transforms the JMS Message into a SOAP Message
*/
private String unloadMessage(Message msg) throws JAXMException, JMSException {
TextMessage txt = null;
if( msg instanceof TextMessage) {
txt = (TextMessage) msg;
return txt.getText();
}
return null;
}
}
AppContext.xml
<bean id="messageConsumer" class="*******.QueueListener">
<constructor-arg index="0" ref="injmsTemplate" />
<constructor-arg index="1" ref="queue" />
</bean>
<bean id="injmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="fungusUserCredentialsConnectionFactory" />
<property name="defaultDestination" ref="queue" />
</bean>
<bean id="fungusUserCredentialsConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory"><ref bean="fungusJConnectionFactory"/></property>
<property name="username" value="JavafungusUser"/>
<property name="password" value="Javafunguspwd"/>
</bean>
<!-- Here we are using TIBCO Queue-->
<bean id="JConnectionFactory" class="****Third party Connection Factory******.TibjmsConnectionFactory">
//use SSL or TCP connection depending upon project requirement
</bean>
<bean id="queue" class="com.tibco.tibjms.TibjmsQueue">
<constructor-arg value="Queue Name here" />
</bean>
<bean id="queue" class="com.tibco.tibjms.TibjmsQueue">
<constructor-arg value="Queue Name here" />
</bean>
No comments:
Post a Comment