jmsspring-jmspollingmessage-listener

Spring JMS DefaultMessageListenerContainer Polling frequency


I am using the DefaultMessageListenerContainer for consuming messages from ActiveMQ queue as below. With this implementation is there any polling mechanism, does the listener poll the queue to see if there is a new message every 1 second or so , or does the onMessage method get invoked whenever there is a new message in the queue? If it uses polling how can we increase or decrease the polling frequency (time) .

DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setMessageListener(new MessageJmsListener ());

public class MessageJmsListener implements MessageListener {
    
  @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                //process the message and create record in Data Base                
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }     
}

Solution

  • The container polls the JMS client, but the broker pushes messages to the client.

    So, no, the container does not poll the queue directly.

    If there are no messages in the queue, the container will timeout after receiveTimeout and immediately re-poll and will get the next message as soon as the broker sends it.

    The prefetch determines how many messages are sent to the consumer by the broker; so that might impact performance (but it's 1000 by default, I think, with recent ActiveMQ versions).

    Setting the prefetch to 1 will give you the slowest delivery rate.

    If you want to slow things down, you can add a Thread.sleep() in your listener.