anshu
anshu

Reputation: 31

MQHeaderList size is 0, but reading a specific MQRFH2 works

I am wring custom java code to read messages from Websphere MQ (version 8) and read all the headers from the MQ message.

When I use the MQHeaderList to parse all the headers the list size is 0:

MQMessage message = new MQMessage();
queue.get(message, getOptions);
DataInput in = new DataInputStream (new ByteArrayInputStream (b));
MQHeaderList headersfoundlist = null;
headersfoundlist = new MQHeaderList (in);
System.out.println("headersfoundlist size: " + headersfoundlist.size());

However, I read only a specific MQRFH2 it works

MQMessage message = new MQMessage();
queue.get(message, getOptions);
DataInput in = new DataInputStream (new ByteArrayInputStream (b));
MQRFH2 rfh2 = new MQRFH2(in);
Element usrfolder = rfh2.getFolder("usr", false);
System.out.println("usr folder" + usrfolder);

How can I parse all the headers of the MQ Message?

Upvotes: 3

Views: 441

Answers (2)

anshu
anshu

Reputation: 31

I found the mistake in my code. I have a few more steps before reading the headers. It was moving the cursor in message buffer to the end. I added message.setDataOffset(0); before reading headers and it worked.

Upvotes: 0

Roger
Roger

Reputation: 7476

DataInput in = new DataInputStream (new ByteArrayInputStream (b));

What's that about? Not sure why you want to do that.

It should just be:

MQMessage message = new MQMessage();
queue.get(message, getOptions);
MQHeaderList headersfoundlist = new MQHeaderList(message);
System.out.println("headersfoundlist size: " + headersfoundlist.size());

Read more here.


Update:

@anshu's comment about it not working, well, I've always found MQHeaderList class to be very buggy. Hence, that is why I don't use it.

Also, 99.99% messages in MQ will only ever have 1 embedded MQ header (i.e. MQRFH2). Note: A JMS message == MQRFH2 message. The only case where you will find 2 embedded MQ headers are for messages on the Dead Letter Queue.

i.e.

{MQDLH}{MQRFH2}{message payload}

Is there a real need for your application to process multiple embedded MQ headers? Is your application putting/getting JMS messages (aka MQRFH2 messages)?

If so then you should do something like the following:

queue.get(receiveMsg, gmo);

if (CMQC.MQFMT_RF_HEADER_2.equals(receiveMsg.format))
{
   receiveMsg.seek(0);
   MQRFH2 rfh2 = new MQRFH2(receiveMsg);

   int strucLen = rfh2.getStrucLength();
   int encoding = rfh2.getEncoding();
   int CCSID    = rfh2.getCodedCharSetId();
   String format= rfh2.getFormat();
   int flags    = rfh2.getFlags();
   int nameValueCCSID = rfh2.getNameValueCCSID();

   String[] folderStrings = rfh2.getFolderStrings();
   for (String folder : folderStrings)
      System.out.println.logger("Folder: "+folder);

   if (CMQC.MQFMT_STRING.equals(format))
   {
      String msgStr = receiveMsg.readStringOfByteLength(receiveMsg.getDataLength());
      System.out.println.logger("Data: "+msgStr);
   }
   else if (CMQC.MQFMT_NONE.equals(format))
   {
      byte[] b = new byte[receiveMsg.getDataLength()];
      receiveMsg.readFully(b);
      System.out.println.logger("Data: "+new String(b));
   }
}
else if ( (CMQC.MQFMT_STRING.equals(receiveMsg.format)) ||
          (CMQC.MQFMT_NONE.equals(receiveMsg.format))    )
{
   Enumeration<String> props = receiveMsg.getPropertyNames("%");
   if (props != null)
   {
      System.out.println.logger("Named Properties:");
      while (props.hasMoreElements())
      {
         String propName = props.nextElement();
         Object o = receiveMsg.getObjectProperty(propName);
         System.out.println.logger("   Name="+propName+" : Value="+o);
      }
   }

   if (CMQC.MQFMT_STRING.equals(receiveMsg.format))
   {
      String msgStr = receiveMsg.readStringOfByteLength(receiveMsg.getMessageLength());
      System.out.println.logger("Data: "+msgStr);
   }
   else
   {
      byte[] b = new byte[receiveMsg.getMessageLength()];
      receiveMsg.readFully(b);
      System.out.println.logger("Data: "+new String(b));
   }
}
else
{
   byte[] b = new byte[receiveMsg.getMessageLength()];
   receiveMsg.readFully(b);
   System.out.println.logger("Data: "+new String(b));
}

Upvotes: 2

Related Questions