Annotation Type RabbitListener
-
@Target({TYPE,METHOD,ANNOTATION_TYPE}) @Retention(RUNTIME) @MessageMapping @Documented @Repeatable(RabbitListeners.class) public @interface RabbitListenerAnnotation that marks a method to be the target of a Rabbit message listener on the specifiedqueues()(orbindings()). ThecontainerFactory()identifies theRabbitListenerContainerFactoryto use to build the rabbit listener container. If not set, a default container factory is assumed to be available with a bean name ofrabbitListenerContainerFactoryunless an explicit default has been provided through configuration.Processing of
@RabbitListenerannotations is performed by registering aRabbitListenerAnnotationBeanPostProcessor. This can be done manually or, more conveniently, through the<rabbit:annotation-driven/>element orEnableRabbitannotation.Annotated methods are allowed to have flexible signatures similar to what
MessageMappingprovides, that isChannelto get access to the ChannelMessageor one if subclass to get access to the raw AMQP messageMessageto use the messaging abstraction counterpart@Payload-annotated method arguments including the support of validation@Header-annotated method arguments to extract a specific header value, including standard AMQP headers defined byAmqpHeaders@Headers-annotated argument that must also be assignable toMapfor getting access to all headers.MessageHeadersarguments for getting access to all headers.MessageHeaderAccessororAmqpMessageHeaderAccessorfor convenient access to all method arguments.
Annotated methods may have a non
voidreturn type. When they do, the result of the method invocation is sent as a reply to the queue defined by theReplyToheader of the incoming message. When this value is not set, a default queue can be provided by adding @SendToto the method declaration.When
bindings()are provided, and the application context contains aRabbitAdmin, the queue, exchange and binding will be automatically declared.When defined at the method level, a listener container is created for each method. The
MessageListeneris aMessagingMessageListenerAdapter, configured with aMethodRabbitListenerEndpoint.When defined at the class level, a single message listener container is used to service all methods annotated with
@RabbitHandler. Method signatures of such annotated methods must not cause any ambiguity such that a single method can be resolved for a particular inbound message. TheMessagingMessageListenerAdapteris configured with aMultiMethodRabbitListenerEndpoint.- Since:
- 1.4
- Author:
- Stephane Nicoll, Gary Russell
- See Also:
EnableRabbit,RabbitListenerAnnotationBeanPostProcessor,RabbitListeners
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.StringackModeOverride the container factoryAcknowledgeModeproperty.java.lang.StringadminReference to aAmqpAdmin.java.lang.StringautoStartupSet to true or false, to override the default setting in the container factory.QueueBinding[]bindingsArray ofQueueBindings providing the listener's queue names, together with the exchange and optional binding information.java.lang.StringconcurrencySet the concurrency of the listener container for this listener.java.lang.StringcontainerFactoryThe bean name of theRabbitListenerContainerFactoryto use to create the message listener container responsible to serve this endpoint.java.lang.StringconverterWinsContentTypeSet to 'false' to override any content type headers set by the message converter with the value of the 'replyContentType' property.java.lang.StringerrorHandlerSet anRabbitListenerErrorHandlerto invoke if the listener method throws an exception.booleanexclusiveWhentrue, a single consumer in the container will have exclusive use of thequeues(), preventing other consumers from receiving messages from the queues.java.lang.StringexecutorSet the task executor bean name to use for this listener's container; overrides any executor set on the container factory.java.lang.StringgroupIf provided, the listener container for this listener will be added to a bean with this value as its name, of typeCollection<MessageListenerContainer>.java.lang.StringidThe unique identifier of the container managing for this endpoint.java.lang.StringmessageConverterOverride the container factory's message converter used for this listener.java.lang.StringpriorityThe priority of this endpoint.java.lang.String[]queuesThe queues for this listener.Queue[]queuesToDeclareThe queues for this listener.java.lang.StringreplyContentTypeUsed to set the content type of a reply message.java.lang.StringreplyPostProcessorThe bean name of aReplyPostProcessorto post process a response before it is sent.java.lang.StringreturnExceptionsSet to "true" to cause exceptions thrown by the listener to be sent to the sender using normalreplyTo/@SendTosemantics.
-
-
-
Element Detail
-
id
java.lang.String id
The unique identifier of the container managing for this endpoint.If none is specified an auto-generated one is provided.
- Returns:
- the
idfor the container managing for this endpoint. - See Also:
RabbitListenerEndpointRegistry.getListenerContainer(String)
- Default:
- ""
-
-
-
containerFactory
java.lang.String containerFactory
The bean name of theRabbitListenerContainerFactoryto use to create the message listener container responsible to serve this endpoint.If not specified, the default container factory is used, if any. If a SpEL expression is provided (
#{...}), the expression can either evaluate to a container factory instance or a bean name.- Returns:
- the
RabbitListenerContainerFactorybean name.
- Default:
- ""
-
-
-
queues
java.lang.String[] queues
The queues for this listener. The entries can be 'queue name', 'property-placeholder keys' or 'expressions'. Expression must be resolved to the queue name orQueueobject. The queue(s) must exist, or be otherwise defined elsewhere as a bean(s) with aRabbitAdminin the application context. Mutually exclusive withbindings()andqueuesToDeclare().- Returns:
- the queue names or expressions (SpEL) to listen to from target
- See Also:
MessageListenerContainer
- Default:
- {}
-
-
-
queuesToDeclare
Queue[] queuesToDeclare
The queues for this listener. If there is aRabbitAdminin the application context, the queue will be declared on the broker with default binding (default exchange with the queue name as the routing key). Mutually exclusive withbindings()andqueues(). NOTE: Broker-named queues cannot be declared this way, they must be defined as beans (with an empty string for the name).- Returns:
- the queue(s) to declare.
- Since:
- 2.0
- See Also:
MessageListenerContainer
- Default:
- {}
-
-
-
exclusive
boolean exclusive
Whentrue, a single consumer in the container will have exclusive use of thequeues(), preventing other consumers from receiving messages from the queues. Whentrue, requires a concurrency of 1. Defaultfalse.- Returns:
- the
exclusiveboolean flag.
- Default:
- false
-
-
-
admin
java.lang.String admin
Reference to aAmqpAdmin. Required if the listener is using auto-delete queues and those queues are configured for conditional declaration. This is the admin that will (re)declare those queues when the container is (re)started. See the reference documentation for more information. If a SpEL expression is provided (#{...}) the expression can evaluate to anAmqpAdmininstance or bean name.- Returns:
- the
AmqpAdminbean name.
- Default:
- ""
-
-
-
bindings
QueueBinding[] bindings
Array ofQueueBindings providing the listener's queue names, together with the exchange and optional binding information. Mutually exclusive withqueues()andqueuesToDeclare(). NOTE: Broker-named queues cannot be declared this way, they must be defined as beans (with an empty string for the name).- Returns:
- the bindings.
- Since:
- 1.5
- See Also:
MessageListenerContainer
- Default:
- {}
-
-
-
group
java.lang.String group
If provided, the listener container for this listener will be added to a bean with this value as its name, of typeCollection<MessageListenerContainer>. This allows, for example, iteration over the collection to start/stop a subset of containers.- Returns:
- the bean name for the group.
- Since:
- 1.5
- Default:
- ""
-
-
-
returnExceptions
java.lang.String returnExceptions
Set to "true" to cause exceptions thrown by the listener to be sent to the sender using normalreplyTo/@SendTosemantics. When false, the exception is thrown to the listener container and normal retry/DLQ processing is performed.- Returns:
- true to return exceptions. If the client side uses a
RemoteInvocationAwareMessageConverterAdapterthe exception will be re-thrown. Otherwise, the sender will receive aRemoteInvocationResultwrapping the exception. - Since:
- 2.0
- Default:
- ""
-
-
-
errorHandler
java.lang.String errorHandler
Set anRabbitListenerErrorHandlerto invoke if the listener method throws an exception. A simple String representing the bean name. If a Spel expression (#{...}) is provided, the expression must evaluate to a bean name or aRabbitListenerErrorHandlerinstance.- Returns:
- the error handler.
- Since:
- 2.0
- Default:
- ""
-
-
-
concurrency
java.lang.String concurrency
Set the concurrency of the listener container for this listener. Overrides the default set by the listener container factory. Maps to the concurrency setting of the container type.For a
SimpleMessageListenerContainerif this value is a simple integer, it sets a fixed number of consumers in theconcurrentConsumersproperty. If it is a string with the form"m-n", theconcurrentConsumersis set tomand themaxConcurrentConsumersis set ton.For a
DirectMessageListenerContainerit sets theconsumersPerQueueproperty.- Returns:
- the concurrency.
- Since:
- 2.0
- Default:
- ""
-
-
-
executor
java.lang.String executor
Set the task executor bean name to use for this listener's container; overrides any executor set on the container factory. If a SpEL expression is provided (#{...}), the expression can either evaluate to a executor instance or a bean name.- Returns:
- the executor bean name.
- Since:
- 2.2
- Default:
- ""
-
-
-
ackMode
java.lang.String ackMode
Override the container factoryAcknowledgeModeproperty. Must be one of the valid enumerations. If a SpEL expression is provided, it must evaluate to aStringorAcknowledgeMode.- Returns:
- the acknowledgement mode.
- Since:
- 2.2
- Default:
- ""
-
-
-
replyPostProcessor
java.lang.String replyPostProcessor
The bean name of aReplyPostProcessorto post process a response before it is sent. If a SpEL expression is provided (#{...}), the expression can either evaluate to a post processor instance or a bean name.- Returns:
- the bean name.
- Since:
- 2.2.5
- See Also:
AbstractAdaptableMessageListener.setReplyPostProcessor(org.springframework.amqp.rabbit.listener.adapter.ReplyPostProcessor)
- Default:
- ""
-
-
-
messageConverter
java.lang.String messageConverter
Override the container factory's message converter used for this listener.- Returns:
- the message converter bean name. If a SpEL expression is provided
(
#{...}), the expression can either evaluate to a converter instance or a bean name. - Since:
- 2.3
- Default:
- ""
-
-
-
replyContentType
java.lang.String replyContentType
Used to set the content type of a reply message. Useful when used in conjunction with message converters that can handle multiple content types, such as theContentTypeDelegatingMessageConverter. SpEL expressions and property placeholders are supported. Also useful if you wish to control the final content type property when used with certain converters. This does not apply when the return type isMessageorMessage; set the content type message property or header respectively, in those cases.- Returns:
- the content type.
- Since:
- 2.3
- See Also:
converterWinsContentType()
- Default:
- ""
-
-
-
converterWinsContentType
java.lang.String converterWinsContentType
Set to 'false' to override any content type headers set by the message converter with the value of the 'replyContentType' property. Some converters, such as theSimpleMessageConverteruse the payload type and set the content type header appropriately. For example, if you set the 'replyContentType' to "application/json" and use the simple message converter when returning a String containing JSON, the converter will overwrite the content type to 'text/plain'. Set this to false, to prevent that action. This does not apply when the return type isMessagebecause there is no conversion involved. When returning aMessage, set the content type message header andAmqpHeaders.CONTENT_TYPE_CONVERTER_WINSto false.- Returns:
- false to use the replyContentType.
- Since:
- 2.3
- See Also:
replyContentType()
- Default:
- "true"
-
-