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 onConfigurationclasses 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 }TheRabbitListenerContainerFactoryis responsible to create the listener container responsible for a particular endpoint. Typical implementations, as theSimpleRabbitListenerContainerFactoryused in the sample above, provides the necessary configuration options that are supported by the underlyingMessageListenerContainer.@EnableRabbitenables detection ofRabbitListenerannotations 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 thecontainerFactoryattribute defining the name of theRabbitListenerContainerFactorybean to use. When none is set aRabbitListenerContainerFactorybean with namerabbitListenerContainerFactoryis assumed to be present.the following configuration would ensure that every time a
Messageis received on theQueuenamed "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, ifMyServicewere annotated with@Component, the following configuration would ensure that its@RabbitListenerannotated 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
Messageabstraction and related annotations, seeRabbitListenerJavadoc 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 theMessageHandlerMethodFactorythat is responsible to build the necessary invoker to process the annotated method. By default,DefaultMessageHandlerMethodFactoryis used.When more control is desired, a
@Configurationclass may implementRabbitListenerConfigurer. This allows access to the underlyingRabbitListenerEndpointRegistrarinstance. 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:
It is also possible to specify a custom<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>RabbitListenerEndpointRegistryin case you need more control on the way the containers are created and managed. The example below also demonstrates how to customize theRabbitHandlerMethodFactoryto use with a customValidatorso that payloads annotated withValidatedare 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:
Implementing<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>RabbitListenerConfigureralso 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 implementingRabbitListenerConfigurerwill 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.