How to delay consuming messages in Apache Camel fr

2019-04-16 11:15发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-04-16 11:33

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-16 11:51

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.

查看更多
登录 后发表回答