Annotation Type RabbitListener
-
@Target({TYPE,METHOD,ANNOTATION_TYPE}) @Retention(RUNTIME) @MessageMapping @Documented @Repeatable(RabbitListeners.class) public @interface RabbitListener
Annotation that marks a method to be the target of a Rabbit message listener on the specifiedqueues()
(orbindings()
). ThecontainerFactory()
identifies theRabbitListenerContainerFactory
to use to build the rabbit listener container. If not set, a default container factory is assumed to be available with a bean name ofrabbitListenerContainerFactory
unless an explicit default has been provided through configuration.Processing of
@RabbitListener
annotations is performed by registering aRabbitListenerAnnotationBeanPostProcessor
. This can be done manually or, more conveniently, through the<rabbit:annotation-driven/>
element orEnableRabbit
annotation.Annotated methods are allowed to have flexible signatures similar to what
MessageMapping
provides, that isChannel
to get access to the ChannelMessage
or one if subclass to get access to the raw AMQP messageMessage
to 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 toMap
for getting access to all headers.MessageHeaders
arguments for getting access to all headers.MessageHeaderAccessor
orAmqpMessageHeaderAccessor
for convenient access to all method arguments.
Annotated methods may have a non
void
return type. When they do, the result of the method invocation is sent as a reply to the queue defined by theReplyTo
header of the incoming message. When this value is not set, a default queue can be provided by adding @SendTo
to 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
MessageListener
is 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. TheMessagingMessageListenerAdapter
is 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 String
ackMode
Override the container factoryAcknowledgeMode
property.String
admin
Reference to aAmqpAdmin
.String
autoStartup
Set to true or false, to override the default setting in the container factory.QueueBinding[]
bindings
Array ofQueueBinding
s providing the listener's queue names, together with the exchange and optional binding information.String
concurrency
Set the concurrency of the listener container for this listener.String
containerFactory
The bean name of theRabbitListenerContainerFactory
to use to create the message listener container responsible to serve this endpoint.String
converterWinsContentType
Set to 'false' to override any content type headers set by the message converter with the value of the 'replyContentType' property.String
errorHandler
Set anRabbitListenerErrorHandler
to invoke if the listener method throws an exception.boolean
exclusive
Whentrue
, a single consumer in the container will have exclusive use of thequeues()
, preventing other consumers from receiving messages from the queues.String
executor
Set the task executor bean name to use for this listener's container; overrides any executor set on the container factory.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>
.String
id
The unique identifier of the container managing for this endpoint.String
messageConverter
Override the container factory's message converter used for this listener.String
priority
The priority of this endpoint.String[]
queues
The queues for this listener.Queue[]
queuesToDeclare
The queues for this listener.String
replyContentType
Used to set the content type of a reply message.String
replyPostProcessor
The bean name of aReplyPostProcessor
to post process a response before it is sent.String
returnExceptions
Set to "true" to cause exceptions thrown by the listener to be sent to the sender using normalreplyTo/@SendTo
semantics.
-
-
-
Element Detail
-
id
String id
The unique identifier of the container managing for this endpoint.If none is specified an auto-generated one is provided.
- Returns:
- the
id
for the container managing for this endpoint. - See Also:
RabbitListenerEndpointRegistry.getListenerContainer(String)
- Default:
- ""
-
-
-
containerFactory
String containerFactory
The bean name of theRabbitListenerContainerFactory
to 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
RabbitListenerContainerFactory
bean name.
- Default:
- ""
-
-
-
queues
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 orQueue
object. The queue(s) must exist, or be otherwise defined elsewhere as a bean(s) with aRabbitAdmin
in 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 aRabbitAdmin
in 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()
.- 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
exclusive
boolean flag.
- Default:
- false
-
-
-
priority
String priority
The priority of this endpoint. Requires RabbitMQ 3.2 or higher. Does not change the container priority by default. Larger numbers indicate higher priority, and both positive and negative numbers can be used.- Returns:
- the priority for the endpoint.
- Default:
- ""
-
-
-
admin
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 anAmqpAdmin
instance or bean name.- Returns:
- the
AmqpAdmin
bean name.
- Default:
- ""
-
-
-
bindings
QueueBinding[] bindings
Array ofQueueBinding
s providing the listener's queue names, together with the exchange and optional binding information. Mutually exclusive withqueues()
andqueuesToDeclare()
.- Returns:
- the bindings.
- Since:
- 1.5
- See Also:
MessageListenerContainer
- Default:
- {}
-
-
-
group
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
String returnExceptions
Set to "true" to cause exceptions thrown by the listener to be sent to the sender using normalreplyTo/@SendTo
semantics. 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
RemoteInvocationAwareMessageConverterAdapter
the exception will be re-thrown. Otherwise, the sender will receive aRemoteInvocationResult
wrapping the exception. - Since:
- 2.0
- Default:
- ""
-
-
-
errorHandler
String errorHandler
Set anRabbitListenerErrorHandler
to 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 aRabbitListenerErrorHandler
instance.- Returns:
- the error handler.
- Since:
- 2.0
- Default:
- ""
-
-
-
concurrency
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
SimpleMessageListenerContainer
if this value is a simple integer, it sets a fixed number of consumers in theconcurrentConsumers
property. If it is a string with the form"m-n"
, theconcurrentConsumers
is set tom
and themaxConcurrentConsumers
is set ton
.For a
DirectMessageListenerContainer
it sets theconsumersPerQueue
property.- Returns:
- the concurrency.
- Since:
- 2.0
- Default:
- ""
-
-
-
autoStartup
String autoStartup
Set to true or false, to override the default setting in the container factory.- Returns:
- true to auto start, false to not auto start.
- Since:
- 2.0
- Default:
- ""
-
-
-
executor
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
String ackMode
Override the container factoryAcknowledgeMode
property. Must be one of the valid enumerations. If a SpEL expression is provided, it must evaluate to aString
orAcknowledgeMode
.- Returns:
- the acknowledgement mode.
- Since:
- 2.2
- Default:
- ""
-
-
-
replyPostProcessor
String replyPostProcessor
The bean name of aReplyPostProcessor
to 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
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
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 isMessage
orMessage
; set the content type message property or header respectively, in those cases.- Returns:
- the content type.
- Since:
- 2.3
- See Also:
converterWinsContentType()
- Default:
- ""
-
-
-
converterWinsContentType
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 theSimpleMessageConverter
use 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 isMessage
because there is no conversion involved. When returning aMessage
, set the content type message header andAmqpHeaders.CONTENT_TYPE_CONVERTER_WINS
to false.- Returns:
- false to use the replyContentType.
- Since:
- 2.3
- See Also:
replyContentType()
- Default:
- "true"
-
-