gpt4 book ai didi

java - ActiveMQ队列和消费者

转载 作者:行者123 更新时间:2023-12-01 22:59:10 24 4
gpt4 key购买 nike

我有一个场景,我有超过 4 个客户端,并且我想向所有这些客户端发送单个队列消息。我没有为客户端确认。因此任何人都可以从队列中获取该消息。但情况是我想知道消费该消息的消费者数量。谁能帮我统计一下消费者数量。

下面是我编写的代码。

  public static boolean sendMessage(String messageText)
{
try {
StompConnection connection = new StompConnection();
HashMap<String, String> header = new HashMap<String, String>();
header.put(PERSISTENT, "true");
connection.open(URLhost, port);
connection.connect("", "");
connection.begin("MQClient");
Thread.sleep(100);
connection.send(queuePath, messageText, "MQClient", header);
connection.commit("MQClient");
connection.disconnect();
return true;
} catch (Exception e) {
throw new BasicException(AppLocal.getIntString("ActiveMQ service ERROR"), e);
}
}

public static String receiveMessage() {
try {
StompConnection connection = new StompConnection();
connection.open(URLhost, port);
connection.connect("", "");
connection.subscribe(queuePath, Subscribe.AckModeValues.INDIVIDUAL);
connection.begin("MQClient");
Thread.sleep(1000);//below not a good NO DATA test .. worked by making thread sleep a while
if (connection.getStompSocket().getInputStream().available() > 1)
{
StompFrame message = connection.receive();
connection.commit("MQClient");
connection.disconnect();
return message.getBody();
}
else
return "";

} catch (Exception e) {
e.printStackTrace();
}
return "";
}

最佳答案

如果您正在写入队列,那么只有一个消费者将收到该消息。点对点消息传递的整体目标是只有一个消费者会收到消息。

如果您想发送一条消息并让所有消费者都接收该消息,那么您需要使用主题而不是队列。

关于java - ActiveMQ队列和消费者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577103/

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