Hi does anyone know how to create a message listener using IBM MQ? I know how to do it using the JMS spec but I am not sure how to do it for IBM MQ. Any links or pointers are greatly appreciated.
Take a look at IBM Help: Writing WebSphere MQ base Java applications
IBM has an API for interacting with queues. Here's their sample:
import com.ibm.mq.*; // Include the WebSphere MQ classes for Java package
public class MQSample
{
private String qManager = "your_Q_manager"; // define name of queue
// manager to connect to.
private MQQueueManager qMgr; // define a queue manager
// object
public static void main(String args[]) {
new MQSample();
}
public MQSample() {
try {
// Create a connection to the queue manager
qMgr = new MQQueueManager(qManager);
// Set up the options on the queue we wish to open...
// Note. All WebSphere MQ Options are prefixed with MQC in Java.
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF |
MQC.MQOO_OUTPUT ;
// Now specify the queue that we wish to open,
// and the open options...
MQQueue system_default_local_queue =
qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE",
openOptions);
// Define a simple WebSphere MQ message, and write some text in UTF format..
MQMessage hello_world = new MQMessage();
hello_world.writeUTF("Hello World!");
// specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the // defaults,
// same as MQPMO_DEFAULT
// put the message on the queue
system_default_local_queue.put(hello_world,pmo);
// get the message back again...
// First define a WebSphere MQ message buffer to receive the message into..
MQMessage retrievedMessage = new MQMessage();
retrievedMessage.messageId = hello_world.messageId;
// Set the get message options...
MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults
// same as MQGMO_DEFAULT
// get the message off the queue...
system_default_local_queue.get(retrievedMessage, gmo);
// And prove we have the message by displaying the UTF message text
String msgText = retrievedMessage.readUTF();
System.out.println("The message is: " + msgText);
// Close the queue...
system_default_local_queue.close();
// Disconnect from the queue manager
qMgr.disconnect();
}
// If an error has occurred in the above, try to identify what went wrong
// Was it a WebSphere MQ error?
catch (MQException ex)
{
System.out.println("A WebSphere MQ error occurred : Completion code " +
ex.completionCode + " Reason code " + ex.reasonCode);
}
// Was it a Java buffer space error?
catch (java.io.IOException ex)
{
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}
}
} // end of sample
I'm not sure if the IBM jars are located at the base Maven repo. I know in the past I've had to extract them from a local IBM install and put them in an local SVN repo. I'm using the following jars:
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.mq</artifactId>
<version>5.3.00</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.mq.pcf</artifactId>
<version>5.3.00</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.mqbind</artifactId>
<version>5.3.00</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.mqjms</artifactId>
<version>5.3.00</version>
<scope>compile</scope>
</dependency>
Take a look at the sample provided above.
Specifically at the lines
MQGetMessageOptions gmo = new MQGetMessageOptions();
system_default_local_queue.get(retrievedMessage, gmo);
You can configure the get to wait for a specified time before throwing a MQRC_NO_MSG_AVAILABLE exception. Or you can wait forever.
gmo.waitInterval= qTimeout;
gmo.options = MQC.MQGMO_WAIT
So you can create a thread that keeps looking for new messages then passes them off to a handler. The getting and putting do not need to be in the same thread or even application.
I hope this helps answer your question.
Although there is a WMQ Java API as noted by the previous responders, WMQ supports JMS as well so here are some resources to get you started there.
Take a look at this article: IBM WebSphere Developer Technical Journal: Running a standalone Java application on WebSphere MQ V6.0 http://bit.ly/c6jlIW
Also, if you have installed the full WMQ client and not just grabbed the jars then you will have lots of sample code installed. By default, these will live in C:\Program Files\IBM\WebSphere MQ\tools\jms or /opt/mqm/samp depending on your platform.
If you need the WMQ Client install media, go here: http://bit.ly/bXM0q3 Note that this is the WMQ v7 client and not the v6 client. It is compatible with the v6 QMgr but since v6 is end-of-life as of September 2011 you should be doing new development on the v7 client and, if possible, a v7 QMgr. There are a lot of functional and performance enhancements available if both sides are v7.
If you need the product manual, go here: http://bit.ly/WMQUsingJava
Finally, please be sure when you get a JMS exception to print the linked exception. This is not a WMQ thing, rather it's a JMS thing. Sun provided a multi-level data structure for JMS exceptions and the really interesting parts are often in the nested level. This is not a big deal and can be implemented in a few lines:
try {
.
. code that might throw a JMSException
.
} catch (JMSException je) {
System.err.println("caught "+je);
Exception e = je.getLinkedException();
if (e != null) {
System.err.println("linked exception: "+e);
} else {
System.err.println("No linked exception found.");
}
}
This helps to determine the difference between a JMS error versus a transport error. For example a JMS security error might be a WMQ 2035, or it might be the JSSE configuration, or the app might not have access to something in the file system. Only one of these is worth spending a lot of time digging through the WMQ error logs for and only by printing the linked exception will you be able to tell if it's that one.
An important point in addition to the existing answers: JMS provides MessageListener
, a class that allows you to receive messages as asynchronous callbacks.
The native API has no equivalent feature! You have to repeatedly call get(...)
as appropriate.