In my previous article IBM Websphere MQ manipulations – part I, I provided an example on how to put and get messages into the queue. What we were doing was essentially performing a stateless transfer of information between systems. The data itself is important but it is not related to any of the other data transferred and thus perhaps one of the simplest middleware examples.
IBM Websphere has undoubtedly changed over time, in version 7 they have added properties and tried to make it more compatible with Java Message Service API. They also added some additional classes to make it easier to add or manipulate headers for the messages you are sending.
MQ Version 7
A message with a RFH2 header
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;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.MQHeaderIterator;
import com.ibm.mq.headers.MQHeaderList;
import com.ibm.mq.headers.MQRFH2;
public class myMQ7 {
String mqHostName;
String mqQueueManagerName;
String mqQueueChannel;
String mqQueueName;
int mqQueuePort;
String filenametoput;
String inputpath;
private MQQueueManager mqQueueManager; // for QMGR object
private MQQueue mqQueue; // for Queue object
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 putHeaderMessage(String args[])
{
init(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));
connect();
open();
putMessage("Aw, the poor puddy tat!");
close();
disconnect();
}
public void getHeaderMessage(String args[])
{
init(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));
connect();
open();
getMessage("favcolor");
close();
disconnect();
}
private void putMessage(String messageTextToSend)
{
try {
// create message
MQMessage mqm = new MQMessage();
//mqm.format = MQC.MQFMT_STRING; // if you do this, your header becomes part of the message
mqm.format = MQC.MQFMT_RF_HEADER_2; // if you do this, your header is known to websphere and
// you can get properties off message later.
//
// do our header stuff
//
MQHeaderList list = new MQHeaderList ();
// create a header type MQRFH2
MQRFH2 myrfh2 = new MQRFH2();
// add the codebase value to the user folder
// yes, it is case sensitive.
try {
myrfh2.setFieldValue("usr", "favcolor", "yellow");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Add the header to the list of headers (the MQRFH2 I have created)
list.add(myrfh2);
// Add all headers on the list to the message
// in this case only one(the MQRFH2 I have created)
try {
list.write(mqm);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//
// read in our file and add to message
//
byte[] bytearray = messageTextToSend.getBytes();
mqm.write(bytearray);
// send it out.
//dumpMessage(mqm);
mqQueue.put(mqm);
System.out.println("Message sent");
}
catch (MQException ex)
{
displayException(ex,"sending header message");
System.exit(1);
}
catch (IOException ex)
{
System.out.println("sending message, write byte array error");
System.exit(1);
}
}
private String getMessage(String propertyname)
{
String returnMessage = "";
String codeLocation = "";
String propertyvalue = "";
try {
MQMessage mqm = new MQMessage();
// read the message
mqQueue.get(mqm);
codeLocation = "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];
MQHeaderIterator iter = new MQHeaderIterator(mqm);
try {
codeLocation = "skip headers";
iter.skipHeaders();
// this is the actual message length without
// headers and stuff.
codeLocation = "get data length";
binMessage = new byte[mqm.getDataLength()];
}
catch (MQDataException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
codeLocation = "do readFully";
mqm.readFully(binMessage);
// see if our property exists on the message
try {
propertyvalue = (String)mqm.getObjectProperty(propertyname);
}
catch (MQException mqExp)
{
if (mqExp.reasonCode == 2471)
{
// well, guess that property didn't really exist, not that serious of an error
propertyvalue = "not found";
}
else
{
mqExp.printStackTrace();
}
}
MQHeaderList headersfoundlist = null;
try {
headersfoundlist = new MQHeaderList (mqm);
System.out.println("headers found in list = " + headersfoundlist.size());
System.out.println("headers list empty? " + headersfoundlist.isEmpty());
if (headersfoundlist.size() != 0)
{
propertyvalue = "no property found";
int idx = headersfoundlist.indexOf("MQRFH2");
MQRFH2 rfh = (MQRFH2) headersfoundlist.get(idx);
try {
propertyvalue = (String)rfh.getFieldValue("usr",propertyname);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (MQDataException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
String foundMessage = new String(binMessage);
System.out.println("message='" + foundMessage + "'");
System.out.println("property name=" + propertyname);
System.out.println("property value=" + propertyvalue);
}
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 " + codeLocation);
System.exit(1);
}
return returnMessage;
}
public static void main(String[] args)
{
myMQ7 myputter;
myputter = new myMQ7();
myputter.putHeaderMessage(args);
myputter.getHeaderMessage(args);
}
}