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.voiddestroy()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.voidsetCompletionTimeout(Duration completionTimeout) Set a duration forCompletableFuture.orTimeout(long, TimeUnit)on returns.voidsetExchange(String exchange) Set a default exchange for publishing.voidsetListeners(com.rabbitmq.client.amqp.Resource.StateListener... listeners) voidsetMessageConverter(MessageConverter messageConverter) Set a converter forconvertAndSend(Object)operations.voidsetPublishTimeout(Duration timeout) voidSet default queue for publishing.voidsetReceiveQueue(String queue) The name of the default queue to receive messages from when none is specified explicitly.voidsetReplyToQueue(String queue) The name of the default queue to receive replies from when none is specified explicitly.voidsetRoutingKey(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 noCompletableFutureAPI likeonTimeout()requested from theCompletableFuture.get(long, TimeUnit), but used in operations AMQP resources have to be closed eventually independently of theCompletableFuturefulfilment. 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:
destroyin interfaceDisposableBean
-
send
Publish a message to the default exchange and routing key (if any) (or queue) configured on this template.- Specified by:
sendin interfaceAsyncAmqpTemplate- Parameters:
message- to publish- Returns:
- the
CompletableFutureas an async result of the message publication.
-
send
Publish the message to the provided queue.- Specified by:
sendin interfaceAsyncAmqpTemplate- Parameters:
queue- to publishmessage- to publish- Returns:
- the
CompletableFutureas an async result of the message publication.
-
send
public CompletableFuture<Boolean> send(String exchange, @Nullable String routingKey, Message message) - Specified by:
sendin 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:
convertAndSendin interfaceAsyncAmqpTemplate- Parameters:
message- to publish- Returns:
- the
CompletableFutureas an async result of the message publication.
-
convertAndSend
- Specified by:
convertAndSendin interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(String exchange, @Nullable String routingKey, Object message) - Specified by:
convertAndSendin interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(Object message, @Nullable MessagePostProcessor messagePostProcessor) - Specified by:
convertAndSendin interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(String queue, Object message, @Nullable MessagePostProcessor messagePostProcessor) - Specified by:
convertAndSendin interfaceAsyncAmqpTemplate
-
convertAndSend
public CompletableFuture<Boolean> convertAndSend(String exchange, @Nullable String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor) - Specified by:
convertAndSendin interfaceAsyncAmqpTemplate
-
receive
- Specified by:
receivein interfaceAsyncAmqpTemplate
-
receive
Request a head message from the provided queue. A returnedCompletableFuturetimeouts aftersetCompletionTimeout(Duration).- Specified by:
receivein interfaceAsyncAmqpTemplate- Parameters:
queueName- the queue to consume message from.- Returns:
- the future with a received message.
- See Also:
-
receiveAndConvert
- Specified by:
receiveAndConvertin interfaceAsyncAmqpTemplate
-
receiveAndConvert
- Specified by:
receiveAndConvertin interfaceAsyncAmqpTemplate
-
receiveAndConvert
Receive a message fromsetReceiveQueue(String)and convert its body to the expected type. ThesetMessageConverter(MessageConverter)must be an implementation ofSmartMessageConverter.- Specified by:
receiveAndConvertin 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:
receiveAndConvertin 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:
receiveAndReplyin interfaceAsyncAmqpTemplate
-
receiveAndReply
public <R,S> CompletableFuture<Boolean> receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback) Description copied from interface:AsyncAmqpTemplatePerform a server-side RPC functionality. The request message must have areplyToproperty. The requestmessageIdproperty is used for correlation. The callback might not produce a reply with the meaning nothing to answer.- Specified by:
receiveAndReplyin 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:AsyncAmqpTemplateSend a message to the default exchange with the default routing key. If the message contains a correlationId property, it must be unique.- Specified by:
sendAndReceivein interfaceAsyncAmqpTemplate- Parameters:
message- the message.- Returns:
- the
CompletableFuture.
-
sendAndReceive
public CompletableFuture<Message> sendAndReceive(String exchange, @Nullable String routingKey, Message message) Description copied from interface:AsyncAmqpTemplateSend a message to the supplied exchange and routing key. If the message contains a correlationId property, it must be unique.- Specified by:
sendAndReceivein interfaceAsyncAmqpTemplate- Parameters:
exchange- the exchange.routingKey- the routing key.message- the message.- Returns:
- the
CompletableFuture.
-
sendAndReceive
Description copied from interface:AsyncAmqpTemplateSend a message to the default exchange with the supplied routing key. If the message contains a correlationId property, it must be unique.- Specified by:
sendAndReceivein interfaceAsyncAmqpTemplate- Parameters:
queue- the routing key.message- the message.- Returns:
- the
CompletableFuture.
-
convertSendAndReceive
Description copied from interface:AsyncAmqpTemplateConvert the object to a message and send it to the default exchange with the default routing key.- Specified by:
convertSendAndReceivein interfaceAsyncAmqpTemplate- Type Parameters:
C- the expected result type.- Parameters:
object- the object to convert.- Returns:
- the
CompletableFuture.
-
convertSendAndReceive
Description copied from interface:AsyncAmqpTemplateConvert the object to a message and send it to the default exchange with the provided routing key.- Specified by:
convertSendAndReceivein 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:AsyncAmqpTemplateConvert the object to a message and send it to the provided exchange and routing key.- Specified by:
convertSendAndReceivein 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:AsyncAmqpTemplateConvert 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:
convertSendAndReceivein 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:AsyncAmqpTemplateConvert 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:
convertSendAndReceivein 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:AsyncAmqpTemplateConvert 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:
convertSendAndReceivein 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:AsyncAmqpTemplateConvert the object to a message and send it to the default exchange with the default routing key.- Specified by:
convertSendAndReceiveAsTypein 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:AsyncAmqpTemplateConvert the object to a message and send it to the default exchange with the provided routing key.- Specified by:
convertSendAndReceiveAsTypein 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:AsyncAmqpTemplateConvert the object to a message and send it to the provided exchange and routing key.- Specified by:
convertSendAndReceiveAsTypein 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:AsyncAmqpTemplateConvert 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:
convertSendAndReceiveAsTypein 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:AsyncAmqpTemplateConvert 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:
convertSendAndReceiveAsTypein 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:AsyncAmqpTemplateConvert 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:
convertSendAndReceiveAsTypein 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.
-