There are 2 ways of interacting with WMQ from Java.
1. A set of proprietary Websphere MQ classes for Java
2. A set of WMQ classes for JMS
I strongly recommend the latter because -
1. A set of proprietary Websphere MQ classes for Java
2. A set of WMQ classes for JMS
I strongly recommend the latter because -
- then you are not tied in to using WMQ. You can change the message queuing software you are using without having to change your Java code. You may think this is unlikely but I've seen it done.
- you get to use spring JMS which is awesome. It completely removes the need to write any boilerplate code and makes your configuration simple and your code minimal.
Receiving Messages
You can either use a MessageListener or receive message explicitly.
Receiving using a MessageListener
First you need to create a class which extends javax.jms.MessageListener and override the onMessage(Message message) method.
public class AlexandrasMessageListener implements MessageListener {
@Override
public void onMessage(final Message message){
if (message instanceOf TextMessage){
// process your message
}
else {
// throw some kind of error
}
}
}
This needs to be plugged into a jmsContainer. I used org.springframework.jms.listener.DefaultMessageListenerContainer rather than the SimpleMessageListenerContainer. The Default is used more commonly (probably because it allows XA Transactions).
Receive using a JmsTemplate
Create a receiver class which contains a JmsTemplate.
Configure your JmsTemplate in spring and wire it in.
<bean id="alexandrasListener" class="com.alexandra.AlexandrasMessageListener"/>
<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="channel" value="${mq.channel}"/>
<property name="hostName" value="${mq.hostName}"/>
<property name="port" value="${mq.port}"/>
<property name="queueManager" value="${mq.queueManager}"/>
<property name="transportType" value="1"/>
</bean>
<bean id="alexandrasContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="mqConnectionFactory"/>
<property name="destinationName" value="${mq.queue}"/>
<property name="messageListener" ref="alexandrasListener"/>
<property name="concurrentConsumers" value="1"/>
<property name="maxConcurrentConsumers" value="1"/>
<property name="idleTaskExecutionLimit" value="4"/>
<property name="maxMessagesPerTask" value="4"/>
<property name="receiveTimeout" value="5000/>
<property name="recoveryInterval" value="5000/>
</bean>
Receive using a JmsTemplate
Create a receiver class which contains a JmsTemplate.
public class AlexandrasReceiver {
@Autowired
private final JmsTemplate alexandrasTemplate;
public TextMessage receiveMessage(){
final Message message = alexandrasTemplate.receiveMessage();
if (message instanceof TextMessage){
return (TextMessage) message;
}
else {
// do something else
}
}
}
Configure your JmsTemplate in spring and wire it in.
<bean id="alexandrasTemplate" class="org.springframework.jms.core.JmsTemplate"
p:defaultDestinationName="${mq.queue}" p:receiveTimeout="2000">
<property name="connectionFactory" ref="mqConnectionFactory"/>
</bean>
Yes this is the excellent article and I have read it several times to understand every piece of it better
ReplyDeletewebsphere Online Training