简体   繁体   English

如何使用Spring Integration轮询AMQP队列

[英]How to poll an AMQP queue using Spring Integration

I have the use case that I need to wait 2 hours before consuming messages from an AMQP (we use Rabbit) queue. 我有一个用例,我需要等待2个小时才能使用AMQP(我们使用Rabbit)队列中的消息。

EDIT: To clarify my use case... I need each individual message to wait 2 hours before being read. 编辑:为了澄清我的用例...我需要每条单独的消息要等待2个小时才能被读取。 Eg Message 1 arrives at 10am and Message 2 arrives at 10:15. 例如,消息1在10am到达,消息2在10:15到达。 I need Message 1 to be read at 12p and Message 2 to be read at 12:15p. 我需要以12p读取消息1和以12:15p读取消息2。

We are using Spring Integration 3.x. 我们正在使用Spring Integration3.x。

The int-amqp:inbound-channel-adapter is message driven and doesn't have a polling option from what I can find. int-amqp:inbound-channel-adapter是消息驱动的,根据我的发现没有轮询选项。

A couple things I've thought of: 我想到的几件事:

Any suggestions? 有什么建议么?

We don't currently have a polling inbound adapter. 我们目前没有轮询入站适配器。 #1 is easy. #1很容易。 For #2, the simplest would be to use a RabbitTemplate and invoke receive() from an inbound-channel-adapter in a POJO. 对于#2,最简单的方法是使用RabbitTemplate并从POJO中的inbound-channel-adapter调用receive()

I would go with #1; 我会选择#1; you don't need quartz, you can use a simple Spring scheduled task and a control bus to start the adapter. 您不需要石英,可以使用简单的Spring计划任务和控制总线来启动适配器。

Another trick is about to use PollableAmqpChannel : 另一个技巧是使用PollableAmqpChannel

<int-amqp:channel id="myQueueName" message-driven="false"/>

and provide the <poller> for the subscriber to that channel. 并为该频道的订户提供<poller>

There is no reason to send messages to that channel (because you will poll messages from Rabbit Queue) and, right, it looks like anti-pattern , but it is a hook how to avoid any workarounds with direct RabbitTemplate usage via SpEL. 没有理由将消息发送到该通道(因为你会从轮询队列兔消息)和正确的,它看起来像anti-pattern ,但它是一个钩子如何避免与直接的任何变通办法RabbitTemplate通过规划环境地政司使用。

UPDATE 更新

<delayer> can help you, but it depends of your requirements. <delayer>可以帮助您,但这取决于您的要求。 If you don't want to poll messages from RabbitMQ, you should use the workaround above. 如果您不想轮询RabbitMQ的消息,则应使用上面的解决方法。 But if you just don't want to process message until some time is elapsed, you can just 'delay' it for that time. 但是,如果您只是不想在一段时间后才处理消息,则可以在该时间内“延迟”消息。

Don't forget to add persistent message-store to avoid losing messages during that period and unexpected application failure. 不要忘记添加持久性message-store以避免在此期间丢失消息以及意外的应用程序故障。

FYI, how I solved the issue. 仅供参考,我如何解决此问题。 (Used solution #3). (使用的解决方案3)。

<rabbit:queue name="delayQueue" durable="true">
  <rabbit:queue-arguments>
    <entry key="x-message-ttl">
       <value type="java.lang.Long">7200000</
     </entry>
     <entry key="x-dead-letter-exchange" value="finalDestinationTopic"/>
     <entry key="x-dead-letter-routing-key" value="finalDestinationQueue"/>
  </rabbit:queue-arguments>
</rabbit:queue>

<rabbit:topic-exchange name="finalDestinationTopic">
  <rabbit:bindings>
    <rabbit:binding queue="finalDestinationQueue" pattern="finalDestinationQueue"/>
  </rabbit:bindings>
</rabbit:topic-exchange>

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

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