Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MQ messageId in string format

Tags:

java

ibm-mq

I'm using the mq library of IBM to read messages from MQ queues. Now I need to retrieve the messageid of the message. I now it is in the message header under the name messageId. But this returns a byte[]. Now I need to change it to a readable string.

How can I transform the messageId from byte[] to string?

I tried a couple of conversions but non of them works:

new String(theMessage.messageId)
new String(theMessage.messageId, "UTF-8")
new String(theMessage.messageId, "UTF-16")
theMessage.messageId.toString()
like image 578
JimmyD Avatar asked Dec 14 '22 21:12

JimmyD


1 Answers

The messageId in the MQMD is represented as 24 bytes. If you know what platform these were generated on you may be able to find some meaning to portions of them by converting the bytes to characters in the character set of the queue manager where they were produced but is not recommended to rely on any data being conveyed in the messageID as character data since I have seen statements from IBM similar to "MsgId is generated by MQ in an IBM proprietary format and it may change at any time."

If you want represent them as a string you should represent them as a 48 character HEX string representing the 24 bytes.

Below is a sample function getHexString IBM has provided in a Technote that will perform this conversion for you. You would use it like this:

getHexString(theMessage.messageId)

The sample function below is from IBM MQ Technote "How to match correlation id's when request is made via JMS application and reply generated from base Java API"

public static String getHexString(byte[] b) throws Exception {
    String result = "";
    for (int i=0; i < b.length; i++) {
        result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
    }
    return result;
}

IBM Documents the format and uniqueness of the Queue Manager generated message IDs at the bottom of the Knowledge Center page "Reference>Developing applications reference>MQI applications reference>Data types used in the MQI>MQMD - Message descriptor>Fields>MsgId (MQBYTE24)"

A MsgId generated by the queue manager consists of a 4-byte product identifier (AMQ¬ or CSQ¬ in either ASCII or EBCDIC, where ¬ represents a blank character), followed by a product-specific implementation of a unique string. In IBM® MQ this contains the first 12 characters of the queue-manager name, and a value derived from the system clock. All queue managers that can intercommunicate must therefore have names that differ in the first 12 characters, in order to ensure that message identifiers are unique. The ability to generate a unique string also depends on the system clock not being changed backward. To eliminate the possibility of a message identifier generated by the queue manager duplicating one generated by the application, the application must avoid generating identifiers with initial characters in the range A through I in ASCII or EBCDIC (X'41' through X'49' and X'C1' through X'C9'). However, the application is not prevented from generating identifiers with initial characters in these ranges.

like image 119
JoshMc Avatar answered Jan 08 '23 13:01

JoshMc