Search code examples
spring-jms

Can we access getConcurrentConsumers method while using @JmsListener Mechanism


I am using @JmsListener to consume messages from the Queue. As this requires DefaultJmsListenerContainerFactory as a connection factory, we can setup concurrency as

DefaultJmsListenerContainerFactory  factory = new DefaultJmsListenerContainerFactory ()
factory.setConcurrency("1-1");

Question is can we get the number of Concurrent Consumers programmatically ?

Like if we use DefaultMessageListenerContainer we have the ability to get the Concurrent Consumers using the method getConcurrentConsumers() and also we can set consumers using method setConcurrentConsumers().

I have used JmsListenerEndPointRegistry to get reference to the container and access the lifecycle methods like start() , stop() but can we set Consumers using this regisrty ?

Or is there any different way in which we can access the methods provided my DefaultMessageListenerContainer and also use the annotation based mechanism i.e @JmsListener ?


Solution

  • You can set the concurrency on the @JmsListener itself; it overrides whatever the factory is configured for.

        /**
         * The concurrency limits for the listener, if any. Overrides the value defined
         * by the container factory used to create the listener container.
         * <p>The concurrency limits can be a "lower-upper" String &mdash; for example,
         * "5-10" &mdash; or a simple upper limit String &mdash; for example, "10", in
         * which case the lower limit will be 1.
         * <p>Note that the underlying container may or may not support all features.
         * For instance, it may not be able to scale, in which case only the upper limit
         * is used.
         */
        String concurrency() default "";
    

    You can cast the listener container from regisgtry.getListenerContainer(id) to a DefaultMessageListenerContainer to call getConcurrentConsumers().