|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
JMS: Getting Message Payload with RFH2 Header |
« View previous topic :: View next topic » |
Author |
Message
|
bloehle13 |
Posted: Fri Apr 23, 2021 3:48 am Post subject: JMS: Getting Message Payload with RFH2 Header |
|
|
Newbie
Joined: 27 Jan 2021 Posts: 6
|
Our JMS MessageListener listens for MQ Application Activity Trace messages. These come through as ByteMessages, and in the bytes of the message I can see the message payload with the RFH2 headers that were set by the traced message. When I create an MQMessage from this BytesMessage though, the message data is empty. I've attached my code below. How can I get the message data with the RFH2 headers from the MQMessage object?
Code: |
// Convert generic jms Message object to BytesMessage
BytesMessage bytesMsg = (BytesMessage) message;
// Read bytes of BytesMessage
byte[] byteData = null;
byteData = new byte[(int) bytesMsg.getBodyLength()];
bytesMsg.readBytes(byteData);
// Create MQMessage from BytesMessage data
mqMsg = new MQMessage();
mqMsg.write(byteData);
mqMsg.seek(0);
// Print MQMessage data
// MQCFGR grpHeader is instantiated earlier
byte[] b = grpHeader.getParameterValue(MQConstants.MQBACF_MESSAGE_DATA) == null ? new byte[] {} : grpHeader.getBytesParameterValue(MQConstants.MQBACF_MESSAGE_DATA);
System.out.println("Payload bytes from group header length: " + b.length);// prints 0
System.out.println("Payload from group header: " + new String(b)); // prints empty String
|
|
|
Back to top |
|
 |
RogerLacroix |
Posted: Fri Apr 23, 2021 3:15 pm Post subject: Re: JMS: Getting Message Payload with RFH2 Header |
|
|
 Jedi Knight
Joined: 15 May 2001 Posts: 3264 Location: London, ON Canada
|
bloehle13 wrote: |
Code: |
// Convert generic jms Message object to BytesMessage
BytesMessage bytesMsg = (BytesMessage) message;
// Read bytes of BytesMessage
byte[] byteData = null;
byteData = new byte[(int) bytesMsg.getBodyLength()];
bytesMsg.readBytes(byteData);
// Create MQMessage from BytesMessage data
mqMsg = new MQMessage();
mqMsg.write(byteData);
mqMsg.seek(0);
// Print MQMessage data
// MQCFGR grpHeader is instantiated earlier
byte[] b = grpHeader.getParameterValue(MQConstants.MQBACF_MESSAGE_DATA) == null ? new byte[] {} : grpHeader.getBytesParameterValue(MQConstants.MQBACF_MESSAGE_DATA);
System.out.println("Payload bytes from group header length: " + b.length);// prints 0
System.out.println("Payload from group header: " + new String(b)); // prints empty String
|
|
I see that you are setting 'mqMsg' with some data but I don't see anywhere in the code where you are accessing/reading it.
bloehle13 wrote: |
Code: |
byte[] b = grpHeader.getParameterValue(MQConstants.MQBACF_MESSAGE_DATA) == null ? new byte[] {} : grpHeader.getBytesParameterValue(MQConstants.MQBACF_MESSAGE_DATA); |
|
Clearly, that parameter doesn't exist in data. Also, your code would run faster if you coded as follows:
Code: |
byte[] b = grpHeader.getBytesParameterValue(MQConstants.MQBACF_MESSAGE_DATA);
if (null == b)
b = new byte[0]; |
Avoid double 'get' calls. Each time the MQ library has to search for the parameter. Don't do it twice when once can get the job done. Also, my way is a lot easier to read rather than 1 super long line of code.
Regards,
Roger Lacroix
Capitalware Inc. _________________ Capitalware: Transforming tomorrow into today.
Connected to MQ!
Twitter |
|
Back to top |
|
 |
bloehle13 |
Posted: Fri May 07, 2021 11:55 am Post subject: |
|
|
Newbie
Joined: 27 Jan 2021 Posts: 6
|
Thanks for the reply Roger. mqMsg was getting populated by a BytesMessage that comes from an onMessage() MessageDrivenBean.
The solution turned out to be that I had to loop through the PCFParameter parameters looking for MQBACF_MESSAGE_DATA:
Code: |
Enumeration<Object> e = pcfm.getParameters();
byte[] b = null;
while (e.hasMoreElements()) {
Object element = e.nextElement();
if (String.valueOf(element).contains("MQBACF_MESSAGE_DATA")) {
// instantiate b here
}
}
|
The reason I do a get twice for a grpHeader parameter is because directly seeking the bytes parameter value will error out if the parameter does not exist, whereas the generic getParameterValue() method will return null. This is behavior I experienced last year on MQ8. It might be fixed by current versions, so it will be worth looking into again for the performance gains you mentioned. |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|