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

问题描述

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

如何在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模式下确认单个消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:22