gpt4 book ai didi

jms - ActiveMQ - 是否可以在 CLIENT_ACKNOWLEDGE 模式下确认单个消息

转载 作者:行者123 更新时间:2023-12-04 00:36:52 25 4
gpt4 key购买 nike

根据 http://docs.oracle.com/javaee/6/api/javax/jms/Message.html#acknowledge()

A client may individually acknowledge each message as it is consumed, or it may choose to acknowledge messages as an application-defined group (which is done by calling acknowledge on the last received message of the group, thereby acknowledging all messages consumed by the session.)

我如何在 ActiveMQ 中做到这一点?我无法让它工作。

最佳答案

这里是 ActiveMQ 客户端的例子,

import javax.jms.Connection;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQMessageConsumer;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.command.ActiveMQTextMessage;

public class SimpleConsumer {

public static void main(String[] args) throws JMSException {
Connection conn = null;
try {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
conn = cf.createConnection("consumer", "consumer");
ActiveMQSession session = (ActiveMQSession) conn.createSession(false,
ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session
.createConsumer(session.createQueue("QUEUE"));
conn.start();
ActiveMQTextMessage msg = null;
while ((msg = (ActiveMQTextMessage) consumer.receive()) != null) {
System.out.println("Received message is: " + msg.getText());
msg.acknowledge();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
}
}
}
}
}

关于jms - ActiveMQ - 是否可以在 CLIENT_ACKNOWLEDGE 模式下确认单个消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40399495/

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