Assuming that you have already installed IBM Websphere MQ version 7.5 or above in your machine, this tutorial will help you create simple consumer and producer app that uses Websphere MQ. The language used is java.
To create a Queue manager in windows, open the command prompt as the administrator and type the following command.
crtmqm QMGR1
This would create a queue manager with the name QMGR1.
Now to display a list of queue managers that exist on the machine and their status:
dspmq
You can start the queue manager thus created:
strmqm QMGR1
To End (stop) a queue manager:
endmqm QMGR1
To start the interactive console for MQSC commands (which enable you to perform administration tasks):
runmqsc QMGR1
To create a queue run the interactive console for MQSC commands then:
define qlocal(MQ.REQUEST)
The above command will create a local queue named MQ.REQUEST.
Once you have created a queue manager and a queue and started the queue manager, without further ado let’s see how to create the Producer:
import com.ibm.mq.MQException;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import java.io.IOException;
/**
* This class is an example of JMS Client. Using sendMsg() method you can send a message to the IBM MQ
*
* @author Riyafa Abdul Hameed
* @version 1.0
*/
public class SimpleJMSProducer
{
public void sendMsg(String msg)
{
MQQueueManager queueManager = null;
MQMessage mqMessage = null;
MQPutMessageOptions myPMO = null;
try
{
// creating queue manager
queueManager = new MQQueueManager("QMGR1");
// creating queue`
MQQueue queue = queueManager.accessQueue("MQ.REQUEST", CMQC.MQOO_OUTPUT);
// configuring message encoding, character set, and format
mqMessage = new MQMessage();
mqMessage.encoding = new Integer("546");
mqMessage.characterSet = new Integer("1208");
mqMessage.format = "MQSTR";
// sending message
mqMessage.writeString(msg);
myPMO = new MQPutMessageOptions();
queue.put(mqMessage, myPMO);
// Closing Queue after putting message
queue.close();
} catch (MQException | NumberFormatException | IOException je)
{
je.printStackTrace(System.err);
} finally
{
if (queueManager != null) try
{
// Disconnecting queue manager
queueManager.disconnect();
} catch (MQException ex) {
ex.printStackTrace(System.err);
}
}
}
public static void main(String[] args)
{
new SimpleJMSProducer().sendMsg("Hello world!!!");
}
}
In order to run this example you need com.ibm.mq.jar and com.ibm.mq.jmqi.jar. You can find these jar files if you go to your websphere location and to java\lib folder. In windows:
C:\Program Files (x86)\IBM\WebSphere MQ\java\lib
You can replace QMGR1 and MQ.REQUEST with the name of the queue manager and queue respectively as you would have created.
Similarly the code for the Consumer:
import com.ibm.mq.MQException;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import java.io.IOException;
/**
* This class is an example of JMS Client. Using receive() method you can receive a message to the IBM MQ if it exists
*
* @author Riyafa Abdul Hameed
* @version 1.0
*/
public class SimpleJMSConsumer
{
public String receiveMsg()
{
MQQueueManager queueManager = null;
MQMessage mqMessage = null;
try
{
// creating queue manager
queueManager = new MQQueueManager("QMGR1");
// creating queue`
MQQueue queue = queueManager.accessQueue("MQ.REQUEST", CMQC.MQOO_INPUT_SHARED);
// configuring message encoding, character set, and format
mqMessage = new MQMessage();
// receiving message
queue.get(mqMessage);
// Closing Queue after putting message
queue.close();
return mqMessage.readStringOfByteLength(mqMessage.getMessageLength());
} catch (MQException | IOException je)
{
je.printStackTrace(System.err);
} finally
{
if (queueManager != null) try
{
// Disconnecting queue manager
queueManager.disconnect();
} catch (Exception e)
{
e.printStackTrace(System.err);
}
}
return "";
}
public static void main(String[] args)
{
System.out.println(new SimpleJMSConsumer().receiveMsg());
}
}
You can find the code on github:
This article is a good start. I think it would be helpful to include steps on how to install MQ Explorer on the server, install IBM MQ in Eclipse (Help > Install New Software > MQExplorerSDK.zip), and to open the IBM MQ Perspective in Eclipse (Window > Perspective > Other Perspective > MQ Explorer), and connect to a remote MQ manager (right-click on Queue Managers and select Add Remote Queue Manager). Also, on the MQ Server, the queue needs to be configured as a remote queue, and not a local queue? Does a server connection channel needs to be set up on the server? These additional details would certainly make your article even better.
LikeLike
Thanks for the comment. Here’s a tutorial on how to install IBM WebSphere MQ on Ubuntu (Linux) : https://riyafa.wordpress.com/2017/12/26/how-to-install-ibm-websphere-mq-on-ubuntu-linux/
LikeLike
I got this error on Linux :
-ksh-4.2$ java -classpath /opt/mqm/java/lib/com.ibm.mq.jmqi.jar:/opt/mqm/java/lib/com.ibm.mq.jar:. SimpleJMSProducer
com.ibm.mq.MQException: MQJE001: Completion Code ‘2’, Reason ‘2495’.
at com.ibm.mq.MQSESSION.(MQSESSION.java:2065)
at com.ibm.mq.MQSESSION.getSession(MQSESSION.java:2105)
at com.ibm.mq.MQManagedConnectionJ11.(MQManagedConnectionJ11.java:208)
at com.ibm.mq.MQBindingsManagedConnectionFactoryJ11._createManagedConnection(MQBindingsManagedConnectionFactoryJ11.java:187)
at com.ibm.mq.MQBindingsManagedConnectionFactoryJ11.createManagedConnection(MQBindingsManagedConnectionFactoryJ11.java:233)
at com.ibm.mq.StoredManagedConnection.(StoredManagedConnection.java:97)
at com.ibm.mq.MQSimpleConnectionManager.allocateConnection(MQSimpleConnectionManager.java:194)
at com.ibm.mq.MQQueueManagerFactory.obtainBaseMQQueueManager(MQQueueManagerFactory.java:868)
at com.ibm.mq.MQQueueManagerFactory.procure(MQQueueManagerFactory.java:816)
at com.ibm.mq.MQQueueManagerFactory.constructQueueManager(MQQueueManagerFactory.java:758)
at com.ibm.mq.MQQueueManagerFactory.createQueueManager(MQQueueManagerFactory.java:200)
at com.ibm.mq.MQQueueManager.(MQQueueManager.java:682)
at SimpleJMSProducer.sendMsg(SimpleJMSProducer.java:27)
at SimpleJMSProducer.main(SimpleJMSProducer.java:66)
Caused by: com.ibm.mq.jmqi.local.LocalMQ$4: CC=2;RC=2495;AMQ8598: Failed to load the WebSphere MQ native JNI library: ‘mqjbnd’.
at com.ibm.mq.jmqi.local.LocalMQ.loadLib(LocalMQ.java:1268)
at com.ibm.mq.jmqi.local.LocalMQ$1.run(LocalMQ.java:309)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.ibm.mq.jmqi.local.LocalMQ.initialise_inner(LocalMQ.java:259)
at com.ibm.mq.jmqi.local.LocalMQ.initialise(LocalMQ.java:221)
at com.ibm.mq.jmqi.local.LocalMQ.(LocalMQ.java:1350)
at com.ibm.mq.jmqi.local.LocalServer.(LocalServer.java:230)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at com.ibm.mq.jmqi.JmqiEnvironment.getInstance(JmqiEnvironment.java:706)
at com.ibm.mq.jmqi.JmqiEnvironment.getMQI(JmqiEnvironment.java:640)
at com.ibm.mq.MQSESSION.(MQSESSION.java:2058)
… 13 more
Caused by: java.lang.UnsatisfiedLinkError: no mqjbnd in java.library.path
LikeLike