How can I control the speed with which Spring gets out of the queue?

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

In the case of one particular queue, it is necessary to slow down the speed at which I reset the queue, on the order of one message every five minutes. Actual message processing is auxiliary, but I would like the listener to sit idle for a while between messages.

I created a little hack, but it is clearly not optimal: I did to set max concurrency to 1 and add Thread.sleep(..) after processing each message. I would like to find a way instead to use the DefaultMessageListenerContainer to wait between retrieval attempts, instead of forcing the handler to wait while future processing the message.

I wondered if there was a ScheduledExecutor that would help, but I understand that throttling should be done where the tasks are performed. Is there any possible method from DefaultMessageListenerContainer that I could override to accomplish what I need?

+6
source share
3 answers

Depending on the queue provider, you can set a maximum bid for consumers who consume it in the queue.

For example, in hornetQ you set this in a factory connection using consumer maximum speed.

+4
source

An alternative to changing your consumer behavior is to use Apache Camel to delay messages in this particular queue.

http://camel.apache.org/delayer.html describes the functionality of the Camel Delayer template. For example:

 <route> <from uri="jms:YOURQUEUE"/> <delay> <constant>1000</constant> </delay> <to uri="jms:DELAYEDQUEUE"/> </route> 

If you then use DELAYEDQUEUE, and all messages will be delayed for 1 second.

+3
source

I'm not 100% sure, but I think that receiveTimeout is what you want.

  <bean id="blahContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> .... <!-- 300000 = 5 * 60 * 1000 == 5 min --> <property name="receiveTimeout" value="300000"/> </bean> 

receiveTimeout takes a timeout in milliseconds, you can learn more about it in javadocs

0
source

Source: https://habr.com/ru/post/906395/


All Articles