以字符串格式获取MQ messageId [英] Get MQ messageId in string format

查看:165
本文介绍了以字符串格式获取MQ messageId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IBM的mq库从MQ队列读取消息.现在,我需要检索消息的messageid.现在,它位于消息标题中的名称messageId下.但这将返回一个byte [].现在,我需要将其更改为可读的字符串.

如何将messageId从byte []转换为字符串?

我尝试了两次转换,但都没有效果:

new String(theMessage.messageId)
new String(theMessage.messageId, "UTF-8")
new String(theMessage.messageId, "UTF-16")
theMessage.messageId.toString()

解决方案

MQMD中的messageId表示为24个字节.如果您知道在这些平台上生成的平台,则可以通过将字节转换为生成它们的队列管理器的字符集中的字符来找到它们的某些部分的含义,但不建议您依赖于所传递的任何数据因为我已经看到来自IBM的类似于"MsgId是由MQ以IBM专有格式生成的,并且它随时可能更改.",所以在messageID中将其作为字符数据.

如果您想将它们表示为字符串,则应将它们表示为代表24个字节的48个字符的十六进制字符串.

以下是IBM在技术说明中提供的示例功能getHexString,它将为您执行此转换.您将像这样使用它:

getHexString(theMessage.messageId)


下面的示例函数来自IBM MQ Technote,"如何通过JMS应用程序发出请求并从基础Java API生成答复时,匹配相关ID "

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在知识中心页面底部的""

由队列管理器生成的MsgId由4字节的产品标识符(ASCII或EBCDIC中的AMQ¬或CSQ¬,其中¬表示空白字符)组成,后跟唯一字符串的产品特定实现.在IBM®MQ中,它包含队列管理器名称的前12个字符,以及从系统时钟派生的值.因此,所有可以相互通信的队列管理器必须使用前12个字符不同的名称,以确保消息标识符是唯一的.生成唯一字符串的能力还取决于系统时钟是否不会向后更改.为了消除队列管理器生成的消息标识符与应用程序生成的消息标识符重复的可能性,应用程序必须避免以ASCII或EBCDIC(X'41'至X'49'和X'C1'至X'C9').但是,不会阻止应用程序生成具有这些范围内的初始字符的标识符.

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()

解决方案

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.

这篇关于以字符串格式获取MQ messageId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆