How to delay consuming messages in Apache Camel from ActiveMQ

风流意气都作罢 提交于 2020-01-01 17:56:12

问题


I have a requirement where I need to throttle by shaping (queuing) inbound traffic when client app sends more than 1000 requests in a 5 sec time span.

The solution I followed is: I have a camel:throttle setting max requests to 1000 and timespan to 5 sec. When threshold is exceeded I am catching throttle exception and within the onException block, I am sending the throttled messages to an ActiveMQ request queue for further processing later as Camel is overloaded based on 1000 req/ 5 sec config.

I am successful in implementing the above, however I would like to have Camel consumer to further process later not all messages from ActiveMQ request queue at one shot instead process each message with a delay of 10 sec for e.g.

I am not able to set a parameter in ActiveMQ to say delay the message to consumer nor delay Camel consumer pulling off the message from request queue.

How do I cater to my above requirement

Please help

Thanks Ramesh.


回答1:


In another SO thread the winning answers promotes the following solution:

from("activemq:queueA").throttle(10).to("activemq:queueB")

To me this solution only makes sense, if you define a prefetch limit, without which the consumer would not care about any downstream throttling. This route should work:

from("activemq:queueA?&destination.consumer.prefetchSize=10").throttle(10).to("activemq:queueB")

This is the threory behind it, right from http://activemq.apache.org/what-is-the-prefetch-limit-for.html

So ActiveMQ uses a prefetch limit on how many messages can be streamed to a consumer at any point in time. Once the prefetch limit is reached, no more messages are dispatched to the consumer until the consumer starts sending back acknowledgements of messages (to indicate that the message has been processed). The actual prefetch limit value can be specified on a per consumer basis.




回答2:


You can enable scheduled delivery of ActiveMQ and then set in your Camel Route AMQ_SCHEDULED_DELAY header and then send your exchange to a queue. This will result to introducing a delay of AMQ_SCHEDULED_DELAY millis before the message appears in the queue (i.e. be available for consumption).

Check this: http://activemq.apache.org/delay-and-schedule-message-delivery.html



来源:https://stackoverflow.com/questions/35838308/how-to-delay-consuming-messages-in-apache-camel-from-activemq

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