ActiveMQ-是否可以在CLIENT_ACKNOWLEDGE模式下确认单个消息 [英] ActiveMQ - is it possible to acknowledge single message in CLIENT_ACKNOWLEDGE mode

查看:668
本文介绍了ActiveMQ-是否可以在CLIENT_ACKNOWLEDGE模式下确认单个消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 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中做到这一点?我无法使其正常工作.

How can I do it in ActiveMQ? I was unable to make it work.

推荐答案

这是ActiveMQ客户端的示例

here is example with ActiveMQ client,

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) {
                }
            }
        }
    }
}

这篇关于ActiveMQ-是否可以在CLIENT_ACKNOWLEDGE模式下确认单个消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆