Annotation Interface RabbitListener
queues()
(or bindings()
). The containerFactory()
identifies the
RabbitListenerContainerFactory
to use to build the rabbit listener container. If not
set, a default container factory is assumed to be available with a bean name
of rabbitListenerContainerFactory
unless an explicit default has been provided
through configuration.
Processing of @RabbitListener
annotations is performed by registering a
RabbitListenerAnnotationBeanPostProcessor
. This can be done manually or, more
conveniently, through the <rabbit:annotation-driven/>
element or
EnableRabbit
annotation.
Annotated methods are allowed to have flexible signatures similar to what
MessageMapping
provides, that is
Channel
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 the
ReplyTo
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 a
RabbitAdmin
, 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 a
MessagingMessageListenerAdapter
,
configured with a
MethodRabbitListenerEndpoint
.
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. The
MessagingMessageListenerAdapter
is configured with a
MultiMethodRabbitListenerEndpoint
.
- Since:
- 1.4
- Author:
- Stephane Nicoll, Gary Russell
- See Also:
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionOverride the container factoryAcknowledgeMode
property.Reference to aAmqpAdmin
.Set to true or false, to override the default setting in the container factory.Array ofQueueBinding
s providing the listener's queue names, together with the exchange and optional binding information.Set the concurrency of the listener container for this listener.The bean name of theRabbitListenerContainerFactory
to use to create the message listener container responsible to serve this endpoint.Set to 'false' to override any content type headers set by the message converter with the value of the 'replyContentType' property.Set anRabbitListenerErrorHandler
to invoke if the listener method throws an exception.boolean
Whentrue
, a single consumer in the container will have exclusive use of thequeues()
, preventing other consumers from receiving messages from the queues.Set the task executor bean name to use for this listener's container; overrides any executor set on the container factory.If provided, the listener container for this listener will be added to a bean with this value as its name, of typeCollection<MessageListenerContainer>
.The unique identifier of the container managing for this endpoint.Override the container factory's message converter used for this listener.The priority of this endpoint.String[]
The queues for this listener.Queue[]
The queues for this listener.Used to set the content type of a reply message.The bean name of aReplyPostProcessor
to post process a response before it is sent.Set to "true" to cause exceptions thrown by the listener to be sent to the sender using normalreplyTo/@SendTo
semantics.
-
Element Details
-
id
String idThe 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:
- Default:
- ""
-
containerFactory
String containerFactoryThe 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[] queuesThe 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:
- Default:
- {}
-
queuesToDeclare
Queue[] queuesToDeclareThe 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:
- Default:
- {}
-
exclusive
boolean exclusiveWhentrue
, 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 priorityThe 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 adminReference 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[] bindingsArray 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:
- Default:
- {}
-
group
String groupIf 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 returnExceptionsSet 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 errorHandlerSet 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 concurrencySet 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 autoStartupSet 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 executorSet 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 ackModeOverride 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 replyPostProcessorThe 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:
- Default:
- ""
-
messageConverter
String messageConverterOverride 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 replyContentTypeUsed 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:
- Default:
- ""
-
converterWinsContentType
String converterWinsContentTypeSet 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:
- Default:
- "true"
-