Class RabbitMessagingTemplate
java.lang.Object
org.springframework.messaging.core.AbstractMessageSendingTemplate<D>
org.springframework.messaging.core.AbstractMessageReceivingTemplate<D>
org.springframework.messaging.core.AbstractMessagingTemplate<String>
org.springframework.amqp.rabbit.core.RabbitMessagingTemplate
- All Implemented Interfaces:
RabbitMessageOperations,InitializingBean,MessageReceivingOperations<String>,MessageRequestReplyOperations<String>,MessageSendingOperations<String>
public class RabbitMessagingTemplate
extends AbstractMessagingTemplate<String>
implements RabbitMessageOperations, InitializingBean
An implementation of
RabbitMessageOperations.- Since:
- 1.4
- Author:
- Stephane Nicoll, Gary Russell
-
Field Summary
Fields inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplate
CONVERSION_HINT_HEADER, logger -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for use with bean properties.RabbitMessagingTemplate(RabbitTemplate rabbitTemplate) Create an instance with theRabbitTemplateto use. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected MessagingExceptionprotected Message<?>convertAmqpMessage(Message message) voidconvertAndSend(String exchange, String routingKey, Object payload) Convert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message and send it to a specific exchange with a specific routing key.voidConvert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message with the given headers and send it to a specific exchange with a specific routing key.voidconvertAndSend(String exchange, String routingKey, Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) Convert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message with the given headers, apply the given post processor, and send the resulting message to a specific exchange with a specific routing key.voidconvertAndSend(String exchange, String routingKey, Object payload, MessagePostProcessor postProcessor) Convert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message, apply the given post processor, and send the resulting message to a specific exchange with a specific routing key.<T> TconvertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass) Convert the given request Object to serialized form, possibly using aMessageConverter, send it as aMessageto a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.<T> TconvertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass, MessagePostProcessor requestPostProcessor) Convert the given request Object to serialized form, possibly using aMessageConverter, apply the given post processor and send the resultingMessageto a specific exchange with a specific routing key, receive the reply and convert its body of the given target class.<T> TconvertSendAndReceive(String exchange, String routingKey, Object request, Map<String, Object> headers, Class<T> targetClass) Convert the given request Object to serialized form, possibly using aMessageConverter, send it as aMessagewith the given headers, to a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.<T> TconvertSendAndReceive(String exchange, String routingKey, Object request, Map<String, Object> headers, Class<T> targetClass, MessagePostProcessor requestPostProcessor) Convert the given request Object to serialized form, possibly using aMessageConverter, wrap it as a message with the given headers, apply the given post processor and send the resultingMessageto a specific exchange with a specific routing key,, receive the reply and convert its body of the given target class.protected Message<?>protected voidprotected voidprotected Message<?>doSendAndReceive(String exchange, String routingKey, Message<?> requestMessage) protected Message<?>doSendAndReceive(String destination, Message<?> requestMessage) Message<?>receive()<T> TreceiveAndConvert(Class<T> targetClass) voidSend a message to a specific exchange with a specific routing key.Message<?>sendAndReceive(String exchange, String routingKey, Message<?> requestMessage) Send a request message to a specific exchange with a specific routing key and wait for the reply.voidsetAmqpMessageConverter(MessageConverter amqpMessageConverter) voidsetRabbitTemplate(RabbitTemplate rabbitTemplate) Set theRabbitTemplateto use.voidsetUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue) When true, use the underlyingRabbitTemplate's defaultReceiveQueue property (if configured) for receive only methods instead of thedefaultDestinationconfigured in this template.Methods inherited from class org.springframework.messaging.core.AbstractMessagingTemplate
convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, sendAndReceive, sendAndReceiveMethods inherited from class org.springframework.messaging.core.AbstractMessageReceivingTemplate
doConvert, receive, receiveAndConvertMethods inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplate
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, doConvert, getDefaultDestination, getMessageConverter, getRequiredDefaultDestination, processHeadersToSend, send, send, setDefaultDestination, setMessageConverterMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.messaging.core.MessageReceivingOperations
receive, receiveAndConvertMethods inherited from interface org.springframework.messaging.core.MessageRequestReplyOperations
convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, sendAndReceive, sendAndReceiveMethods inherited from interface org.springframework.messaging.core.MessageSendingOperations
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, send, send
-
Constructor Details
-
RabbitMessagingTemplate
public RabbitMessagingTemplate()Constructor for use with bean properties. RequiressetRabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate)to be called. -
RabbitMessagingTemplate
Create an instance with theRabbitTemplateto use.- Parameters:
rabbitTemplate- the template.
-
-
Method Details
-
setRabbitTemplate
Set theRabbitTemplateto use.- Parameters:
rabbitTemplate- the template.
-
getRabbitTemplate
- Returns:
- the configured
RabbitTemplate.
-
setAmqpMessageConverter
Set theMessageConverterto use to convert aMessagefrom the messaging to and from aMessage. By default, aMessagingMessageConverteris defined using the providedRabbitTemplate's message converter (aSimpleMessageConverterby default) to convert the payload of the message.Consider configuring a
MessagingMessageConverterwith a differentpayload converterfor more advanced scenarios.- Parameters:
amqpMessageConverter- the message converter.- See Also:
-
getAmqpMessageConverter
- Returns:
- the
MessageConverterto use to convert aMessagefrom the messaging to and from aMessage.
-
setUseTemplateDefaultReceiveQueue
public void setUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue) When true, use the underlyingRabbitTemplate's defaultReceiveQueue property (if configured) for receive only methods instead of thedefaultDestinationconfigured in this template. Set this to true to use the template's queue instead. Default false, but will be true in a future release.- Parameters:
useTemplateDefaultReceiveQueue- true to use the template's queue.- Since:
- 2.2.22
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
send
Description copied from interface:RabbitMessageOperationsSend a message to a specific exchange with a specific routing key.- Specified by:
sendin interfaceRabbitMessageOperations- Parameters:
exchange- the name of the exchangeroutingKey- the routing keymessage- the message to send- Throws:
MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object payload) throws MessagingException Description copied from interface:RabbitMessageOperationsConvert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message and send it to a specific exchange with a specific routing key.- Specified by:
convertAndSendin interfaceRabbitMessageOperations- Parameters:
exchange- the name of the exchangeroutingKey- the routing keypayload- the Object to use as payload- Throws:
MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object payload, @Nullable Map<String, Object> headers) throws MessagingExceptionDescription copied from interface:RabbitMessageOperationsConvert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message with the given headers and send it to a specific exchange with a specific routing key.- Specified by:
convertAndSendin interfaceRabbitMessageOperations- Parameters:
exchange- the name of the exchangeroutingKey- the routing keypayload- the Object to use as payloadheaders- headers for the message to send- Throws:
MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException Description copied from interface:RabbitMessageOperationsConvert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message, apply the given post processor, and send the resulting message to a specific exchange with a specific routing key.- Specified by:
convertAndSendin interfaceRabbitMessageOperations- Parameters:
exchange- the name of the exchangeroutingKey- the routing keypayload- the Object to use as payloadpostProcessor- the post processor to apply to the message- Throws:
MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object payload, @Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingExceptionDescription copied from interface:RabbitMessageOperationsConvert the given Object to serialized form, possibly using aMessageConverter, wrap it as a message with the given headers, apply the given post processor, and send the resulting message to a specific exchange with a specific routing key.- Specified by:
convertAndSendin interfaceRabbitMessageOperations- Parameters:
exchange- the name of the exchangeroutingKey- the routing keypayload- the Object to use as payloadheaders- headers for the message to sendpostProcessor- the post processor to apply to the message- Throws:
MessagingException- a messaging exception.
-
sendAndReceive
@Nullable public Message<?> sendAndReceive(String exchange, String routingKey, Message<?> requestMessage) throws MessagingException Description copied from interface:RabbitMessageOperationsSend a request message to a specific exchange with a specific routing key and wait for the reply.- Specified by:
sendAndReceivein interfaceRabbitMessageOperations- Parameters:
exchange- the name of the exchangeroutingKey- the routing keyrequestMessage- the message to send- Returns:
- the reply, possibly
nullif the message could not be received, for example due to a timeout - Throws:
MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass) throws MessagingException Description copied from interface:RabbitMessageOperationsConvert the given request Object to serialized form, possibly using aMessageConverter, send it as aMessageto a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.- Specified by:
convertSendAndReceivein interfaceRabbitMessageOperations- Type Parameters:
T- return type- Parameters:
exchange- the name of the exchangeroutingKey- the routing keyrequest- payload for the request message to sendtargetClass- the target type to convert the payload of the reply to- Returns:
- the payload of the reply message, possibly
nullif the message could not be received, for example due to a timeout - Throws:
MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(String exchange, String routingKey, Object request, @Nullable Map<String, Object> headers, Class<T> targetClass) throws MessagingExceptionDescription copied from interface:RabbitMessageOperationsConvert the given request Object to serialized form, possibly using aMessageConverter, send it as aMessagewith the given headers, to a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.- Specified by:
convertSendAndReceivein interfaceRabbitMessageOperations- Type Parameters:
T- return type- Parameters:
exchange- the name of the exchangeroutingKey- the routing keyrequest- payload for the request message to sendheaders- headers for the request message to sendtargetClass- the target type to convert the payload of the reply to- Returns:
- the payload of the reply message, possibly
nullif the message could not be received, for example due to a timeout - Throws:
MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass, @Nullable MessagePostProcessor requestPostProcessor) throws MessagingException Description copied from interface:RabbitMessageOperationsConvert the given request Object to serialized form, possibly using aMessageConverter, apply the given post processor and send the resultingMessageto a specific exchange with a specific routing key, receive the reply and convert its body of the given target class.- Specified by:
convertSendAndReceivein interfaceRabbitMessageOperations- Type Parameters:
T- return type- Parameters:
exchange- the name of the exchangeroutingKey- the routing keyrequest- payload for the request message to sendtargetClass- the target type to convert the payload of the reply torequestPostProcessor- post process to apply to the request message- Returns:
- the payload of the reply message, possibly
nullif the message could not be received, for example due to a timeout - Throws:
MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(String exchange, String routingKey, Object request, @Nullable Map<String, Object> headers, Class<T> targetClass, @Nullable MessagePostProcessor requestPostProcessor) throws MessagingExceptionDescription copied from interface:RabbitMessageOperationsConvert the given request Object to serialized form, possibly using aMessageConverter, wrap it as a message with the given headers, apply the given post processor and send the resultingMessageto a specific exchange with a specific routing key,, receive the reply and convert its body of the given target class.- Specified by:
convertSendAndReceivein interfaceRabbitMessageOperations- Type Parameters:
T- return type- Parameters:
exchange- the name of the exchangeroutingKey- the routing keyrequest- payload for the request message to sendheaders- headers for the message to sendtargetClass- the target type to convert the payload of the reply torequestPostProcessor- post process to apply to the request message- Returns:
- the payload of the reply message, possibly
nullif the message could not be received, for example due to a timeout - Throws:
MessagingException- a messaging exception.
-
doSend
- Specified by:
doSendin classAbstractMessageSendingTemplate<String>
-
doSend
-
receive
- Specified by:
receivein interfaceMessageReceivingOperations<String>- Overrides:
receivein classAbstractMessageReceivingTemplate<String>
-
receiveAndConvert
- Specified by:
receiveAndConvertin interfaceMessageReceivingOperations<String>- Overrides:
receiveAndConvertin classAbstractMessageReceivingTemplate<String>
-
doReceive
- Specified by:
doReceivein classAbstractMessageReceivingTemplate<String>
-
doSendAndReceive
- Specified by:
doSendAndReceivein classAbstractMessagingTemplate<String>
-
doSendAndReceive
-
convertAmqpMessage
-
convertAmqpException
-