ActiveMQ - 是否可以在 CLIENT_ACKNOWLEDGE 模式下确认单个消息
ActiveMQ - is it possible to acknowledge single message in CLIENT_ACKNOWLEDGE mode
根据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 中实现?我无法让它工作。
在与 Matt Pavlovich 发表以下评论后,我正在更正我的答案。
CLIENT_ACKNOWLEDGE: With this option, a client acknowledges a message
by calling the message’s acknowledge method. Acknowledging a consumed
message automatically acknowledges the receipt of all messages that
have been delivered by its session.
因此,CLIENT_ACKNOWLEDGE 选项不能用于向 JMS 提供程序发送单个消息的确认。
您可以在此处查看规格:
http://download.oracle.com/otndocs/jcp/jms-2_0-fr-eval-spec/index.html
这里是 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) {
}
}
}
}
}
根据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 中实现?我无法让它工作。
在与 Matt Pavlovich 发表以下评论后,我正在更正我的答案。
CLIENT_ACKNOWLEDGE: With this option, a client acknowledges a message by calling the message’s acknowledge method. Acknowledging a consumed message automatically acknowledges the receipt of all messages that have been delivered by its session.
因此,CLIENT_ACKNOWLEDGE 选项不能用于向 JMS 提供程序发送单个消息的确认。
您可以在此处查看规格:
http://download.oracle.com/otndocs/jcp/jms-2_0-fr-eval-spec/index.html
这里是 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) {
}
}
}
}
}