Class AsyncRabbitTemplate
- All Implemented Interfaces:
org.springframework.amqp.core.AsyncAmqpTemplate,org.springframework.amqp.core.MessageListener,RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback,RabbitTemplate.ReturnsCallback,ChannelAwareMessageListener,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.context.Lifecycle,org.springframework.context.Phased,org.springframework.context.SmartLifecycle
ListenableFuture
allowing the caller to obtain the reply later, using get() or a callback.
When confirms are enabled, the future has a confirm property which is itself a
ListenableFuture. If the reply is received before the publisher confirm,
the confirm is discarded since the reply implicitly indicates the message was
published.
Returned (undeliverable) request messages are presented as a
AmqpMessageReturnedException cause of an
ExecutionException.
Internally, the template uses a RabbitTemplate and an
AbstractMessageListenerContainer either provided or constructed internally
(a SimpleMessageListenerContainer).
If an external RabbitTemplate is provided and confirms/returns are enabled,
it must not previously have had callbacks registered because this object needs to
be the callback.
- Since:
- 1.6
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassAAsyncRabbitTemplate.RabbitFuturewith a return type of the template's generic parameter.classBase class forListenableFutures returned byAsyncRabbitTemplate.classAAsyncRabbitTemplate.RabbitFuturewith a return type ofMessage. -
Field Summary
FieldsFields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionAsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey) Construct an instance using the provided arguments.AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey, String replyQueue) Construct an instance using the provided arguments.AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey, String replyQueue, String replyAddress) Construct an instance using the provided arguments.AsyncRabbitTemplate(RabbitTemplate template) Construct an instance using the provided arguments.AsyncRabbitTemplate(RabbitTemplate template, AbstractMessageListenerContainer container) Construct an instance using the provided arguments.AsyncRabbitTemplate(RabbitTemplate template, AbstractMessageListenerContainer container, String replyAddress) Construct an instance using the provided arguments. -
Method Summary
Modifier and TypeMethodDescriptionvoidconfirm(CorrelationData correlationData, boolean ack, String cause) Confirmation callback.convertSendAndReceive(Object object) convertSendAndReceive(Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor) convertSendAndReceive(String routingKey, Object object) convertSendAndReceive(String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor) convertSendAndReceive(String exchange, String routingKey, Object object) convertSendAndReceive(String exchange, String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor) convertSendAndReceiveAsType(Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, org.springframework.core.ParameterizedTypeReference<C> responseType) convertSendAndReceiveAsType(Object object, org.springframework.core.ParameterizedTypeReference<C> responseType) convertSendAndReceiveAsType(String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, org.springframework.core.ParameterizedTypeReference<C> responseType) convertSendAndReceiveAsType(String routingKey, Object object, org.springframework.core.ParameterizedTypeReference<C> responseType) convertSendAndReceiveAsType(String exchange, String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, org.springframework.core.ParameterizedTypeReference<C> responseType) convertSendAndReceiveAsType(String exchange, String routingKey, Object object, org.springframework.core.ParameterizedTypeReference<C> responseType) org.springframework.amqp.support.converter.MessageConverterintgetPhase()Return the underlyingRabbitTemplateused for sending.booleanbooleanvoidonMessage(org.springframework.amqp.core.Message message, com.rabbitmq.client.Channel channel) Callback for processing a received Rabbit message.voidreturnedMessage(org.springframework.amqp.core.ReturnedMessage returned) Returned message callback.sendAndReceive(String exchange, String routingKey, org.springframework.amqp.core.Message message) sendAndReceive(String routingKey, org.springframework.amqp.core.Message message) sendAndReceive(org.springframework.amqp.core.Message message) voidsetAutoStartup(boolean autoStartup) voidsetBeanName(String beanName) voidsetEnableConfirms(boolean enableConfirms) Set to true to enable publisher confirms.voidsetMandatory(boolean mandatory) Set to true to enable the receipt of returned messages that cannot be delivered in the form of aAmqpMessageReturnedException.voidsetMandatoryExpression(org.springframework.expression.Expression mandatoryExpression) voidsetMandatoryExpressionString(String mandatoryExpression) voidsetPhase(int phase) voidsetReceiveTimeout(long receiveTimeout) Set the receive timeout - the future returned by the send and receive methods will be canceled when this timeout expires.voidsetTaskScheduler(org.springframework.scheduling.TaskScheduler taskScheduler) Set the task scheduler to expire timed out futures.voidstart()voidstop()toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener
onMessage, onMessageBatchMethods inherited from interface org.springframework.amqp.core.MessageListener
containerAckMode, isAsyncReplies, onMessageBatchMethods inherited from interface org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnsCallback
delegate, returnedMessageMethods inherited from interface org.springframework.context.SmartLifecycle
stop
-
Field Details
-
DEFAULT_RECEIVE_TIMEOUT
public static final int DEFAULT_RECEIVE_TIMEOUT- See Also:
-
-
Constructor Details
-
AsyncRabbitTemplate
public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey, String replyQueue) Construct an instance using the provided arguments. Replies will be routed to the default exchange using the reply queue name as the routing key.- Parameters:
connectionFactory- the connection factory.exchange- the default exchange to which requests will be sent.routingKey- the default routing key.replyQueue- the name of the reply queue to listen for replies.
-
AsyncRabbitTemplate
public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey, String replyQueue, String replyAddress) Construct an instance using the provided arguments. If 'replyAddress' is null, replies will be routed to the default exchange using the reply queue name as the routing key. Otherwise it should have the form exchange/routingKey and must cause messages to be routed to the reply queue.- Parameters:
connectionFactory- the connection factory.exchange- the default exchange to which requests will be sent.routingKey- the default routing key.replyQueue- the name of the reply queue to listen for replies.replyAddress- the reply address (exchange/routingKey).
-
AsyncRabbitTemplate
Construct an instance using the provided arguments. The first queue the container is configured to listen to will be used as the reply queue. Replies will be routed using the default exchange with that queue name as the routing key.- Parameters:
template- aRabbitTemplatecontainer- aAbstractMessageListenerContainer.
-
AsyncRabbitTemplate
public AsyncRabbitTemplate(RabbitTemplate template, AbstractMessageListenerContainer container, String replyAddress) Construct an instance using the provided arguments. The first queue the container is configured to listen to will be used as the reply queue. If 'replyAddress' is null, replies will be routed using the default exchange with that queue name as the routing key. Otherwise it should have the form exchange/routingKey and must cause messages to be routed to the reply queue.- Parameters:
template- aRabbitTemplate.container- aAbstractMessageListenerContainer.replyAddress- the reply address.
-
AsyncRabbitTemplate
Construct an instance using the provided arguments. "Direct replyTo" is used for replies.- Parameters:
connectionFactory- the connection factory.exchange- the default exchange to which requests will be sent.routingKey- the default routing key.- Since:
- 2.0
-
AsyncRabbitTemplate
Construct an instance using the provided arguments. "Direct replyTo" is used for replies.- Parameters:
template- aRabbitTemplate- Since:
- 2.0
-
-
Method Details
-
setAutoStartup
public void setAutoStartup(boolean autoStartup) - Parameters:
autoStartup- true for auto start.- See Also:
-
setPhase
public void setPhase(int phase) - Parameters:
phase- the phase.- See Also:
-
setMandatory
public void setMandatory(boolean mandatory) Set to true to enable the receipt of returned messages that cannot be delivered in the form of aAmqpMessageReturnedException.- Parameters:
mandatory- true to enable returns.
-
setMandatoryExpression
public void setMandatoryExpression(org.springframework.expression.Expression mandatoryExpression) - Parameters:
mandatoryExpression- a SpELExpressionto evaluate against each request message. The result of the evaluation must be abooleanvalue.- Since:
- 2.0
-
setMandatoryExpressionString
- Parameters:
mandatoryExpression- a SpELExpressionto evaluate against each request message. The result of the evaluation must be abooleanvalue.- Since:
- 2.0
-
setEnableConfirms
public void setEnableConfirms(boolean enableConfirms) Set to true to enable publisher confirms. When enabled, theAsyncRabbitTemplate.RabbitFuturereturned by the send and receive operation will have aListenableFuture<Boolean>in itsconfirmproperty.- Parameters:
enableConfirms- true to enable publisher confirms.
-
getBeanName
-
setBeanName
- Specified by:
setBeanNamein interfaceorg.springframework.beans.factory.BeanNameAware
-
getConnectionFactory
- Returns:
- a reference to the underlying connection factory in the
RabbitTemplate.
-
setReceiveTimeout
public void setReceiveTimeout(long receiveTimeout) Set the receive timeout - the future returned by the send and receive methods will be canceled when this timeout expires.<= 0means futures never expire. Beware that this will cause a memory leak if a reply is not received. Default: 30000 (30 seconds).- Parameters:
receiveTimeout- the timeout in milliseconds.
-
setTaskScheduler
public void setTaskScheduler(org.springframework.scheduling.TaskScheduler taskScheduler) Set the task scheduler to expire timed out futures.- Parameters:
taskScheduler- the task scheduler- See Also:
-
getMessageConverter
public org.springframework.amqp.support.converter.MessageConverter getMessageConverter()- Returns:
- a reference to the underlying
RabbitTemplate'sMessageConverter.
-
getRabbitTemplate
Return the underlyingRabbitTemplateused for sending.- Returns:
- the template.
- Since:
- 2.2
-
sendAndReceive
public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive(org.springframework.amqp.core.Message message) - Specified by:
sendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
sendAndReceive
public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive(String routingKey, org.springframework.amqp.core.Message message) - Specified by:
sendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
sendAndReceive
public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive(String exchange, String routingKey, org.springframework.amqp.core.Message message) - Specified by:
sendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceive
- Specified by:
convertSendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceive
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(String routingKey, Object object) - Specified by:
convertSendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceive
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(String exchange, String routingKey, Object object) - Specified by:
convertSendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceive
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor) - Specified by:
convertSendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceive
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor) - Specified by:
convertSendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceive
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(String exchange, String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor) - Specified by:
convertSendAndReceivein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceiveAsType
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType(Object object, org.springframework.core.ParameterizedTypeReference<C> responseType) - Specified by:
convertSendAndReceiveAsTypein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceiveAsType
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType(String routingKey, Object object, org.springframework.core.ParameterizedTypeReference<C> responseType) - Specified by:
convertSendAndReceiveAsTypein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceiveAsType
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType(String exchange, String routingKey, Object object, org.springframework.core.ParameterizedTypeReference<C> responseType) - Specified by:
convertSendAndReceiveAsTypein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceiveAsType
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType(Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, org.springframework.core.ParameterizedTypeReference<C> responseType) - Specified by:
convertSendAndReceiveAsTypein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceiveAsType
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType(String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, org.springframework.core.ParameterizedTypeReference<C> responseType) - Specified by:
convertSendAndReceiveAsTypein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
convertSendAndReceiveAsType
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType(String exchange, String routingKey, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, org.springframework.core.ParameterizedTypeReference<C> responseType) - Specified by:
convertSendAndReceiveAsTypein interfaceorg.springframework.amqp.core.AsyncAmqpTemplate
-
start
public void start()- Specified by:
startin interfaceorg.springframework.context.Lifecycle
-
stop
public void stop()- Specified by:
stopin interfaceorg.springframework.context.Lifecycle
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceorg.springframework.context.Lifecycle
-
getPhase
public int getPhase()- Specified by:
getPhasein interfaceorg.springframework.context.Phased- Specified by:
getPhasein interfaceorg.springframework.context.SmartLifecycle
-
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartupin interfaceorg.springframework.context.SmartLifecycle
-
onMessage
public void onMessage(org.springframework.amqp.core.Message message, com.rabbitmq.client.Channel channel) Description copied from interface:ChannelAwareMessageListenerCallback for processing a received Rabbit message.Implementors are supposed to process the given Message, typically sending reply messages through the given Session.
- Specified by:
onMessagein interfaceChannelAwareMessageListener- Parameters:
message- the received AMQP message (nevernull)channel- the underlying Rabbit Channel (nevernullunless called by the stream listener container).
-
returnedMessage
public void returnedMessage(org.springframework.amqp.core.ReturnedMessage returned) Description copied from interface:RabbitTemplate.ReturnsCallbackReturned message callback.- Specified by:
returnedMessagein interfaceRabbitTemplate.ReturnCallback- Specified by:
returnedMessagein interfaceRabbitTemplate.ReturnsCallback- Parameters:
returned- the returned message and metadata.
-
confirm
Description copied from interface:RabbitTemplate.ConfirmCallbackConfirmation callback.- Specified by:
confirmin interfaceRabbitTemplate.ConfirmCallback- Parameters:
correlationData- correlation data for the callback.ack- true for ack, false for nackcause- An optional cause, for nack, when available, otherwise null.
-
toString
-