IBM Websphere MQ manipulations – part I

Quite some years ago on a project that I was involved with, I was exposed to Websphere MQ.  Our needs were actually very basic to put it mildly.  We were using it as a secure method for transferring not messages but actually for transferring entire data files.  The files were not all that large and the only requirement was that they were sent with certainty.

We did not need to set special message id’s or any special attributes.  The data was almost always the same type and we used a naming convention for the data files.  The files were zipped before they were sent and thus the names and other attributes were preserved.

Management didn’t tell us if this would be difficult or not, they just asked us to get it done.  The Internet proved to be an excellent research tool.  There are quite a few examples for doing some simple queue manipulations here, here and here just to name a few.

What exactly is Websphere MQ

The product emphasizes reliability and robustness of message traffic, and ensures that a message should never be lost when MQ is appropriately configured.  Websphere MQ is a multi-platform product that allows delivery of messages in either a homogeneous or heterogeneous computing environment.  The strength of solution is due to the guaranteed delivery of messages.

Every product either continues to change and transform or it stagnates and becomes irrelevant.  During the evolution of the product, IBM has added to the Java classes that can be used to help lighten the load of using their solution in non-trivial ways.

From release to release IBM most likely does a lot of changes to existing classes or adds new ones to support new functionality.  I don’t have a full overview of all the improvements but the one thing that did come up was the new support for header records.  I will discuss more about that in a subsequent article about MQ.

IBM MQ vs JMS

Websphere MQ is an example of a message oriented middleware (MOM) system.  In this case, it is a middleware which allows for the transmission of message data between systems.  In MQ, the two important pieces that are required are the messages and the queues to store them in.  IBM has decided to add their MOM system the “queue manager” as part of their particular solution, this doesn’t exist by other solutions.

Java Message System (JMS) was created to allow Java programs to access existing message systems.  However, due to network effects, more and more message oriented middleware systems have also implemented JMS specification.  Below is just a small sample of implemented systems accessible via JMS.

  • Apache MQ
  • Open Message Queue, from Oracle
  • OpenJMS, from The OpenJMS Group
  • Solace JMS from Solace Systems
  • SAP NetWeaver Process Integration
  • SonicMQ from Aurea Software
  • SwiftMQ
  • Tervela

A more comprehensive list can be found on the Internet, or at other sites such as wikipedia.

MQ Examples

Below is an example of dealing with messages and  queues. The put example will actually work for either version 6 or version 7 or newer of Websphere.  Considering how simple the actual task is they may work without any changes for some time to come.

MQ Put / Get Example

import java.io.IOException;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class myMQ6 {

	String mqHostName;
	String mqQueueManagerName;
	String mqQueueChannel;
	String mqQueueName;
	int    mqQueuePort;

	private MQQueueManager mqQueueManager; // for QMGR object
	private MQQueue mqQueue; // for Queue object

	public myMQ6() {
	}

	private void displayException(MQException ex, String action)
	{
		System.out.println("Error while " + action);
		System.out.println("QMGR Name : " + mqQueueManagerName);
		System.out.println("Queue Name : " + mqQueueName);
		System.out.println("CC   : " + ex.completionCode);
		System.out.println("RC   : " + ex.reasonCode);
	}
	
	public void init(String host, String managername, String channel, String queuename, int queueport)
	{
		mqHostName 			= host;
		mqQueueManagerName 	= managername;
		mqQueueChannel  	= channel;
		mqQueueName    		= queuename;
		mqQueuePort     	= queueport;

		// validity checking left off.
	}
	public void connect() 
	{ 
		try {
			MQEnvironment.hostname = mqHostName;
			MQEnvironment.channel = mqQueueChannel;
			MQEnvironment.port = mqQueuePort;

			mqQueueManager = new MQQueueManager(mqQueueManagerName);
		} 
		catch (MQException mqExp) 
		{
			displayException(mqExp,"doing queue manager connect");
			System.exit(1);
		}
	}
	public void disconnect()  
	{ 													 
		try {
			mqQueueManager.disconnect();
		} 
		catch (MQException mqExp) 
		{
			displayException(mqExp,"doing queue manager disconnect");
			System.exit(1);
		}
	}
	public void open()
	{
		int openOption = MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_AS_Q_DEF;

		try {
			mqQueue = mqQueueManager.accessQueue(mqQueueName, openOption, null, null, null);
		} 
		catch (MQException e) 		
		{
			displayException(e,"doing queue open");
			System.exit(1);
		}
	}
	public void close()  
	{
		try {
			mqQueue.close();
		} 
		catch (MQException mqExp) 
		{
			displayException(mqExp,"closing queue");
			System.exit(1);
		}
	}

	public void putSimpleMessage(String messageTextToSend) 
	{
		try {
			// create message
			MQMessage mqm = new MQMessage();
			mqm.format = MQC.MQFMT_STRING;

			// the byte array could be contents of a file, but we 
			// will keep this simple.
			byte[] bytearray = messageTextToSend.getBytes();

			mqm.write(bytearray);

			// send it out.
			mqQueue.put(mqm);

			System.out.println("Message sent");
		} 
		catch (MQException mqExp) 
		{
			displayException(mqExp,"sending message");
			System.exit(1);		
		} 
		catch (IOException e) 
		{
			System.out.println("sending message, write byte array error");
			System.exit(1);		
		}
	}

	private void getSimpleMessage()   
	{
		String spot = "";
		try {
			MQMessage mqm = new MQMessage();		

			// read the message
			mqQueue.get(mqm);
			spot = "get message length";
			int mLen = mqm.getMessageLength();
			System.out.println("Got message, all " + mLen + " bytes");

			// get data from the MQMessage object
			byte[] binMessage = new byte[mLen];

			mqm.readFully(binMessage);

			String messagetext = new String(binMessage);
			System.out.println("message='" + messagetext+ "'");
		} 
		catch (MQException mqExp) 
		{
			if (mqExp.reasonCode == 2033)
			{
				// queue empty, not really an error
				System.out.println("no message found");
			}
			else
			{
				displayException(mqExp,"reading from queue");
				System.exit(1);
			}
		} 
		catch (IOException e2) 
		{ 
			System.out.println("IO Exception while " + spot);
			System.exit(1);
		}
	}

	public void putSimple(String args[])
	{
		init(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));
		connect();
		open();
		putSimpleMessage("hello world!");
		close();
		disconnect();
	}

	public void getSimple(String args[])
	{
		init(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));
		connect();
		open();
		getSimpleMessage();
		close();
		disconnect();
	}

	public static void main(String[] args) 
	{
		myMQ6 myputter;
		myputter = new myMQ6();
		myputter.putSimple(args);
		
		myputter.getSimple(args);
	}
}

This was all interesting but extremely basic.  MQ gets more interesting when you can add some sorts of meta data to the messages themselves or start using the various headers that are supported.

The two IBM version 6 libraries that are needed are com.ibm.mq.jar and connect.jar.

I will discuss this further in part II in a few days.

Interesting References

www.scribd.com/doc/6811160/Websphere-MQ-Using-Java

 

This entry was posted in programming, Setup From Scratch and tagged , . Bookmark the permalink.