Annotation Type EnableRabbit
-
@Target(TYPE) @Retention(RUNTIME) @Documented @Import(RabbitListenerConfigurationSelector.class) public @interface EnableRabbit
Enable Rabbit listener annotated endpoints that are created under the cover by aRabbitListenerContainerFactory
. To be used onConfiguration
classes as follows:@Configuration @EnableRabbit public class AppConfig { @Bean public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory() { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory()); factory.setMaxConcurrentConsumers(5); return factory; } // other @Bean definitions }
TheRabbitListenerContainerFactory
is responsible to create the listener container responsible for a particular endpoint. Typical implementations, as theSimpleRabbitListenerContainerFactory
used in the sample above, provides the necessary configuration options that are supported by the underlyingMessageListenerContainer
.@EnableRabbit
enables detection ofRabbitListener
annotations on any Spring-managed bean in the container. For example, given a classMyService
:package com.acme.foo; public class MyService { @RabbitListener(containerFactory="myRabbitListenerContainerFactory", queues="myQueue") public void process(String msg) { // process incoming message } }
The container factory to use is identified by thecontainerFactory
attribute defining the name of theRabbitListenerContainerFactory
bean to use. When none is set aRabbitListenerContainerFactory
bean with namerabbitListenerContainerFactory
is assumed to be present.the following configuration would ensure that every time a
Message
is received on theQueue
named "myQueue",MyService.process()
is called with the content of the message:@Configuration @EnableRabbit public class AppConfig { @Bean public MyService myService() { return new MyService(); } // Rabbit infrastructure setup }
Alternatively, ifMyService
were annotated with@Component
, the following configuration would ensure that its@RabbitListener
annotated method is invoked with a matching incoming message:@Configuration @EnableRabbit @ComponentScan(basePackages="com.acme.foo") public class AppConfig { }
Note that the created containers are not registered against the application context but can be easily located for management purposes using theRabbitListenerEndpointRegistry
.Annotated methods can use flexible signature; in particular, it is possible to use the
Message
abstraction and related annotations, seeRabbitListener
Javadoc for more details. For instance, the following would inject the content of the message and a a custom "myCounter" AMQP header:@RabbitListener(containerFactory = "myRabbitListenerContainerFactory", queues = "myQueue") public void process(String msg, @Header("myCounter") int counter) { // process incoming message }
These features are abstracted by theMessageHandlerMethodFactory
that is responsible to build the necessary invoker to process the annotated method. By default,DefaultMessageHandlerMethodFactory
is used.When more control is desired, a
@Configuration
class may implementRabbitListenerConfigurer
. This allows access to the underlyingRabbitListenerEndpointRegistrar
instance. The following example demonstrates how to specify an explicit defaultRabbitListenerContainerFactory
@Configuration @EnableRabbit public class AppConfig implements RabbitListenerConfigurer { @Override public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) { registrar.setContainerFactory(myRabbitListenerContainerFactory()); } @Bean public RabbitListenerContainerFactory<?> myRabbitListenerContainerFactory() { // factory settings } @Bean public MyService myService() { return new MyService(); } }
For reference, the example above can be compared to the following Spring XML configuration:<beans> <rabbit:annotation-driven container-factory="myRabbitListenerContainerFactory"/> <bean id="myRabbitListenerContainerFactory" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory"> // factory settings </bean> <bean id="myService" class="com.acme.foo.MyService"/> </beans>
RabbitListenerEndpointRegistry
in case you need more control on the way the containers are created and managed. The example below also demonstrates how to customize theRabbitHandlerMethodFactory
to use with a customValidator
so that payloads annotated withValidated
are first validated against a customValidator
.@Configuration @EnableRabbit public class AppConfig implements RabbitListenerConfigurer { @Override public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) { registrar.setEndpointRegistry(myRabbitListenerEndpointRegistry()); registrar.setMessageHandlerMethodFactory(myMessageHandlerMethodFactory()); } @Bean public RabbitListenerEndpointRegistry<?> myRabbitListenerEndpointRegistry() { // registry configuration } @Bean public RabbitHandlerMethodFactory myMessageHandlerMethodFactory() { DefaultRabbitHandlerMethodFactory factory = new DefaultRabbitHandlerMethodFactory(); factory.setValidator(new MyValidator()); return factory; } @Bean public MyService myService() { return new MyService(); } }
For reference, the example above can be compared to the following Spring XML configuration:<beans> <rabbit:annotation-driven registry="myRabbitListenerEndpointRegistry" handler-method-factory="myRabbitHandlerMethodFactory"/> <bean id="myRabbitListenerEndpointRegistry" class="org.springframework.amqp.rabbit.config.RabbitListenerEndpointRegistry"> // registry configuration </bean> <bean id="myRabbitHandlerMethodFactory" class="org.springframework.amqp.rabbit.config.DefaultRabbitHandlerMethodFactory"> <property name="validator" ref="myValidator"/> </bean> <bean id="myService" class="com.acme.foo.MyService"/> </beans>
RabbitListenerConfigurer
also allows for fine-grained control over endpoints registration via theRabbitListenerEndpointRegistrar
. For example, the following configures an extra endpoint:@Configuration @EnableRabbit public class AppConfig implements RabbitListenerConfigurer { @Override public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) { SimpleRabbitListenerEndpoint myEndpoint = new SimpleRabbitListenerEndpoint(); // ... configure the endpoint registrar.registerEndpoint(endpoint, anotherRabbitListenerContainerFactory()); } @Bean public MyService myService() { return new MyService(); } @Bean public RabbitListenerContainerFactory<?> anotherRabbitListenerContainerFactory() { // ... } // Rabbit infrastructure setup }
Note that all beans implementingRabbitListenerConfigurer
will be detected and invoked in a similar fashion. The example above can be translated in a regular bean definition registered in the context in case you use the XML configuration.- Since:
- 1.4
- Author:
- Stephane Nicoll, Artem Bilan
- See Also:
RabbitListener
,RabbitListenerAnnotationBeanPostProcessor
,RabbitListenerEndpointRegistrar
,RabbitListenerEndpointRegistry