Get the Correlation ID from MQ as string

2019-07-18 09:32发布

问题:

I am setting the Correlation ID while sending the message to MQ. And I need to use the CorrelationID from the COA of the message I pushed for the further processing.

I am setting the correlation ID and sending the message to queue using the below code.

MQMessage message = createMQMessage("12345");
message.write("Some message to push".getBytes());
queue.put(message);

private MQMessage createMQMessage(String corrID){

        MQMessage message = new MQMessage();
        message.messageFlags = MQConstants.MQMF_SEGMENTATION_ALLOWED;

        if (ackQueueName != null) {

            message.messageType = MQConstants.MQMT_REQUEST;
            message.replyToQueueManagerName = ackQueueManagerName;
            message.replyToQueueName = ackQueueName;
            message.report = MQConstants.MQRO_COA | MQConstants.MQRO_COD;
            message.correlationId = corrID.getBytes();



        }
        return message;
    }

I am reading the replyQueue from another application to get the COA and extract the correlation ID for further processing.

But the correlation ID is in byte[] format and I used the below method getHexString to get the string. But all I got is 48 digit Hex format of my correlation ID like

414d5120514d41444556202020202020b5ca0d5b13b3bb20

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;

    }

All I need is the approach to convert the 48digit HexString to the Original Correlation ID I set. I tried using the below method to convert, but its giving me the junk data.

public static String hexStringToByteArray(String hex) {
        int l = hex.length();
        byte[] data = new byte[l/2];
        for (int i = 0; i < l; i += 2) {
            data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i+1), 16));
        }
        return new String(data);
    }

回答1:

Your report options are not explicitly set.

MQRO_COPY_MSG_ID_TO_CORREL_ID This is the default action, and indicates that if a report or reply is generated as a result of this message, the MsgId of this message is copied to the CorrelId of the report or reply message.

Because of the default action, you will never receive the initial Correlation ID of your message. As Roger mentioned in his answer, it is bad practice to set the MsgID programmatically. Simply use the following option to have the CorrelID copied to the report message:

MQRO_PASS_CORREL_ID If a report or reply is generated as a result of this message, the CorrelId of this message is copied to the CorrelId of the report or reply message.

message.report = MQConstants.MQRO_COA 
                 | MQConstants.MQRO_COD 
                 | MQConstants.MQRO_PASS_CORREL_ID;


回答2:

I set the message ID myself, now I am getting it fine

Do NOT set the message's messageId yourself. Let the queue manager create a unique messageId for each message that is put to a queue. If you need to pass information along with the message either (1) put it in the message data/payload or (2) add a Message Property (aka Named Property) to the message.

If you create your own messageId then there is a chance of duplicate messageIds which is a very bad thing and goes against Best Practices. IBM MQ Best Practices says to let the queue manager create a unique messageId.



标签: java ibm-mq