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
,InitializingBean
,MessageReceivingOperations<java.lang.String>
,MessageRequestReplyOperations<java.lang.String>
,MessageSendingOperations<java.lang.String>
public class RabbitMessagingTemplate extends AbstractMessagingTemplate<java.lang.String> implements RabbitMessageOperations, InitializingBean
An implementation ofRabbitMessageOperations
.- 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
Constructors Constructor Description RabbitMessagingTemplate()
Constructor for use with bean properties.RabbitMessagingTemplate(RabbitTemplate rabbitTemplate)
Create an instance with theRabbitTemplate
to use.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
protected MessagingException
convertAmqpException(java.lang.RuntimeException ex)
protected Message<?>
convertAmqpMessage(Message message)
void
convertAndSend(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.void
convertAndSend(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.void
convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, java.util.Map<java.lang.String,java.lang.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.void
convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.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> T
convertSendAndReceive(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 aMessage
to a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.<T> T
convertSendAndReceive(java.lang.String exchange, java.lang.String routingKey, java.lang.Object request, java.lang.Class<T> targetClass, MessagePostProcessor requestPostProcessor)
Convert the given request Object to serialized form, possibly using aMessageConverter
, apply the given post processor and send the resultingMessage
to a specific exchange with a specific routing key, receive the reply and convert its body of the given target class.<T> T
convertSendAndReceive(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 aMessage
with 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> T
convertSendAndReceive(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, 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 resultingMessage
to a specific exchange with a specific routing key,, receive the reply and convert its body of the given target class.protected Message<?>
doReceive(java.lang.String destination)
protected void
doSend(java.lang.String exchange, java.lang.String routingKey, Message<?> message)
protected void
doSend(java.lang.String destination, Message<?> message)
protected Message<?>
doSendAndReceive(java.lang.String exchange, java.lang.String routingKey, Message<?> requestMessage)
protected Message<?>
doSendAndReceive(java.lang.String destination, Message<?> requestMessage)
MessageConverter
getAmqpMessageConverter()
RabbitTemplate
getRabbitTemplate()
Message<?>
receive()
<T> T
receiveAndConvert(java.lang.Class<T> targetClass)
void
send(java.lang.String exchange, java.lang.String routingKey, Message<?> message)
Send a message to a specific exchange with a specific routing key.Message<?>
sendAndReceive(java.lang.String exchange, java.lang.String routingKey, Message<?> requestMessage)
Send a request message to a specific exchange with a specific routing key and wait for the reply.void
setAmqpMessageConverter(MessageConverter amqpMessageConverter)
void
setRabbitTemplate(RabbitTemplate rabbitTemplate)
Set theRabbitTemplate
to use.void
setUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue)
When true, use the underlyingRabbitTemplate
's defaultReceiveQueue property (if configured) for receive only methods instead of thedefaultDestination
configured 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
-
Methods inherited from interface org.springframework.messaging.core.MessageRequestReplyOperations
convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, convertSendAndReceive, sendAndReceive, sendAndReceive
-
Methods inherited from interface org.springframework.messaging.core.MessageSendingOperations
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, send, send
-
-
-
-
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 theRabbitTemplate
to use.- Parameters:
rabbitTemplate
- the template.
-
-
Method Detail
-
setRabbitTemplate
public void setRabbitTemplate(RabbitTemplate rabbitTemplate)
Set theRabbitTemplate
to use.- Parameters:
rabbitTemplate
- the template.
-
getRabbitTemplate
public RabbitTemplate getRabbitTemplate()
- Returns:
- the configured
RabbitTemplate
.
-
setAmqpMessageConverter
public void setAmqpMessageConverter(MessageConverter amqpMessageConverter)
Set theMessageConverter
to use to convert aMessage
from the messaging to and from aMessage
. By default, aMessagingMessageConverter
is defined using the providedRabbitTemplate
's message converter (aSimpleMessageConverter
by default) to convert the payload of the message.Consider configuring a
MessagingMessageConverter
with a differentpayload converter
for more advanced scenarios.- Parameters:
amqpMessageConverter
- the message converter.- See Also:
MessagingMessageConverter
-
getAmqpMessageConverter
public MessageConverter getAmqpMessageConverter()
- Returns:
- the
MessageConverter
to use to convert aMessage
from 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 thedefaultDestination
configured 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:
afterPropertiesSet
in interfaceInitializingBean
-
send
public void send(java.lang.String exchange, java.lang.String routingKey, Message<?> message) throws MessagingException
Description copied from interface:RabbitMessageOperations
Send a message to a specific exchange with a specific routing key.- Specified by:
send
in interfaceRabbitMessageOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- the message to send- Throws:
MessagingException
- a messaging exception.
-
convertAndSend
public void convertAndSend(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload) throws MessagingException
Description copied from interface:RabbitMessageOperations
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.- Specified by:
convertAndSend
in 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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, @Nullable java.util.Map<java.lang.String,java.lang.Object> headers) throws MessagingException
Description copied from interface:RabbitMessageOperations
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.- Specified by:
convertAndSend
in 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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException
Description copied from interface:RabbitMessageOperations
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.- Specified by:
convertAndSend
in 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(java.lang.String exchange, java.lang.String routingKey, java.lang.Object payload, @Nullable java.util.Map<java.lang.String,java.lang.Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingException
Description copied from interface:RabbitMessageOperations
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.- Specified by:
convertAndSend
in 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(java.lang.String exchange, java.lang.String routingKey, Message<?> requestMessage) throws MessagingException
Description copied from interface:RabbitMessageOperations
Send a request message to a specific exchange with a specific routing key and wait for the reply.- Specified by:
sendAndReceive
in interfaceRabbitMessageOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keyrequestMessage
- the message to send- Returns:
- the reply, possibly
null
if the message could not be received, for example due to a timeout - Throws:
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 MessagingException
Description copied from interface:RabbitMessageOperations
Convert the given request Object to serialized form, possibly using aMessageConverter
, send it as aMessage
to a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.- Specified by:
convertSendAndReceive
in 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
null
if the message could not be received, for example due to a timeout - Throws:
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 MessagingException
Description copied from interface:RabbitMessageOperations
Convert the given request Object to serialized form, possibly using aMessageConverter
, send it as aMessage
with 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:
convertSendAndReceive
in 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
null
if the message could not be received, for example due to a timeout - Throws:
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 MessagePostProcessor requestPostProcessor) throws MessagingException
Description copied from interface:RabbitMessageOperations
Convert the given request Object to serialized form, possibly using aMessageConverter
, apply the given post processor and send the resultingMessage
to a specific exchange with a specific routing key, receive the reply and convert its body of the given target class.- Specified by:
convertSendAndReceive
in 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
null
if the message could not be received, for example due to a timeout - Throws:
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 MessagePostProcessor requestPostProcessor) throws MessagingException
Description copied from interface:RabbitMessageOperations
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 resultingMessage
to a specific exchange with a specific routing key,, receive the reply and convert its body of the given target class.- Specified by:
convertSendAndReceive
in 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
null
if the message could not be received, for example due to a timeout - Throws:
MessagingException
- a messaging exception.
-
doSend
protected void doSend(java.lang.String destination, Message<?> message)
- Specified by:
doSend
in classAbstractMessageSendingTemplate<java.lang.String>
-
doSend
protected void doSend(java.lang.String exchange, java.lang.String routingKey, Message<?> message)
-
receive
@Nullable public Message<?> receive()
- Specified by:
receive
in interfaceMessageReceivingOperations<java.lang.String>
- Overrides:
receive
in classAbstractMessageReceivingTemplate<java.lang.String>
-
receiveAndConvert
@Nullable public <T> T receiveAndConvert(java.lang.Class<T> targetClass)
- Specified by:
receiveAndConvert
in interfaceMessageReceivingOperations<java.lang.String>
- Overrides:
receiveAndConvert
in classAbstractMessageReceivingTemplate<java.lang.String>
-
doReceive
protected Message<?> doReceive(java.lang.String destination)
- Specified by:
doReceive
in classAbstractMessageReceivingTemplate<java.lang.String>
-
doSendAndReceive
@Nullable protected Message<?> doSendAndReceive(java.lang.String destination, Message<?> requestMessage)
- Specified by:
doSendAndReceive
in classAbstractMessagingTemplate<java.lang.String>
-
doSendAndReceive
@Nullable protected Message<?> doSendAndReceive(java.lang.String exchange, java.lang.String routingKey, Message<?> requestMessage)
-
convertAmqpException
protected MessagingException convertAmqpException(java.lang.RuntimeException ex)
-
-