Class RabbitAmqpTemplate
java.lang.Object
org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate
- All Implemented Interfaces:
AsyncAmqpTemplate
,DisposableBean
,InitializingBean
public class RabbitAmqpTemplate
extends Object
implements AsyncAmqpTemplate, InitializingBean, 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 TypeMethodDescriptionvoid
convertAndSend
(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 routingKey, 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 routingKey, 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> convertSendAndReceive
(String exchange, 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, 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> convertSendAndReceiveAsType
(Object object, MessagePostProcessor messagePostProcessor, 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 routingKey, 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 routingKey, Object object, ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the default exchange with the provided routing key.<C> CompletableFuture
<C> convertSendAndReceiveAsType
(String exchange, 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, String routingKey, Object object, ParameterizedTypeReference<C> responseType) Convert the object to a message and send it to the provided exchange and routing key.void
destroy()
receive()
receiveAndConvert
(String queueName) <T> CompletableFuture
<T> receiveAndConvert
(String queueName, ParameterizedTypeReference<T> type) Receive a message fromsetDefaultReceiveQueue(String)
and convert its body to the expected type.<T> CompletableFuture
<T> Receive a message fromsetDefaultReceiveQueue(String)
and convert its body to the expected type.<R,
S> boolean receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback) <R,
S> boolean receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback, String replyExchange, String replyRoutingKey) <R,
S> boolean receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) <R,
S> boolean receiveAndReply
(ReceiveAndReplyCallback<R, S> callback) <R,
S> boolean receiveAndReply
(ReceiveAndReplyCallback<R, S> callback, String replyExchange, String replyRoutingKey) <R,
S> boolean receiveAndReply
(ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) 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, String routingKey, Message message) Send a message to the supplied exchange and routing key.sendAndReceive
(String routingKey, 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
setDefaultReceiveQueue
(String queue) The name of the default queue to receive messages from when none is specified explicitly.void
setExchange
(String exchange) Set a default exchange for publishing.void
Set a default routing key.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.
-
Constructor Details
-
RabbitAmqpTemplate
public RabbitAmqpTemplate(com.rabbitmq.client.amqp.Connection amqpConnection)
-
-
Method Details
-
setListeners
public void setListeners(com.rabbitmq.client.amqp.Resource.StateListener... listeners) -
setPublishTimeout
-
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
-
setKey
Set a default routing key. Mutually exclusive withsetQueue(String)
.- Parameters:
key
- the default routing key.
-
setQueue
Set default queue for publishing. Mutually exclusive withsetExchange(String)
andsetKey(String)
.- Parameters:
queue
- the default queue.
-
setMessageConverter
Set a converter forconvertAndSend(Object)
operations.- Parameters:
messageConverter
- the converter.
-
setDefaultReceiveQueue
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.
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
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
- Specified by:
receive
in interfaceAsyncAmqpTemplate
-
receiveAndConvert
- Specified by:
receiveAndConvert
in interfaceAsyncAmqpTemplate
-
receiveAndConvert
- Specified by:
receiveAndConvert
in interfaceAsyncAmqpTemplate
-
receiveAndConvert
Receive a message fromsetDefaultReceiveQueue(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, ParameterizedTypeReference<T> type) Receive a message fromsetDefaultReceiveQueue(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
- Throws:
AmqpException
-
receiveAndReply
public <R,S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback) throws AmqpException- Throws:
AmqpException
-
receiveAndReply
public <R,S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, String replyExchange, String replyRoutingKey) throws AmqpException- Throws:
AmqpException
-
receiveAndReply
public <R,S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback, String replyExchange, String replyRoutingKey) throws AmqpException- Throws:
AmqpException
-
receiveAndReply
public <R,S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException- Throws:
AmqpException
-
receiveAndReply
public <R,S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException- Throws:
AmqpException
-
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
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:
routingKey
- the routing key.message
- the message.- Returns:
- the
CompletableFuture
.
-
sendAndReceive
public CompletableFuture<Message> sendAndReceive(String exchange, 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
.
-
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:
routingKey
- the routing key.object
- the object to convert.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
public <C> CompletableFuture<C> convertSendAndReceive(String exchange, 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 routingKey, 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:
routingKey
- the routing key.object
- the object to convert.messagePostProcessor
- the post processor.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceive
public <C> CompletableFuture<C> convertSendAndReceive(String exchange, 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 routingKey, 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:
routingKey
- the routing key.object
- the object to convert.responseType
- the response type.- Returns:
- the
CompletableFuture
.
-
convertSendAndReceiveAsType
public <C> CompletableFuture<C> convertSendAndReceiveAsType(String exchange, 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, MessagePostProcessor messagePostProcessor, 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 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 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:
routingKey
- 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, 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
.
-