gpt4 book ai didi

java - 如何在不从队列中删除消息的情况下使用java读取MQ消息

转载 作者:行者123 更新时间:2023-11-29 06:51:53 25 4
gpt4 key购买 nike

当我执行下面的代码时,它会在控制台上从 MQ 读取数据,然后从队列中删除所有数据。我不希望在从 MQ 读取时从队列中删除我的数据。我想让它参数化,也想把数据写入excel。谁能帮我解决这个问题。以下是我正在使用的代码。

public class MQReadJava
{
private MQQueueManager _queueManager = null;
public int port = 1416;
public String hostname = "xyz";
public String channel = "SYSTEM.ABC.123";
public String qManager = "ABC.BAW";
public String inputQName = "MYQUEUE";

public MQReadJava()
{
super();
}

private void init(String[] args) throws IllegalArgumentException
{
// Set up MQ environment

MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
}

public static void main(String[] args)throws IllegalArgumentException
{
MQReadJava readQ = new MQReadJava();
try
{
readQ.init(args);
readQ.selectQMgr();
readQ.read();
}

catch (IllegalArgumentException e)
{
System.exit(1);
}
catch (MQException e)
{
System.out.println(e);
System.exit(1);
}
}

private void selectQMgr() throws MQException
{
_queueManager = new MQQueueManager(qManager);
}

private void read() throws MQException
{
int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED;

//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;

MQQueue queue = _queueManager.accessQueue( inputQName,
openOptions,
null, // default q manager
null, // no dynamic q name
null ); // no alternate user id

System.out.println("MQRead is now connected.\n");
int depth = queue.getCurrentDepth();
System.out.println("Current depth: " + depth + "\n");

if (depth == 0)
{
return;
}

MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING +
MQC.MQGMO_CONVERT;

while(true)
{
MQMessage message = new MQMessage();
try
{
queue.get(message, getOptions);
byte[] b = new byte[message.getMessageLength()];
message.readFully(b);
System.out.println(new String(b));
message.clearMessage();
}
catch (IOException e)
{
System.out.println("IOException during GET: " + e.getMessage());
break;
}
catch (MQException e)
{
if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
if (depth > 0)
{
System.out.println("All messages read.");
}
}
else
{
System.out.println("GET Exception: " + e);
}
break;
}
}
queue.close();
_queueManager.disconnect();
}
}

最佳答案

Below is my code that I am using.

:) 您下载了我的 MQRead 程序。如果您不希望它执行破坏性的 MQGET,那么您需要更新代码以进行浏览(请参阅 JoshMc 的评论)。你为什么不下载我的 MQBrowse 程序?最后,请开始阅读 MQ Knowledge Center因为它包含大量适用于 IBM MQ 初学者的信息。


更新:您不应使用 MQEnvironment 类,因为它不是线程安全的。将连接信息放在哈希表中。看这里的例子: Java program to connect WMQ with User Id instead of channel

关于java - 如何在不从队列中删除消息的情况下使用java读取MQ消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45345115/

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