gpt4 book ai didi

java - 无法从 MQ 队列检索消息

转载 作者:行者123 更新时间:2023-11-30 03:32:44 25 4
gpt4 key购买 nike

我正在尝试从 MQ 队列检索消息。下面是我正在使用的代码:

    //Fetching Queue details
MQEnvironment.hostname=getMqCall().getMqHost();
MQEnvironment.channel=getMqCall().getMqChannel();
MQEnvironment.port=getMqCall().getMqPort();
MQQueueManager qMgr= new MQQueueManager(getMqCall().getMqQueManager());
MQQueue outputQueue = qMgr.accessQueue(getMqCall().getRespQueue(), MQC.MQOO_INQUIRE | MQC.MQOO_BROWSE | MQC.MQOO_INPUT_AS_Q_DEF);

//Checking for messages in queue
int i=outputQueue.getCurrentDepth();
System.out.println("Number of messages in Assign promo queue:: "+i);

for(int j=0;j<i;j++){
MQMessage sampleResponse = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
outputQueue.get(sampleResponse, gmo);
String msgtext = sampleResponse.readStringOfCharLength(sampleResponse.getMessageLength());

System.out.println("Message read is: "+msgtext);
System.out.println("Response length: "+sampleResponse.getTotalMessageLength());

if(msgtext!=null){

// Some code

}else{

System.out.println("Message not read from response queue");

}

}

//Checking for messages in queue after processing
i=outputQueue.getCurrentDepth();
System.out.println("Number of messages in queue after processing :: "+i);

outputQueue.close();
qMgr.close();
qMgr.disconnect();

尽管 getCurrentDepth() 显示值存在于队列中,但我得到的 msgtext 值为 null。甚至 getTotalMessageLength() 也会返回 null 值。已验证消息存在于该队列中。

之前我使用了readString()方法来代替readStringOfCharLength()。但是,我仍然没有得到响应,因此我检查了 readStringOfCharLength(),因为 readString() 方法已弃用。

我什至尝试过使用readLine()方法,但仍然无法检索消息。

提前致谢。

PS:我通过每 15 分钟运行一次的调度程序运行上面的代码。

最佳答案

永远不要根据队列的当前深度设置循环。不这样做的原因有很多,并且在 IBM 的 MQ 标准文档中进行了记录。

其次,确保捕获 MQ 抛出的 MQException。否则,您将发布消息说“我的消息在哪里”。关于 MQException 最重要的是原因代码。所有 MQ 原因代码均记录在 MQ 知识中心中。为链接添加书签并习惯查找原因代码。

最后,这是一个结构正确的 Java/MQ 程序:

private void testReceive()
{
int openOptions = CMQC.MQOO_INQUIRE + CMQC.MQOO_INPUT_SHARED + CMQC.MQOO_FAIL_IF_QUIESCING;
MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = CMQC.MQGMO_NO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;
boolean getMore = true;
MQQueueManager qMgr = null;
MQQueue queue = null;
MQMessage receiveMsg = null;

try
{
qMgr = new MQQueueManager(qManager);
queue = qMgr.accessQueue(inputQName, openOptions);

while(getMore)
{
try
{
receiveMsg = new MQMessage();
queue.get(receiveMsg, getOptions);
byte[] b = new byte[receiveMsg.getMessageLength()];
receiveMsg.readFully(b);
System.out.println("Message-->" + new String(b));
}
catch (MQException e)
{
if ( (e.completionCode == CMQC.MQCC_WARNING) &&
(e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) )
{
System.out.println("Bottom of the queue reached.");
getMore = false;
}
else
{
System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
getMore = false;
}
}
catch (IOException e)
{
System.out.println("MQRead " +e.getLocalizedMessage());
}
}
}
catch (MQException e)
{
System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
finally
{
try
{
if (queue != null)
queue.close();
}
catch (MQException e)
{
System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
try
{
if (qMgr != null)
qMgr.disconnect();
}
catch (MQException e)
{
System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
}
}

关于java - 无法从 MQ 队列检索消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28645497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com