Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control the rate at which Spring receives from a queue?

Tags:

java

spring

jms

I am using Spring's message-driven POJO framework (and DefaultMessageListenerContainer in particular) to listen to several queues and topics.

In the case of one particularly queue, there is a need to slow the rate at which I drain the queue, on the order of one message every five minutes. The actual processing of the messages is a sub-second operation, but I would like the listener to sit idle for some time in between messages.

I have created a bit of a hack, but it is decidedly sub-optimal: What I've done is to set the max concurrency to 1 and add a Thread.sleep(..) after processing each message. I would like to find a way instead to use the DefaultMessageListenerContainer to wait between attempts to receive, rather than causing the handler to do the waiting during the would-be processing of a message.

I had considered if there was a ScheduledExecutor that would help, but I realize that the throttling would need to be done where the tasks are produced. Is there perhaps some method from DefaultMessageListenerContainer that I could override to accomplish what I'm after?

like image 587
Ray Avatar asked Jan 19 '12 13:01

Ray


1 Answers

Depending on the provider of the queue, you may be able to set a max rate for consumers that consume it's queues.

For example in hornetQ you set this in the connection factory using consumer-max-rate.

like image 153
andreadi Avatar answered Oct 13 '22 08:10

andreadi