ActiveMQ - is it possible to acknowledge single message in CLIENT_ACKNOWLEDGE mode

风流意气都作罢 提交于 2019-12-08 20:00:31

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

I am correcting my answer after the below comments with 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.

So, the CLIENT_ACKNOWLEDGE option can NOT be used to send an acknowledgment to the JMS provider for a single message.

You can look at spec here:

http://download.oracle.com/otndocs/jcp/jms-2_0-fr-eval-spec/index.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!