MQQueue get调用无法从队列中获取消息

时间:2018-08-03 14:39:05

标签: ibm-mq

MQQueue get调用无法从队列2033(07F1)(RC2033)中检索消息:MQRC_NO_MSG_AVAILABLE错误。我正在使用以下代码从队列中获取消息:

获取消息:

    byte[] replyMessageBytes = null;
    try {
        this.replyConnection.open();

        MQQueue replyQueue = this.replyConnection.getQueue(CMQC.MQOO_INPUT_AS_Q_DEF);
        if (null == replyQueue) {
            logger.error("Could not create reply queue.");
            throw new PFMCommunicationException("Could not create reply queue.");
        }

        MQMessage replyMessage = new MQMessage();
        MQGetMessageOptions gmo = new MQGetMessageOptions();
        gmo.resolvedQueueName = replyQueue.getResolvedQName();
        if (timeout.length > 0) {
            try {
                gmo.waitInterval = Integer.valueOf("" + timeout[0]);
            } catch (NumberFormatException e) {
            }
        } else {
            gmo.waitInterval = MQConstants.MQWI_UNLIMITED;
        }
        gmo.options = MQConstants.MQGMO_WAIT;
        if (null != correlationId) {
            gmo.matchOptions = MQConstants.MQMO_MATCH_MSG_ID | MQConstants.MQMO_MATCH_CORREL_ID;
            replyMessage.messageId = correlationId.getBytes();
            replyMessage.correlationId = correlationId.getBytes();
        } else {
            gmo.matchOptions = MQConstants.MQMO_NONE;
            replyMessage.messageId = MQConstants.MQMI_NONE;
            replyMessage.correlationId = MQConstants.MQCI_NONE;
        }

        try {
            replyQueue.get(replyMessage, gmo);
            int length = replyMessage.getMessageLength();
            replyMessageBytes = replyMessage.readStringOfByteLength(length).getBytes();
        } catch (MQException e) {
            logger.error("ERROR on receiving reply: CC=" + e.completionCode + " RC=" + e.reasonCode + " "
                    + e.getMessage());
        } catch (IOException e) {
            logger.error("ERROR on receiving reply.", e);
        }

        if (null == replyMessageBytes) {
            logger.error("No reply received.");
        } else {
            logger.debug("Received message: " + new String(replyMessageBytes));
        }
    } catch (MQException e) {
        logger.error("ERROR:", e);
        throw new PFMCommunicationException(e);
    } catch (PFMConnectionException e) {
        logger.error(e.getMessage());
        throw new PFMCommunicationException(e);
    } finally {
        this.replyConnection.close();
        logger.debug("Closed connection with MQ replies.");
    }

我确认消息在waitInterval过期之前队列中已经存在,并且correlationId也匹配。实际上,当我运行代码而不尝试匹配correlationId时,我就能得到消息。我猜这意味着响应消息出了点问题,或者我在正确创建matchOptions时犯了一个错误。

    gmo.matchOptions = MQConstants.MQMO_NONE;
    replyMessage.messageId = MQConstants.MQMI_NONE;
    replyMessage.correlationId = MQConstants.MQCI_NONE;

这是我的响应消息头的样子:

   <Header Origin="DISPUTE1" Addressee="PSXDX2" Date="20180802" Time="123055" Area="QUERY" Content="RAddr" ID="8af3257cf01a4842bf5eec8d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cms/customerAccountDetails.xsd">

我尝试将CorrelId而不是ID放在响应标头中,但仍然相同。有人可以帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

if (null != correlationId) {
  gmo.matchOptions = MQConstants.MQMO_MATCH_MSG_ID | MQConstants.MQMO_MATCH_CORREL_ID;
  replyMessage.messageId = correlationId.getBytes();
  replyMessage.correlationId = correlationId.getBytes();
}

嗯,那是你的问题。即使有,MQ消息的MsgId和CorrelId字段中的值都相同,这将是极为罕见的。

如果您收到发送的请求的回复消息,并且在MQPUT之后保存了 请求消息的MsgId ,则代码应为:

if (null != correlationId) {
  gmo.matchOptions = MQConstants.MQMO_MATCH_CORREL_ID;
  replyMessage.correlationId = correlationId.getBytes();
}

注意:您不保存请求(出站)消息的CorrelId,而是保存MsgId。处理您的请求的服务器应将传入的MsgId放入回复消息的CorrelId中。因此,这就是为什么您只在CorrelId上匹配答复消息的原因。