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, org.springframework.beans.factory.InitializingBean, org.springframework.messaging.core.MessageReceivingOperations<String>, org.springframework.messaging.core.MessageRequestReplyOperations<String>, org.springframework.messaging.core.MessageSendingOperations<String>

public class RabbitMessagingTemplate extends org.springframework.messaging.core.AbstractMessagingTemplate<String> implements RabbitMessageOperations, org.springframework.beans.factory.InitializingBean
An implementation of RabbitMessageOperations.
Since:
1.4
  • Field Summary

    Fields inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplate

    CONVERSION_HINT_HEADER, logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for use with bean properties.
    Create an instance with the RabbitTemplate to use.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected org.springframework.messaging.MessagingException
     
    protected org.springframework.messaging.Message<?>
    convertAmqpMessage(org.springframework.amqp.core.Message message)
     
    void
    convertAndSend(String exchange, String routingKey, Object payload)
    Convert the given Object to serialized form, possibly using a MessageConverter, wrap it as a message and send it to a specific exchange with a specific routing key.
    void
    convertAndSend(String exchange, String routingKey, Object payload, Map<String,Object> headers)
    Convert the given Object to serialized form, possibly using a MessageConverter, wrap it as a message with the given headers and send it to a specific exchange with a specific routing key.
    void
    convertAndSend(String exchange, String routingKey, Object payload, Map<String,Object> headers, org.springframework.messaging.core.MessagePostProcessor postProcessor)
    Convert the given Object to serialized form, possibly using a MessageConverter, 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(String exchange, String routingKey, Object payload, org.springframework.messaging.core.MessagePostProcessor postProcessor)
    Convert the given Object to serialized form, possibly using a MessageConverter, 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(String exchange, String routingKey, Object request, Class<T> targetClass)
    Convert the given request Object to serialized form, possibly using a MessageConverter, send it as a Message to a specific exchange with a specific routing key, receive the reply and convert its body of the specified target class.
    <T> T
    convertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass, org.springframework.messaging.core.MessagePostProcessor requestPostProcessor)
    Convert the given request Object to serialized form, possibly using a MessageConverter, apply the given post processor and send the resulting Message to a specific exchange with a specific routing key, receive the reply and convert its body of the given target class.
    <T> T
    convertSendAndReceive(String exchange, String routingKey, Object request, Map<String,Object> headers, Class<T> targetClass)
    Convert the given request Object to serialized form, possibly using a MessageConverter, send it as a Message 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(String exchange, String routingKey, Object request, Map<String,Object> headers, Class<T> targetClass, org.springframework.messaging.core.MessagePostProcessor requestPostProcessor)
    Convert the given request Object to serialized form, possibly using a MessageConverter, 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,, receive the reply and convert its body of the given target class.
    protected org.springframework.messaging.Message<?>
    doReceive(String destination)
     
    protected void
    doSend(String exchange, String routingKey, org.springframework.messaging.Message<?> message)
     
    protected void
    doSend(String destination, org.springframework.messaging.Message<?> message)
     
    protected org.springframework.messaging.Message<?>
    doSendAndReceive(String exchange, String routingKey, org.springframework.messaging.Message<?> requestMessage)
     
    protected org.springframework.messaging.Message<?>
    doSendAndReceive(String destination, org.springframework.messaging.Message<?> requestMessage)
     
    org.springframework.amqp.support.converter.MessageConverter
     
     
    org.springframework.messaging.Message<?>
     
    <T> T
    receiveAndConvert(Class<T> targetClass)
     
    void
    send(String exchange, String routingKey, org.springframework.messaging.Message<?> message)
    Send a message to a specific exchange with a specific routing key.
    org.springframework.messaging.Message<?>
    sendAndReceive(String exchange, 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.
    void
    setAmqpMessageConverter(org.springframework.amqp.support.converter.MessageConverter amqpMessageConverter)
    Set the MessageConverter to use to convert a Message from the messaging to and from a Message.
    void
    Set the RabbitTemplate to use.
    void
    setUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue)
    When true, use the underlying RabbitTemplate's defaultReceiveQueue property (if configured) for receive only methods instead of the defaultDestination 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 Details

  • Method Details

    • setRabbitTemplate

      public void setRabbitTemplate(RabbitTemplate rabbitTemplate)
      Set the RabbitTemplate to 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 the MessageConverter to use to convert a Message from the messaging to and from a Message. By default, a MessagingMessageConverter is defined using the provided RabbitTemplate's message converter (a SimpleMessageConverter by default) to convert the payload of the message.

      Consider configuring a MessagingMessageConverter with a different payload converter for more advanced scenarios.

      Parameters:
      amqpMessageConverter - the message converter.
      See Also:
      • MessagingMessageConverter
    • getAmqpMessageConverter

      public org.springframework.amqp.support.converter.MessageConverter getAmqpMessageConverter()
      Returns:
      the MessageConverter to use to convert a Message from the messaging to and from a Message.
    • setUseTemplateDefaultReceiveQueue

      public void setUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue)
      When true, use the underlying RabbitTemplate's defaultReceiveQueue property (if configured) for receive only methods instead of the defaultDestination 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 interface org.springframework.beans.factory.InitializingBean
    • send

      public void send(String exchange, String routingKey, org.springframework.messaging.Message<?> message) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Send a message to a specific exchange with a specific routing key.
      Specified by:
      send in interface RabbitMessageOperations
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      message - the message to send
      Throws:
      org.springframework.messaging.MessagingException - a messaging exception.
    • convertAndSend

      public void convertAndSend(String exchange, String routingKey, Object payload) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given Object to serialized form, possibly using a MessageConverter, wrap it as a message and send it to a specific exchange with a specific routing key.
      Specified by:
      convertAndSend in interface RabbitMessageOperations
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      payload - the Object to use as payload
      Throws:
      org.springframework.messaging.MessagingException - a messaging exception.
    • convertAndSend

      public void convertAndSend(String exchange, String routingKey, Object payload, @Nullable Map<String,Object> headers) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given Object to serialized form, possibly using a MessageConverter, 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 interface RabbitMessageOperations
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      payload - the Object to use as payload
      headers - headers for the message to send
      Throws:
      org.springframework.messaging.MessagingException - a messaging exception.
    • convertAndSend

      public void convertAndSend(String exchange, String routingKey, Object payload, @Nullable org.springframework.messaging.core.MessagePostProcessor postProcessor) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given Object to serialized form, possibly using a MessageConverter, 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 interface RabbitMessageOperations
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      payload - the Object to use as payload
      postProcessor - the post processor to apply to the message
      Throws:
      org.springframework.messaging.MessagingException - a messaging exception.
    • convertAndSend

      public void convertAndSend(String exchange, String routingKey, Object payload, @Nullable Map<String,Object> headers, @Nullable org.springframework.messaging.core.MessagePostProcessor postProcessor) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given Object to serialized form, possibly using a MessageConverter, 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 interface RabbitMessageOperations
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      payload - the Object to use as payload
      headers - headers for the message to send
      postProcessor - the post processor to apply to the message
      Throws:
      org.springframework.messaging.MessagingException - a messaging exception.
    • sendAndReceive

      @Nullable public org.springframework.messaging.Message<?> sendAndReceive(String exchange, String routingKey, org.springframework.messaging.Message<?> requestMessage) throws org.springframework.messaging.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 interface RabbitMessageOperations
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      requestMessage - the message to send
      Returns:
      the reply, possibly null if 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(String exchange, String routingKey, Object request, Class<T> targetClass) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given request Object to serialized form, possibly using a MessageConverter, send it as a Message 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 interface RabbitMessageOperations
      Type Parameters:
      T - return type
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      request - payload for the request message to send
      targetClass - 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:
      org.springframework.messaging.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 org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given request Object to serialized form, possibly using a MessageConverter, send it as a Message 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 interface RabbitMessageOperations
      Type Parameters:
      T - return type
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      request - payload for the request message to send
      headers - headers for the request message to send
      targetClass - 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:
      org.springframework.messaging.MessagingException - a messaging exception.
    • convertSendAndReceive

      @Nullable public <T> T convertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass, @Nullable org.springframework.messaging.core.MessagePostProcessor requestPostProcessor) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given request Object to serialized form, possibly using a MessageConverter, apply the given post processor and send the resulting Message 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 interface RabbitMessageOperations
      Type Parameters:
      T - return type
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      request - payload for the request message to send
      targetClass - the target type to convert the payload of the reply to
      requestPostProcessor - 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:
      org.springframework.messaging.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 org.springframework.messaging.core.MessagePostProcessor requestPostProcessor) throws org.springframework.messaging.MessagingException
      Description copied from interface: RabbitMessageOperations
      Convert the given request Object to serialized form, possibly using a MessageConverter, 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,, receive the reply and convert its body of the given target class.
      Specified by:
      convertSendAndReceive in interface RabbitMessageOperations
      Type Parameters:
      T - return type
      Parameters:
      exchange - the name of the exchange
      routingKey - the routing key
      request - payload for the request message to send
      headers - headers for the message to send
      targetClass - the target type to convert the payload of the reply to
      requestPostProcessor - 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:
      org.springframework.messaging.MessagingException - a messaging exception.
    • doSend

      protected void doSend(String destination, org.springframework.messaging.Message<?> message)
      Specified by:
      doSend in class org.springframework.messaging.core.AbstractMessageSendingTemplate<String>
    • doSend

      protected void doSend(String exchange, String routingKey, org.springframework.messaging.Message<?> message)
    • receive

      @Nullable public org.springframework.messaging.Message<?> receive()
      Specified by:
      receive in interface org.springframework.messaging.core.MessageReceivingOperations<String>
      Overrides:
      receive in class org.springframework.messaging.core.AbstractMessageReceivingTemplate<String>
    • receiveAndConvert

      @Nullable public <T> T receiveAndConvert(Class<T> targetClass)
      Specified by:
      receiveAndConvert in interface org.springframework.messaging.core.MessageReceivingOperations<String>
      Overrides:
      receiveAndConvert in class org.springframework.messaging.core.AbstractMessageReceivingTemplate<String>
    • doReceive

      protected org.springframework.messaging.Message<?> doReceive(String destination)
      Specified by:
      doReceive in class org.springframework.messaging.core.AbstractMessageReceivingTemplate<String>
    • doSendAndReceive

      @Nullable protected org.springframework.messaging.Message<?> doSendAndReceive(String destination, org.springframework.messaging.Message<?> requestMessage)
      Specified by:
      doSendAndReceive in class org.springframework.messaging.core.AbstractMessagingTemplate<String>
    • doSendAndReceive

      @Nullable protected org.springframework.messaging.Message<?> doSendAndReceive(String exchange, 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(RuntimeException ex)