简体   繁体   English

批量使用JMS消息

[英]Batch consuming of JMS messages

The MessageListener interface only defines a method onMessage that receives a single Message as argument. MessageListener接口仅定义onMessage方法,该方法接收单个Message作为参数。 I'm looking for a way to get multiple Message s from a queue so that I can process the batch and then acknowledge all the Message s in the batch. 我正在寻找一种方法从队列中获取多个Message ,以便我可以处理批处理,然后确认批处理中的所有Message

Is there such feature in JMS world? JMS世界中有这样的功能吗? If no, is it supported by ActiveMQ as an extension? 如果不是,ActiveMQ是否支持扩展?

Thanks, Mickael 谢谢,Mickael

This is explained in JMS 1.1 spec section 4.5.1 Synchronous Delivery : 这在JMS 1.1规范部分4.5.1同步传递中进行了解释

A client can request the next message from a MessageConsumer using one of its receive methods. 客户端可以使用其接收方法之一从MessageConsumer请求下一条消息。 There are several variations of receive that allow a client to poll or wait for the next message. 接收有多种变体,允许客户端轮询或等待下一条消息。

There is even a code example in Section 9.2.2 Receiving a Message Synchronously : 9.2.2同步接收消息中甚至有一个代码示例:

TextMessage stockMessage;
stockMessage = (TextMessage)receiver.receive();

Note that you should also have a look into acknowledgement when using polling and batch processing - see section 4.4.11 Message Acknowledgment for further information on this. 请注意,在使用轮询和批处理时,您还应该了解确认 - 有关此内容的更多信息,请参见4.4.11消息确认部分。 Especially interesting could be the following: 特别有趣的可能是以下内容:

CLIENT_ACKNOWLEDGE - With this option, a client acknowledges a message by calling the message's acknowledge method. CLIENT_ACKNOWLEDGE - 使用此选项,客户端通过调用消息的确认方法来确认消息。 Acknowledging a consumed message automatically acknowledges the receipt of all messages that have been delivered by its session. 确认已消耗的消息会自动确认收到其会话已传递的所有消息。

Yes this is part of the regular JMS API. 是的,这是常规JMS API的一部分。 Instead of doing your consumption in a MessageListener do it in a loop using MessageConsumer.receive() . 而不是在MessageListener进行消费,而是使用MessageConsumer.receive()在循环中进行消费。 For it to be transactionally batched, you need to use the SESSION_TRANSACTED acknowledgement mode, and call Session.commit() when you want to send the batch acknowledgement. 要对其进行事务批处理,您需要使用SESSION_TRANSACTED确认模式,并在要发送批处理确认时调用Session.commit()

Take a look at the source code for the sjms-batch component in Apache Camel to see how this is done. 查看Apache Camel中sjms-batch组件的源代码,了解如何完成此操作。

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

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