public class AsyncRabbitTemplate extends java.lang.Object implements org.springframework.context.SmartLifecycle, org.springframework.amqp.core.MessageListener, RabbitTemplate.ReturnCallback, RabbitTemplate.ConfirmCallback, org.springframework.beans.factory.BeanNameAware
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 a
SimpleMessageListenerContainer
either provided or constructed internally.
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.
Modifier and Type | Class and Description |
---|---|
class |
AsyncRabbitTemplate.RabbitConverterFuture<C>
A
AsyncRabbitTemplate.RabbitFuture with a return type of the template's
generic parameter. |
class |
AsyncRabbitTemplate.RabbitFuture<T>
Base class for
ListenableFuture s returned by AsyncRabbitTemplate . |
class |
AsyncRabbitTemplate.RabbitMessageFuture
A
AsyncRabbitTemplate.RabbitFuture with a return type of Message . |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_RECEIVE_TIMEOUT |
Constructor and Description |
---|
AsyncRabbitTemplate(ConnectionFactory connectionFactory,
java.lang.String exchange,
java.lang.String routingKey,
java.lang.String replyQueue)
Construct an instance using the provided arguments.
|
AsyncRabbitTemplate(ConnectionFactory connectionFactory,
java.lang.String exchange,
java.lang.String routingKey,
java.lang.String replyQueue,
java.lang.String replyAddress)
Construct an instance using the provided arguments.
|
AsyncRabbitTemplate(RabbitTemplate template,
SimpleMessageListenerContainer container)
Construct an instance using the provided arguments.
|
AsyncRabbitTemplate(RabbitTemplate template,
SimpleMessageListenerContainer container,
java.lang.String replyAddress)
Construct an instance using the provided arguments.
|
Modifier and Type | Method and Description |
---|---|
void |
confirm(CorrelationData correlationData,
boolean ack,
java.lang.String cause)
Confirmation callback.
|
<C> AsyncRabbitTemplate.RabbitConverterFuture<C> |
convertSendAndReceive(java.lang.Object message)
Convert the object to a message and send it to the default exchange with the
default routing key.
|
<C> AsyncRabbitTemplate.RabbitConverterFuture<C> |
convertSendAndReceive(java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
Convert the object to a message and send it to the default exchange with the
default routing key after invoking the
MessagePostProcessor . |
<C> AsyncRabbitTemplate.RabbitConverterFuture<C> |
convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message)
Convert the object to a message and send it to the default exchange with the
provided routing key.
|
<C> AsyncRabbitTemplate.RabbitConverterFuture<C> |
convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
Convert the object to a message and send it to the default exchange with the
provided routing key after invoking the
MessagePostProcessor . |
<C> AsyncRabbitTemplate.RabbitConverterFuture<C> |
convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message)
Convert the object to a message and send it to the provided exchange and
routing key.
|
<C> AsyncRabbitTemplate.RabbitConverterFuture<C> |
convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
Convert the object to a message and send it to the provided exchange and
routing key after invoking the
MessagePostProcessor . |
java.lang.String |
getBeanName() |
ConnectionFactory |
getConnectionFactory() |
org.springframework.amqp.support.converter.MessageConverter |
getMessageConverter() |
int |
getPhase() |
boolean |
isAutoStartup() |
boolean |
isRunning() |
void |
onMessage(org.springframework.amqp.core.Message message) |
void |
returnedMessage(org.springframework.amqp.core.Message message,
int replyCode,
java.lang.String replyText,
java.lang.String exchange,
java.lang.String routingKey)
Returned message callback.
|
AsyncRabbitTemplate.RabbitMessageFuture |
sendAndReceive(org.springframework.amqp.core.Message message)
Send a message to the default exchange with the default routing key.
|
AsyncRabbitTemplate.RabbitMessageFuture |
sendAndReceive(java.lang.String routingKey,
org.springframework.amqp.core.Message message)
Send a message to the default exchange with the supplied routing key.
|
AsyncRabbitTemplate.RabbitMessageFuture |
sendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
Send a message to the supplied exchange and routing key.
|
void |
setAutoStartup(boolean autoStartup) |
void |
setBeanName(java.lang.String beanName) |
void |
setCharset(java.nio.charset.Charset charset)
Deprecated.
- will be removed in 2.0 when byte[] correlation id is removed
|
void |
setEnableConfirms(boolean enableConfirms)
Set to true to enable publisher confirms.
|
void |
setMandatory(boolean mandatory)
Set to true to enable the receipt of returned messages that cannot be delivered
in the form of a
AmqpMessageReturnedException . |
void |
setPhase(int phase) |
void |
setReceiveTimeout(long receiveTimeout)
Set the receive timeout - the future returned by the send and receive
methods will be canceled when this timeout expires.
|
void |
setTaskScheduler(org.springframework.scheduling.TaskScheduler taskScheduler)
Set the task scheduler to expire timed out futures.
|
void |
start() |
void |
stop() |
void |
stop(java.lang.Runnable callback) |
java.lang.String |
toString() |
public static final int DEFAULT_RECEIVE_TIMEOUT
public AsyncRabbitTemplate(ConnectionFactory connectionFactory, java.lang.String exchange, java.lang.String routingKey, java.lang.String replyQueue)
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.public AsyncRabbitTemplate(ConnectionFactory connectionFactory, java.lang.String exchange, java.lang.String routingKey, java.lang.String replyQueue, java.lang.String replyAddress)
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).public AsyncRabbitTemplate(RabbitTemplate template, SimpleMessageListenerContainer container)
template
- a RabbitTemplate
container
- a SimpleMessageListenerContainer
.public AsyncRabbitTemplate(RabbitTemplate template, SimpleMessageListenerContainer container, java.lang.String replyAddress)
template
- a RabbitTemplate
.container
- a SimpleMessageListenerContainer
.replyAddress
- the reply address.public void setAutoStartup(boolean autoStartup)
autoStartup
- true for auto start.isAutoStartup()
public void setPhase(int phase)
phase
- the phase.getPhase()
public void setMandatory(boolean mandatory)
AmqpMessageReturnedException
.mandatory
- true to enable returns.public void setEnableConfirms(boolean enableConfirms)
AsyncRabbitTemplate.RabbitFuture
returned by the send and receive operation will have a
ListenableFuture<Boolean>
in its confirm
property.enableConfirms
- true to enable publisher confirms.@Deprecated public void setCharset(java.nio.charset.Charset charset)
charset
- the charset.public java.lang.String getBeanName()
public void setBeanName(java.lang.String beanName)
setBeanName
in interface org.springframework.beans.factory.BeanNameAware
public ConnectionFactory getConnectionFactory()
RabbitTemplate
.public void setReceiveTimeout(long receiveTimeout)
<= 0
means
futures never expire. Beware that this will cause a memory leak if a
reply is not received. Default: 30000 (30 seconds).receiveTimeout
- the timeout in milliseconds.public void setTaskScheduler(org.springframework.scheduling.TaskScheduler taskScheduler)
taskScheduler
- the task schedulersetReceiveTimeout(long)
public org.springframework.amqp.support.converter.MessageConverter getMessageConverter()
RabbitTemplate
's
MessageConverter
.public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive(org.springframework.amqp.core.Message message)
message
- the message.AsyncRabbitTemplate.RabbitMessageFuture
.public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive(java.lang.String routingKey, org.springframework.amqp.core.Message message)
routingKey
- the routing key.message
- the message.AsyncRabbitTemplate.RabbitMessageFuture
.public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(java.lang.Object message)
C
- the expected result type.message
- the message.AsyncRabbitTemplate.RabbitConverterFuture
.public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(java.lang.String routingKey, java.lang.Object message) throws org.springframework.amqp.AmqpException
C
- the expected result type.routingKey
- the routing key.message
- the message.AsyncRabbitTemplate.RabbitConverterFuture
.org.springframework.amqp.AmqpException
public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object message)
C
- the expected result type.exchange
- the exchange.routingKey
- the routing key.message
- the message.AsyncRabbitTemplate.RabbitConverterFuture
.public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(java.lang.Object message, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
MessagePostProcessor
.
If the post processor adds a correlationId property, it must be unique.C
- the expected result type.message
- the message.messagePostProcessor
- the post processor.AsyncRabbitTemplate.RabbitConverterFuture
.public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(java.lang.String routingKey, java.lang.Object message, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
MessagePostProcessor
.
If the post processor adds a correlationId property, it must be unique.C
- the expected result type.routingKey
- the routing key.message
- the message.messagePostProcessor
- the post processor.AsyncRabbitTemplate.RabbitConverterFuture
.public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive(java.lang.String exchange, java.lang.String routingKey, org.springframework.amqp.core.Message message)
exchange
- the exchange.routingKey
- the routing key.message
- the message.AsyncRabbitTemplate.RabbitMessageFuture
.public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object message, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
MessagePostProcessor
.
If the post processor adds a correlationId property, it must be unique.C
- the expected result type.exchange
- the exchangeroutingKey
- the routing key.message
- the message.messagePostProcessor
- the post processor.AsyncRabbitTemplate.RabbitConverterFuture
.public void start()
start
in interface org.springframework.context.Lifecycle
public void stop()
stop
in interface org.springframework.context.Lifecycle
public boolean isRunning()
isRunning
in interface org.springframework.context.Lifecycle
public int getPhase()
getPhase
in interface org.springframework.context.Phased
public boolean isAutoStartup()
isAutoStartup
in interface org.springframework.context.SmartLifecycle
public void stop(java.lang.Runnable callback)
stop
in interface org.springframework.context.SmartLifecycle
public void onMessage(org.springframework.amqp.core.Message message)
onMessage
in interface org.springframework.amqp.core.MessageListener
public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, java.lang.String replyText, java.lang.String exchange, java.lang.String routingKey)
RabbitTemplate.ReturnCallback
returnedMessage
in interface RabbitTemplate.ReturnCallback
message
- the returned message.replyCode
- the reply code.replyText
- the reply text.exchange
- the exchange.routingKey
- the routing key.public void confirm(CorrelationData correlationData, boolean ack, java.lang.String cause)
RabbitTemplate.ConfirmCallback
confirm
in interface RabbitTemplate.ConfirmCallback
correlationData
- correlation data for the callback.ack
- true for ack, false for nackcause
- An optional cause, for nack, when available, otherwise null.public java.lang.String toString()
toString
in class java.lang.Object