Class RabbitMessagingTemplate
- java.lang.Object
-
- org.springframework.messaging.core.AbstractMessageSendingTemplate<D>
-
- org.springframework.messaging.core.AbstractMessageReceivingTemplate<D>
-
- org.springframework.messaging.core.AbstractMessagingTemplate<java.lang.String>
-
- org.springframework.amqp.rabbit.core.RabbitMessagingTemplate
-
- All Implemented Interfaces:
RabbitMessageOperations,org.springframework.beans.factory.InitializingBean,org.springframework.messaging.core.MessageReceivingOperations<java.lang.String>,org.springframework.messaging.core.MessageRequestReplyOperations<java.lang.String>,org.springframework.messaging.core.MessageSendingOperations<java.lang.String>
public class RabbitMessagingTemplate extends org.springframework.messaging.core.AbstractMessagingTemplate<java.lang.String> implements RabbitMessageOperations, org.springframework.beans.factory.InitializingBean
An implementation ofRabbitMessageOperations.- Since:
- 1.4
-
-
Constructor Summary
Constructors Constructor Description RabbitMessagingTemplate()Constructor for use with bean properties.RabbitMessagingTemplate(RabbitTemplate rabbitTemplate)Create an instance with theRabbitTemplateto use.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()protected org.springframework.messaging.MessagingExceptionconvertAmqpException(java.lang.RuntimeException ex)protected org.springframework.messaging.Message<?>convertAmqpMessage(org.springframework.amqp.core.Message message)voidconvertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.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.voidconvertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, java.util.Map<java.lang.String,java.lang.Object> headers)Convert 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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, java.util.Map<java.lang.String,java.lang.Object> headers, org.springframework.messaging.core.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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, org.springframework.messaging.core.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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.lang.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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.lang.Class<T> targetClass, org.springframework.messaging.core.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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.Class<T> targetClass, org.springframework.messaging.core.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 org.springframework.messaging.Message<?>doReceive(java.lang.String destination)protected voiddoSend(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> message)protected voiddoSend(java.lang.String destination, org.springframework.messaging.Message<?> message)protected org.springframework.messaging.Message<?>doSendAndReceive(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> requestMessage)protected org.springframework.messaging.Message<?>doSendAndReceive(java.lang.String destination, org.springframework.messaging.Message<?> requestMessage)org.springframework.amqp.support.converter.MessageConvertergetAmqpMessageConverter()RabbitTemplategetRabbitTemplate()org.springframework.messaging.Message<?>receive()<T> TreceiveAndConvert(java.lang.Class<T> targetClass)voidsend(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> message)Send a message to a specific exchange with a specific routing key.org.springframework.messaging.Message<?>sendAndReceive(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> requestMessage)Send a request message to a specific exchange with a specific routing key and wait for the reply.voidsetAmqpMessageConverter(org.springframework.amqp.support.converter.MessageConverter amqpMessageConverter)Set theMessageConverterto use to convert aMessagefrom the messaging to and from aMessage.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, sendAndReceive
-
Methods inherited from class org.springframework.messaging.core.AbstractMessageReceivingTemplate
doConvert, receive, receiveAndConvert
-
Methods inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplate
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, doConvert, getDefaultDestination, getMessageConverter, getRequiredDefaultDestination, processHeadersToSend, send, send, setDefaultDestination, setMessageConverter
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.springframework.messaging.core.MessageReceivingOperations
receive, receiveAndConvert
-
-
-
-
Constructor Detail
-
RabbitMessagingTemplate
public RabbitMessagingTemplate()
Constructor for use with bean properties. RequiressetRabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate)to be called.
-
RabbitMessagingTemplate
public RabbitMessagingTemplate(RabbitTemplate rabbitTemplate)
Create an instance with theRabbitTemplateto use.- Parameters:
rabbitTemplate- the template.
-
-
Method Detail
-
setRabbitTemplate
public void setRabbitTemplate(RabbitTemplate rabbitTemplate)
Set theRabbitTemplateto use.- Parameters:
rabbitTemplate- the template.
-
getRabbitTemplate
public RabbitTemplate getRabbitTemplate()
- Returns:
- the configured
RabbitTemplate.
-
setAmqpMessageConverter
public void setAmqpMessageConverter(org.springframework.amqp.support.converter.MessageConverter amqpMessageConverter)
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:
MessagingMessageConverter
-
getAmqpMessageConverter
public org.springframework.amqp.support.converter.MessageConverter 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 interfaceorg.springframework.beans.factory.InitializingBean
-
send
public void send(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> message) throws org.springframework.messaging.MessagingExceptionDescription 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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload) throws org.springframework.messaging.MessagingExceptionDescription 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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, @Nullable java.util.Map<java.lang.String,java.lang.Object> headers) throws org.springframework.messaging.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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, @Nullable org.springframework.messaging.core.MessagePostProcessor postProcessor) throws org.springframework.messaging.MessagingExceptionDescription 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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertAndSend
public void convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, @Nullable java.util.Map<java.lang.String,java.lang.Object> headers, @Nullable org.springframework.messaging.core.MessagePostProcessor postProcessor) throws org.springframework.messaging.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:
org.springframework.messaging.MessagingException- a messaging exception.
-
sendAndReceive
@Nullable public org.springframework.messaging.Message<?> sendAndReceive(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> requestMessage) throws org.springframework.messaging.MessagingExceptionDescription 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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.lang.Class<T> targetClass) throws org.springframework.messaging.MessagingExceptionDescription 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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, @Nullable java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.Class<T> targetClass) throws org.springframework.messaging.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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.lang.Class<T> targetClass, @Nullable org.springframework.messaging.core.MessagePostProcessor requestPostProcessor) throws org.springframework.messaging.MessagingExceptionDescription 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:
org.springframework.messaging.MessagingException- a messaging exception.
-
convertSendAndReceive
@Nullable public <T> T convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, @Nullable java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.Class<T> targetClass, @Nullable org.springframework.messaging.core.MessagePostProcessor requestPostProcessor) throws org.springframework.messaging.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:
org.springframework.messaging.MessagingException- a messaging exception.
-
doSend
protected void doSend(java.lang.String destination, org.springframework.messaging.Message<?> message)- Specified by:
doSendin classorg.springframework.messaging.core.AbstractMessageSendingTemplate<java.lang.String>
-
doSend
protected void doSend(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> message)
-
receive
@Nullable public org.springframework.messaging.Message<?> receive()
- Specified by:
receivein interfaceorg.springframework.messaging.core.MessageReceivingOperations<java.lang.String>- Overrides:
receivein classorg.springframework.messaging.core.AbstractMessageReceivingTemplate<java.lang.String>
-
receiveAndConvert
@Nullable public <T> T receiveAndConvert(java.lang.Class<T> targetClass)
- Specified by:
receiveAndConvertin interfaceorg.springframework.messaging.core.MessageReceivingOperations<java.lang.String>- Overrides:
receiveAndConvertin classorg.springframework.messaging.core.AbstractMessageReceivingTemplate<java.lang.String>
-
doReceive
protected org.springframework.messaging.Message<?> doReceive(java.lang.String destination)
- Specified by:
doReceivein classorg.springframework.messaging.core.AbstractMessageReceivingTemplate<java.lang.String>
-
doSendAndReceive
@Nullable protected org.springframework.messaging.Message<?> doSendAndReceive(java.lang.String destination, org.springframework.messaging.Message<?> requestMessage)- Specified by:
doSendAndReceivein classorg.springframework.messaging.core.AbstractMessagingTemplate<java.lang.String>
-
doSendAndReceive
@Nullable protected org.springframework.messaging.Message<?> doSendAndReceive(java.lang.String exchange, java.lang.String routingKey, org.springframework.messaging.Message<?> requestMessage)
-
convertAmqpMessage
@Nullable protected org.springframework.messaging.Message<?> convertAmqpMessage(@Nullable org.springframework.amqp.core.Message message)
-
convertAmqpException
protected org.springframework.messaging.MessagingException convertAmqpException(java.lang.RuntimeException ex)
-
-