Class RabbitAmqpTemplate
java.lang.Object
org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate
- All Implemented Interfaces:
AsyncAmqpTemplate
,DisposableBean
The
AmqpTemplate
for RabbitMQ AMQP 1.0 protocol support.
A Spring-friendly wrapper around Environment.connectionBuilder()
;- Since:
- 4.0
- Author:
- Artem Bilan
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconvertAndSend
(Object message) Publish a message from converted body to the default exchange and routing key (if any) (or queue) configured on this template.convertAndSend
(Object message, @Nullable MessagePostProcessor messagePostProcessor) convertAndSend
(String exchange, @Nullable String routingKey, Object message) convertAndSend
(String exchange, @Nullable String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor) convertAndSend
(String queue, Object message) convertAndSend
(String queue, Object message, @Nullable MessagePostProcessor messagePostProcessor) <C> CompletableFuture
<C> convertSendAndReceive
(Object object) Convert the object to a message and send it to the default exchange with the default routing key.<C> CompletableFuture
<C> convertSendAndReceive
(Object object, MessagePostProcessor messagePostProcessor) Convert the object to a message and send it to the default exchange with the default routing key after invoking theMessagePostProcessor
.<C> CompletableFuture
<C> convertSendAndReceive
(String exchange, @Nullable String routingKey, Object object) Convert the object to a message and send it to the provided exchange and routing key.<C> CompletableFuture
<C> convertSendAndReceive
(String exchange, @Nullable String routingKey, Object object, @Nullable MessagePostProcessor messagePostProcessor) Convert the object to a message and send it to the provided exchange and routing key after invoking theMessagePostProcessor
.<C> CompletableFuture
<C> convertSendAndReceive
(String queue, Object object) Convert the object to a message and send it to the default exchange with the provided routing key.<C> CompletableFuture
<C> convertSendAndReceive
(String queue, Object object, MessagePostProcessor messagePostProcessor) Convert the object to a message and send it to the default exchange with the provided routing key after invoking theMessagePostProcessor
.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(Object object, @Nullable MessagePostProcessor messagePostProcessor, @Nullable ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the default exchange with the default routing key after invoking theMessagePostProcessor
.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(Object object, ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the default exchange with the default routing key.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(String exchange, @Nullable String routingKey, Object object, @Nullable MessagePostProcessor messagePostProcessor, @Nullable ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the provided exchange and routing key after invoking theMessagePostProcessor
.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(String exchange, @Nullable String routingKey, Object object, ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the provided exchange and routing key.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(String queue, Object object, @Nullable MessagePostProcessor messagePostProcessor, @Nullable ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the default exchange with the provided routing key after invoking theMessagePostProcessor
.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(String queue, Object object, ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the default exchange with the provided routing key.void
destroy()
receive()
Request a head message from the provided queue.receiveAndConvert
(String queueName) <T> CompletableFuture
<T> receiveAndConvert
(String queueName, @Nullable ParameterizedTypeReference<T> type) Receive a message fromsetReceiveQueue(String)
and convert its body to the expected type.<T> CompletableFuture
<T> receiveAndConvert
(@Nullable ParameterizedTypeReference<T> type) Receive a message fromsetReceiveQueue(String)
and convert its body to the expected type.<R,
S> CompletableFuture <Boolean> receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback) Perform a server-side RPC functionality.<R,
S> CompletableFuture <Boolean> receiveAndReply
(ReceiveAndReplyCallback<R, S> callback) Publish the message to the provided queue.Publish a message to the default exchange and routing key (if any) (or queue) configured on this template.sendAndReceive
(String exchange, @Nullable String routingKey, Message message) Send a message to the supplied exchange and routing key.sendAndReceive
(String queue, Message message) Send a message to the default exchange with the supplied routing key.sendAndReceive
(Message message) Send a message to the default exchange with the default routing key.void
setCompletionTimeout
(Duration completionTimeout) Set a duration forCompletableFuture.orTimeout(long, TimeUnit)
on returns.void
setExchange
(String exchange) Set a default exchange for publishing.void
setListeners
(com.rabbitmq.client.amqp.Resource.StateListener... listeners) void
setMessageConverter
(MessageConverter messageConverter) Set a converter forconvertAndSend(Object)
operations.void
setPublishTimeout
(Duration timeout) void
Set default queue for publishing.void
setReceiveQueue
(String queue) The name of the default queue to receive messages from when none is specified explicitly.void
setReplyToQueue
(String queue) The name of the default queue to receive replies from when none is specified explicitly.void
setRoutingKey
(String routingKey) Set a default routing key.
-
Constructor Details
-
RabbitAmqpTemplate
-
-
Method Details
-
setListeners
public void setListeners(com.rabbitmq.client.amqp.Resource.StateListener... listeners) -
setPublishTimeout
-
setCompletionTimeout
Set a duration forCompletableFuture.orTimeout(long, TimeUnit)
on returns. There is noCompletableFuture
API likeonTimeout()
requested from theCompletableFuture.get(long, TimeUnit)
, but used in operations AMQP resources have to be closed eventually independently of theCompletableFuture
fulfilment. Defaults to 1 minute.- Parameters:
completionTimeout
- duration for future completions.
-
setExchange
Set a default exchange for publishing. Cannot be real default AMQP exchange. ThesetQueue(String)
is recommended instead. Mutually exclusive withsetQueue(String)
.- Parameters:
exchange
- the default exchange
-
setRoutingKey
Set a default routing key. Mutually exclusive withsetQueue(String)
.- Parameters:
routingKey
- the default routing key.
-
setQueue
Set default queue for publishing. Mutually exclusive withsetExchange(String)
andsetRoutingKey(String)
.- Parameters:
queue
- the default queue.
-
setReceiveQueue
The name of the default queue to receive messages from when none is specified explicitly.- Parameters:
queue
- the default queue name to use for receive operation.
-
setReplyToQueue
The name of the default queue to receive replies from when none is specified explicitly.- Parameters:
queue
- the default queue name to use for send-n-receive operation.
-
setMessageConverter
Set a converter forconvertAndSend(Object)
operations.- Parameters:
messageConverter
- the converter.
-
destroy
public void destroy()- Specified by:
destroy
in interfaceDisposableBean
-
send
Publish a message to the default exchange and routing key (if any) (or queue) configured on this template.- Specified by:
send
in interfaceAsyncAmqpTemplate
- Parameters:
message
- to publish- Returns:
- the
CompletableFuture
as an async result of the message publication.
-
send
Publish the message to the provided queue.- Specified by:
send
in interfaceAsyncAmqpTemplate
- Parameters:
queue
- to publishmessage
- to publish- Returns:
- the
CompletableFuture
as an async result of the message publication.
-
send
public CompletableFuture<Boolean> send(String exchange, @Nullable String routingKey, Message message) - Specified by:
send
in interfaceAsyncAmqpTemplate
-
convertAndSend
Publish a message from converted body to the default exchange and routing key (if any) (or queue) configured on this template.- Specified by:
convertAndSend
in interfaceAsyncAmqpTemplate
- Parameters:
message
- to publish- Returns:
- the
CompletableFuture
as an async result of the message publication.
-
convertAndSend
- Specified by:
convertAndSend
in interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(String exchange, @Nullable String routingKey, Object message) - Specified by:
convertAndSend
in interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(Object message, @Nullable MessagePostProcessor messagePostProcessor) - Specified by:
convertAndSend
in interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(String queue, Object message, @Nullable MessagePostProcessor messagePostProcessor) - Specified by:
convertAndSend
in interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(String exchange, @Nullable String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor) - Specified by:
convertAndSend
in interfaceAsyncAmqpTemplate
-
receive
- Specified by:
receive
in interfaceAsyncAmqpTemplate
-
receive
Request a head message from the provided queue. A returnedCompletableFuture
timeouts aftersetCompletionTimeout(Duration)
.- Specified by:
receive
in interfaceAsyncAmqpTemplate
- Parameters:
queueName
- the queue to consume message from.- Returns:
- the future with a received message.
- See Also:
-
receiveAndConvert
- Specified by:
receiveAndConvert
in interfaceAsyncAmqpTemplate
-
receiveAndConvert
- Specified by:
receiveAndConvert
in interfaceAsyncAmqpTemplate
-
receiveAndConvert
Receive a message fromsetReceiveQueue(String)
and convert its body to the expected type. ThesetMessageConverter(MessageConverter)
must be an implementation ofSmartMessageConverter
.- Specified by:
receiveAndConvert
in interfaceAsyncAmqpTemplate
- Parameters:
type
- the type to covert received result.- Returns:
- the CompletableFuture with a result.
-
receiveAndConvert
public <T> CompletableFuture<T> receiveAndConvert(String queueName, @Nullable ParameterizedTypeReference<T> type) Receive a message fromsetReceiveQueue(String)
and convert its body to the expected type. ThesetMessageConverter(MessageConverter)
must be an implementation ofSmartMessageConverter
.- Specified by:
receiveAndConvert
in interfaceAsyncAmqpTemplate
- Parameters:
queueName
- the queue to consume message from.type
- the type to covert received result.- Returns:
- the CompletableFuture with a result.
-
receiveAndReply
- Specified by:
receiveAndReply
in interfaceAsyncAmqpTemplate
-
receiveAndReply
public <R,S> CompletableFuture<Boolean> receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback) Description copied from interface:AsyncAmqpTemplate
Perform a server-side RPC functionality. The request message must have areplyTo
property. The requestmessageId
property is used for correlation. The callback might not produce a reply with the meaning nothing to answer.- Specified by:
receiveAndReply
in interfaceAsyncAmqpTemplate
- Type Parameters:
R
- the request body type.S
- the response body type- Parameters:
queueName
- the queue to consume request.callback
- an application callback to handle request and produce reply.- Returns:
- the completion status: true if no errors, and reply has been produced.
-
sendAndReceive
Description copied from interface:AsyncAmqpTemplate
Send a message to the default exchange with the default routing key. If the message contains a correlationId property, it must be unique.- Specified by:
sendAndReceive
in interfaceAsyncAmqpTemplate
- Parameters:
message
- the message.- Returns:
- the
CompletableFuture
.
-
sendAndReceive
public CompletableFuture<Message> sendAndReceive(String exchange, @Nullable String routingKey, Message message) Description copied from interface:AsyncAmqpTemplate
Send a message to the supplied exchange and routing key. If the message contains a correlationId property, it must be unique.- Specified by:
sendAndReceive
in interfaceAsyncAmqpTemplate
- Parameters:
exchange
- the exchange.routingKey
- the routing key.message
- the message.- Returns:
- the
CompletableFuture
.
-
sendAndReceive
Description copied from interface:AsyncAmqpTemplate
Send a message to the default exchange with the supplied routing key. If the message contains a correlationId property, it must be unique.- Specified by:
sendAndReceive
in interfaceAsyncAmqpTemplate
- Parameters:
queue
- the routing key.message
- the message.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the default routing key.- Specified by:
convertSendAndReceive
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
object
- the object to convert.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the provided routing key.- Specified by:
convertSendAndReceive
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
queue
- the routing key.object
- the object to convert.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
public <C> CompletableFuture<C> convertSendAndReceive(String exchange, @Nullable String routingKey, Object object) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the provided exchange and routing key.- Specified by:
convertSendAndReceive
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
exchange
- the exchange.routingKey
- the routing key.object
- the object to convert.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
public <C> CompletableFuture<C> convertSendAndReceive(Object object, MessagePostProcessor messagePostProcessor) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the default routing key after invoking theMessagePostProcessor
. If the post-processor adds a correlationId property, it must be unique.- Specified by:
convertSendAndReceive
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
object
- the object to convert.messagePostProcessor
- the post-processor.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
public <C> CompletableFuture<C> convertSendAndReceive(String queue, Object object, MessagePostProcessor messagePostProcessor) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the provided routing key after invoking theMessagePostProcessor
. If the post-processor adds a correlationId property, it must be unique.- Specified by:
convertSendAndReceive
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
queue
- the routing key.object
- the object to convert.messagePostProcessor
- the post-processor.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
public <C> CompletableFuture<C> convertSendAndReceive(String exchange, @Nullable String routingKey, Object object, @Nullable MessagePostProcessor messagePostProcessor) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the provided exchange and routing key after invoking theMessagePostProcessor
. If the post-processor adds a correlationId property, it must be unique.- Specified by:
convertSendAndReceive
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
exchange
- the exchangeroutingKey
- the routing key.object
- the object to convert.messagePostProcessor
- the post-processor.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(Object object, ParameterizedTypeReference<C> responseType) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the default routing key.- Specified by:
convertSendAndReceiveAsType
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
object
- the object to convert.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(String queue, Object object, ParameterizedTypeReference<C> responseType) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the provided routing key.- Specified by:
convertSendAndReceiveAsType
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
queue
- the routing key.object
- the object to convert.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(String exchange, @Nullable String routingKey, Object object, ParameterizedTypeReference<C> responseType) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the provided exchange and routing key.- Specified by:
convertSendAndReceiveAsType
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
exchange
- the exchange.routingKey
- the routing key.object
- the object to convert.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(Object object, @Nullable MessagePostProcessor messagePostProcessor, @Nullable ParameterizedTypeReference<C> responseType) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the default routing key after invoking theMessagePostProcessor
. If the post-processor adds a correlationId property, it must be unique.- Specified by:
convertSendAndReceiveAsType
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
object
- the object to convert.messagePostProcessor
- the post-processor.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(String queue, Object object, @Nullable MessagePostProcessor messagePostProcessor, @Nullable ParameterizedTypeReference<C> responseType) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the default exchange with the provided routing key after invoking theMessagePostProcessor
. If the post-processor adds a correlationId property, it must be unique.- Specified by:
convertSendAndReceiveAsType
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
queue
- the routing key.object
- the object to convert.messagePostProcessor
- the post-processor.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(String exchange, @Nullable String routingKey, Object object, @Nullable MessagePostProcessor messagePostProcessor, @Nullable ParameterizedTypeReference<C> responseType) Description copied from interface:AsyncAmqpTemplate
Convert the object to a message and send it to the provided exchange and routing key after invoking theMessagePostProcessor
. If the post-processor adds a correlationId property, it must be unique.- Specified by:
convertSendAndReceiveAsType
in interfaceAsyncAmqpTemplate
- Type Parameters:
C
- the expected result type.- Parameters:
exchange
- the exchangeroutingKey
- the routing key.object
- the object to convert.messagePostProcessor
- the post-processor.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-