0

我们需要在处理有效负载后使用来自请求队列的相同消息回复响应队列。我在尝试将字符串写入传入消息时遇到问题。处理消息被附加到现有的有效负载中。这是我正在使用的代码片段

public void readFromQueue(String reqQueue, String queueMgrName) {
        int depth = 0;
        MQQueueManager queueMgr = null;
        MQQueue queue = null;
        MQHeaderList headerList = null;
        try {
            queueMgr = getQueueManager(); //queue manager is initiated
            queue = getQueue("get", queueMgr); // queue is intiated
            MQGetMessageOptions getOptions = new MQGetMessageOptions(); // Message GET options
            getOptions.options = MQConstants.MQGMO_WAIT + MQConstants.MQGMO_FAIL_IF_QUIESCING + 
                                 MQConstants.MQGMO_COMPLETE_MSG + 
                                 MQConstants.MQGMO_PROPERTIES_FORCE_MQRFH2;
            byte[] b = null;
            while (true) {
                try {
                    message = new MQMessage();
                    queue.get(message, getOptions);
                    headerList = new MQHeaderList(message, true);
                    processMessage(headerList, message);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } 
             }
}

public void processMessage(MQHeaderList headerList, MQMessage message) {
      String receivedRequest = headerList.getBody().toString();
      /* ---- Message processing logic here ---- */
      message.writeString(outMessage);
      writeToQueue(message);
}   
public void writeToQueue (MQMessage putMsg)
            throws MQDataException, ConfigurationException { 
        MQQueueManager queueMgr = null;
        MQQueue queue = null;
        try {
            queueMgr = getQueueManager();
            queue = getQueue("put", queueMgr);
            MQPutMessageOptions pmo = new MQPutMessageOptions();
            queue.put(putMsg, pmo);
            queueMgr.commit();
        }
        catch (Exception mqex) {
            System.out.println(mqex);
        } 
}

有效负载消息示例:

{
   Msg:{
        MsgHeader: {
            headerKey1: headerValue1,
            headerKey2: headerValue2
        },
        MsgBody: {
            bodyKey1: bodyValue1,
            bodyKey2: bodyValue2
        }
}

响应消息中的预期有效负载:

{
   Msg:{
        MsgHeader: {
            headerKey1: headerValue1,
            headerKey2: headerValue2
        },
        MsgBody: {
            newBodyKey1: newBodyValue1,
            newBodyKey2: newBodyValue2,
        }
} 

响应消息中的实际有效负载:(附加到实际有效负载而不是替换它)

{
   Msg:{
        MsgHeader: {
            headerKey1: headerValue1,
            headerKey2: headerValue2
        },
        MsgBody: {
            bodyKey1: bodyValue1,
            bodyKey2: bodyValue2
        }
}
{
   Msg:{
        MsgHeader: {
            headerKey1: headerValue1,
            headerKey2: headerValue2
        },
        MsgBody: {
            newBodyKey1: newBodyValue1,
            newBodyKey2: newBodyValue2,
        }
} 

请帮我解决这个问题

4

1 回答 1

2

我不明白你为什么使用 MQHeaderList 类。您的原始请求消息中有多少个 MQ 内部标头?

此外,您是否尝试将 MQMessage 对象放入 JSON 对象中,因为您的描述非常混乱。

听起来您的请求消息布局是:

{RFH2 header}{mcd folder}{jms folder}{usr folder}{message payload}

并且您希望回复消息是:

{RFH2 header}{mcd folder}{jms folder}{usr folder}{updated message payload}

其中 RFH2 标头、mcd 文件夹、jms 文件夹和 usr 文件夹包含请求消息的原始值。

如果这是您正在尝试做的事情,那么请查看另一篇博客文章,看看它是否与您尝试做的事情相匹配。


更新日期:2020 年 2 月 3 日。

要从请求消息中获取消息负载,只需执行以下操作:

String msgStr = null;
if (CMQC.MQFMT_STRING.equals(rfh2.getFormat()))
{
   msgStr = requestMsg.readStringOfByteLength(requestMsg.getDataLength());
}
else
{
   byte[] b = new byte[requestMsg.getDataLength()];
   requestMsg.readFully(b);
   msgStr = new String(b);
}
于 2020-01-31T18:17:37.273 回答