Class RabbitAmqpTemplate

java.lang.Object
org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate
All Implemented Interfaces:
org.springframework.amqp.core.AsyncAmqpTemplate, org.springframework.beans.factory.DisposableBean

public class RabbitAmqpTemplate extends Object implements org.springframework.amqp.core.AsyncAmqpTemplate, org.springframework.beans.factory.DisposableBean
The AmqpTemplate for RabbitMQ AMQP 1.0 protocol support. A Spring-friendly wrapper around Environment.connectionBuilder();
Since:
4.0
  • Constructor Details

  • Method Details

    • setListeners

      public void setListeners(com.rabbitmq.client.amqp.Resource.StateListener... listeners)
    • setPublishTimeout

      public void setPublishTimeout(Duration timeout)
    • setCompletionTimeout

      public void setCompletionTimeout(Duration completionTimeout)
      Set a duration for CompletableFuture.orTimeout(long, TimeUnit) on returns. There is no CompletableFuture API like onTimeout() requested from the CompletableFuture.get(long, TimeUnit), but used in operations AMQP resources have to be closed eventually independently of the CompletableFuture fulfilment. Defaults to 1 minute.
      Parameters:
      completionTimeout - duration for future completions.
    • setExchange

      public void setExchange(String exchange)
      Set a default exchange for publishing. Cannot be real default AMQP exchange. The setQueue(String) is recommended instead. Mutually exclusive with setQueue(String).
      Parameters:
      exchange - the default exchange
    • setRoutingKey

      public void setRoutingKey(String routingKey)
      Set a default routing key. Mutually exclusive with setQueue(String).
      Parameters:
      routingKey - the default routing key.
    • setQueue

      public void setQueue(String queue)
      Set default queue for publishing. Mutually exclusive with setExchange(String) and setRoutingKey(String).
      Parameters:
      queue - the default queue.
    • setReceiveQueue

      public void setReceiveQueue(String queue)
      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

      public void setReplyToQueue(String queue)
      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

      public void setMessageConverter(org.springframework.amqp.support.converter.MessageConverter messageConverter)
      Set a converter for convertAndSend(Object) operations.
      Parameters:
      messageConverter - the converter.
    • getPublisher

      public com.rabbitmq.client.amqp.Publisher getPublisher()
      Return the Publisher for low-level AMQP operations.
      Returns:
      the Publisher for low-level AMQP operations.
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • send

      public CompletableFuture<Boolean> send(org.springframework.amqp.core.Message message)
      Publish a message to the default exchange and routing key (if any) (or queue) configured on this template.
      Specified by:
      send in interface org.springframework.amqp.core.AsyncAmqpTemplate
      Parameters:
      message - to publish
      Returns:
      the CompletableFuture as an async result of the message publication.
    • send

      public CompletableFuture<Boolean> send(String queue, org.springframework.amqp.core.Message message)
      Publish the message to the provided queue.
      Specified by:
      send in interface org.springframework.amqp.core.AsyncAmqpTemplate
      Parameters:
      queue - to publish
      message - to publish
      Returns:
      the CompletableFuture as an async result of the message publication.
    • send

      public CompletableFuture<Boolean> send(String exchange, @Nullable String routingKey, org.springframework.amqp.core.Message message)
      Specified by:
      send in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertAndSend

      public CompletableFuture<Boolean> convertAndSend(Object message)
      Publish a message from converted body to the default exchange and routing key (if any) (or queue) configured on this template.
      Specified by:
      convertAndSend in interface org.springframework.amqp.core.AsyncAmqpTemplate
      Parameters:
      message - to publish
      Returns:
      the CompletableFuture as an async result of the message publication.
    • convertAndSend

      public CompletableFuture<Boolean> convertAndSend(String queue, Object message)
      Specified by:
      convertAndSend in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertAndSend

      public CompletableFuture<Boolean> convertAndSend(String exchange, @Nullable String routingKey, Object message)
      Specified by:
      convertAndSend in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertAndSend

      public CompletableFuture<Boolean> convertAndSend(Object message, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
      Specified by:
      convertAndSend in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertAndSend

      public CompletableFuture<Boolean> convertAndSend(String queue, Object message, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
      Specified by:
      convertAndSend in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertAndSend

      public CompletableFuture<Boolean> convertAndSend(String exchange, @Nullable String routingKey, Object message, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
      Specified by:
      convertAndSend in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • receive

      public CompletableFuture<org.springframework.amqp.core.Message> receive()
      Specified by:
      receive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • receive

      public CompletableFuture<org.springframework.amqp.core.Message> receive(String queueName)
      Request a head message from the provided queue. A returned CompletableFuture timeouts after setCompletionTimeout(Duration).
      Specified by:
      receive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      Parameters:
      queueName - the queue to consume message from.
      Returns:
      the future with a received message.
      See Also:
    • receiveAndConvert

      public CompletableFuture<Object> receiveAndConvert()
      Specified by:
      receiveAndConvert in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • receiveAndConvert

      public CompletableFuture<Object> receiveAndConvert(String queueName)
      Specified by:
      receiveAndConvert in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • receiveAndConvert

      public <T> CompletableFuture<T> receiveAndConvert(@Nullable org.springframework.core.ParameterizedTypeReference<T> type)
      Receive a message from setReceiveQueue(String) and convert its body to the expected type. The setMessageConverter(MessageConverter) must be an implementation of SmartMessageConverter.
      Specified by:
      receiveAndConvert in interface org.springframework.amqp.core.AsyncAmqpTemplate
      Parameters:
      type - the type to covert received result.
      Returns:
      the CompletableFuture with a result.
    • receiveAndConvert

      public <T> CompletableFuture<T> receiveAndConvert(String queueName, @Nullable org.springframework.core.ParameterizedTypeReference<T> type)
      Receive a message from setReceiveQueue(String) and convert its body to the expected type. The setMessageConverter(MessageConverter) must be an implementation of SmartMessageConverter.
      Specified by:
      receiveAndConvert in interface org.springframework.amqp.core.AsyncAmqpTemplate
      Parameters:
      queueName - the queue to consume message from.
      type - the type to covert received result.
      Returns:
      the CompletableFuture with a result.
    • receiveAndReply

      public <R,S> CompletableFuture<Boolean> receiveAndReply(org.springframework.amqp.core.ReceiveAndReplyCallback<R,S> callback)
      Specified by:
      receiveAndReply in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • receiveAndReply

      public <R,S> CompletableFuture<Boolean> receiveAndReply(String queueName, org.springframework.amqp.core.ReceiveAndReplyCallback<R,S> callback)
      Specified by:
      receiveAndReply in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • sendAndReceive

      public CompletableFuture<org.springframework.amqp.core.Message> sendAndReceive(org.springframework.amqp.core.Message message)
      Specified by:
      sendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • sendAndReceive

      public CompletableFuture<org.springframework.amqp.core.Message> sendAndReceive(String exchange, @Nullable String routingKey, org.springframework.amqp.core.Message message)
      Specified by:
      sendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • sendAndReceive

      public CompletableFuture<org.springframework.amqp.core.Message> sendAndReceive(String queue, org.springframework.amqp.core.Message message)
      Specified by:
      sendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceive

      public <C> CompletableFuture<C> convertSendAndReceive(Object object)
      Specified by:
      convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceive

      public <C> CompletableFuture<C> convertSendAndReceive(String queue, Object object)
      Specified by:
      convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceive

      public <C> CompletableFuture<C> convertSendAndReceive(String exchange, @Nullable String routingKey, Object object)
      Specified by:
      convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceive

      public <C> CompletableFuture<C> convertSendAndReceive(Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
      Specified by:
      convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceive

      public <C> CompletableFuture<C> convertSendAndReceive(String queue, Object object, org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
      Specified by:
      convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceive

      public <C> CompletableFuture<C> convertSendAndReceive(String exchange, @Nullable String routingKey, Object object, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
      Specified by:
      convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceiveAsType

      public <C> CompletableFuture<C> convertSendAndReceiveAsType(Object object, org.springframework.core.ParameterizedTypeReference<C> responseType)
      Specified by:
      convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceiveAsType

      public <C> CompletableFuture<C> convertSendAndReceiveAsType(String queue, Object object, org.springframework.core.ParameterizedTypeReference<C> responseType)
      Specified by:
      convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceiveAsType

      public <C> CompletableFuture<C> convertSendAndReceiveAsType(String exchange, @Nullable String routingKey, Object object, org.springframework.core.ParameterizedTypeReference<C> responseType)
      Specified by:
      convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceiveAsType

      public <C> CompletableFuture<C> convertSendAndReceiveAsType(Object object, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, @Nullable org.springframework.core.ParameterizedTypeReference<C> responseType)
      Specified by:
      convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceiveAsType

      public <C> CompletableFuture<C> convertSendAndReceiveAsType(String queue, Object object, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, @Nullable org.springframework.core.ParameterizedTypeReference<C> responseType)
      Specified by:
      convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
    • convertSendAndReceiveAsType

      public <C> CompletableFuture<C> convertSendAndReceiveAsType(String exchange, @Nullable String routingKey, Object object, @Nullable org.springframework.amqp.core.MessagePostProcessor messagePostProcessor, @Nullable org.springframework.core.ParameterizedTypeReference<C> responseType)
      Specified by:
      convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate