将HEX字符串设置为MQ Correlation ID

时间:2017-06-07 07:44:44

标签: java ibm-mq

我正在尝试将我的MQ消息的相关ID设置为此十六进制数: correlationID =" 414d51205350434954495f5550493032115bf458a7180410" 它最初作为字符串传递给函数

        MQMessage msg = new MQMessage();
        byte[] corrID = null;
        corrID = Base64.getDecoder().decode(correlationID);
        try 
        {    
            msg.writeString(message);   
            msg.correlationId = corrID;
            queue_con.put(msg,pmo);
            corrID = msg.correlationId;
            System.out.println(corrID);
            System.out.println(Base64.getEncoder().encodeToString(corrID));

        } 

这是48位长。我已经读过CorrelationID字段可以保存24bytes / 48十六进制数字。但是当我尝试设置msg.correlationId字段时,该数字将被截断为仅32位数。你能帮帮我吗?

3 个答案:

答案 0 :(得分:3)

使用JRE17的XML框架部分:

try {
 msg.correlationId = javax.xml.bind.DatatypeConverter.parseHexBinary(correlationID);
} catch (IllegalArgumentException e) {
 msg.correlationId = correlationID.getBytes();
}

答案 1 :(得分:0)

这就是我所做的并且有效:

MQMessage msg = new MQMessage();
        int len = correlationID.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(correlationID.charAt(i), 
16) << 4)
                                 + 
Character.digit(correlationID.charAt(i+1), 16));
        }
        byte[] corrID = data;
        try 
        {
            msg.writeString(message);   
            msg.correlationId = corrID;
            queue_con.put(msg,pmo);
        }

答案 2 :(得分:0)

对于那些试图从纯粹的jms做同样事情的人:

javax.jms.Message message...
message.setJMSCorrelationIDAsBytes(javax.xml.bind.DatatypeConverter.parseHexBinary(new String(value)) );