简体   繁体   English

ActiveMQ-保留消耗的消息,直到确认删除为止

[英]ActiveMQ - keep consumed messages until it's acknowledge to delete

I have an Spring Boot app that is listening to a ActiveMQ queue. 我有一个正在侦听ActiveMQ队列的Spring Boot应用程序。

Is there a way to tell ActiveMQ to keep consumed messages until it's received acknowledgement to delete the message from my service? 有没有办法告诉ActiveMQ保留消耗的消息,直到收到确认从我的服务中删除消息的确认?

My scenario is I read message from queue and once consumed, I'm sending it to an external queue. 我的情况是我从队列中读取消息,并且一旦消耗掉,就将其发送到外部队列。 If the external service fails to process the message, I would loose the message. 如果外部服务无法处理该消息,我将松开该消息。 Is there away I can tell active mq to hold onto consumed message until it's received an acknowledgement to delete. 在那儿,我可以告诉活动的mq保留已使用的消息,直到收到确认删除为止。

The safest option would be to use an XA transaction to perform the consume/send atomically (assuming the broker where you're sending the message supports XA). 最安全的选择是使用XA事务原子地执行消耗/发送(假设您要在其中发送消息的代理支持XA)。 I'm not sure how easy or hard that is in Spring, but I know it's pretty simple in a Java EE container. 我不确定在Spring中有多么容易或困难,但是我知道在Java EE容器中这非常简单。

You can use the individual acknowledge mode which ack the single message. 您可以使用确认单个消息的个人确认模式。 Below is the code for same: 下面是相同的代码:

    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());
               // Call your service and ack the message if successfully processed                    
                 msg.acknowledge();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM