JMS消息仅在消息被确认之前确认 [英] Jms message acknowledge only till the message acknowledged on

查看:138
本文介绍了JMS消息仅在消息被确认之前确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保消息确认仅删除直到在jms代理中调用了确认的消息为止的消息. 目前,我有一个从jms队列中消费并对其进行部分处理的系统.有时稍后,这些消息的批处理将被另一个线程保留.我现在需要确认消息.但是问题是我必须停止使用这些消息,否则,确认先前收到的消息还将确认收到的所有其他后续消息.

How to ensure message acknowledge deletes only messages upto the message on which acknowledge is called in a jms broker. Currently I have a system which consumes from a jms queue and partially processes it.Sometime later a batch of these messages gets persisted by a different thread. I need to acknowledge on messages now. But the problem is I have to stop consuming the messages, otherwise acknowledging a previously received message will also acknowledge all other subsequent messages received.

换句话说,假设我在队列中有10条消息.我消耗了其中的7个,然后在第5条消息中确认.反过来,这会将使用者收到的所有7条消息从队列中删除.有一种方法只能确认并从队列中删除消息,直到第5条消息为止.

In other words suppose I have 10 messages in a queue. I consume 7 of them, and then acknowledge on 5th message. This in turn removes all 7 messages received by consumer from the queue.Is there a way to only acknowledge and remove messages from queue till 5th message.

我尝试创建两个会话并从不同的会话使用,但是(使用Apache Qpid Atleast)这执行不一致.我的意思是不一致的,有时在测试过程中,无论您等待多长时间,一个消费者都能够接收消息,而另一个消费者却根本无法接收消息.这本来可以作为我的解决方案,但由于不一致,因此无法将其用作解决方案.

I have tried creating two sessions and consuming from different sessions, but (with apache qpid atleast) this performs inconsistently. By inconsistently I mean, sometimes during the test it so happens that one consumer is able to receive messages, while the other doesn't receive at all, no matter how long you wait. This would have worked for me as a solution, but because of inconsistency can't use this as a solution.

推荐答案

我知道这篇文章已经过时了,但是这个答案应该对那些后来发现它的人有所帮助.

I understand this post is old, but this answer should benefit those who stumble upon it later.

如果您希望对要确认的邮件进行精细控制,则individual确认方法应该可以为您提供帮助.使用此确认模式,您可以确认会话中的各个消息.未确认的邮件将重新发送.

If you'd like fine grained control of which messages you'd like to acknowledge, the individual acknowledge method should help you. Using this acknowledgement mode you can ack individual messages in a session. Messages that have not been ack-ed will be redelivered.

这不是规范的一部分,但是大多数队列提供程序都在规范之外支持它.

This is not part of the spec, but most queue providers support it outside the spec.

为获得更大的灵活性,Message Queue允许您自定义JMS 客户端确认模式.在客户端确认模式下,客户端 通过调用 消息对象的accept()方法.

For more flexibility, Message Queue lets you customize the JMS client-acknowledge mode. In client-acknowledge mode, the client explicitly acknowledges message consumption by invoking the acknowledge() method of a message object.

的标准行为 此方法将导致会话确认所有消息, 自上次以来,该会话中的任何消费者都已消费了 该方法被调用. (即,会话确认当前 消息以及所有先前未确认的消息,无论谁 消耗掉了它们.)

The standard behavior of this method is to cause the session to acknowledge all messages that have been consumed by any consumer in the session since the last time the method was invoked. (That is, the session acknowledges the current message and all previously unacknowledged messages, regardless of who consumed them.)

除了JMS指定的标准行为外,Message Queue 使您可以使用客户端确认模式在某处确认一条消息 时间.

In addition to the standard behavior specified by JMS, Message Queue lets you use client-acknowledge mode to acknowledge one message at a time.

public interface com.sun.messaging.jms.Message {
          void acknowledgeThisMessage() throws JMSException;
          void acknowledgeUpThroughThisMessage() throws JMSException;
}

ActiveMQ

人们可以想象其他的确认模式也将有用,因为 示例:CONSUMER_ACKNOWLEDGE,其中Message.acknowledge()将 只确认在特定MessageConsumer上收到的消息, 或CONSUMER_CHECKPOINT_ACKNOWLEDGE,其中Message.acknowledge()将 仅确认直到(包括)该消息为止收到的消息 调用该方法的实例.

One can imagine other acknowledge modes that would be useful too, for example: CONSUMER_ACKNOWLEDGE where Message.acknowledge() would acknowledge only messages received up on a particular MessageConsumer, or CONSUMER_CHECKPOINT_ACKNOWLEDGE where Message.acknowledge() would acknowledge only messages received up to and including the Message instance on which the method was called.

但是,如果不承担所有这些各种不同的可能性, 是否可以考虑仅添加INDIVIDUAL_ACKNOWLEDGE 模式?仅此一项就可以使多线程应用程序成为可能 来实现他们所需的任何行为.

But without embarking on all these various different possibilities, would it be possible to consider just adding INDIVIDUAL_ACKNOWLEDGE mode? This alone would make it possible for multithreaded applications to achieve whatever behaviors they need.

connection.createQueueSession(false, ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);

我个人没有使用过QPID,但是

I have not used QPID personally, however the documentation hints to the fact that individual message acks are possible.

Examples
  # acknowledge all received messages
  session.acknowledge

  # acknowledge a single message
  session.acknowledge :message => message

在处理批处理时,您可以确认收到并处理的每条消息.如果遇到异常,请不要确认该消息.

While processing a batch you can ack each message that is received and processed. If you encounter an exception, do not ack the message.

这篇关于JMS消息仅在消息被确认之前确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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