Class AsyncRabbitTemplate

  • All Implemented Interfaces:
    org.springframework.amqp.core.AsyncAmqpTemplate, org.springframework.amqp.core.MessageListener, RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback, RabbitTemplate.ReturnsCallback, ChannelAwareMessageListener, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle

    public class AsyncRabbitTemplate
    extends java.lang.Object
    implements org.springframework.amqp.core.AsyncAmqpTemplate, ChannelAwareMessageListener, RabbitTemplate.ReturnsCallback, RabbitTemplate.ConfirmCallback, org.springframework.beans.factory.BeanNameAware, org.springframework.context.SmartLifecycle
    Provides asynchronous send and receive operations returning a ListenableFuture allowing the caller to obtain the reply later, using get() or a callback.

    When confirms are enabled, the future has a confirm property which is itself a ListenableFuture. If the reply is received before the publisher confirm, the confirm is discarded since the reply implicitly indicates the message was published.

    Returned (undeliverable) request messages are presented as a AmqpMessageReturnedException cause of an ExecutionException.

    Internally, the template uses a RabbitTemplate and an AbstractMessageListenerContainer either provided or constructed internally (a SimpleMessageListenerContainer). If an external RabbitTemplate is provided and confirms/returns are enabled, it must not previously have had callbacks registered because this object needs to be the callback.

    Since:
    1.6
    • Field Detail

      • DEFAULT_RECEIVE_TIMEOUT

        public static final int DEFAULT_RECEIVE_TIMEOUT
        See Also:
        Constant Field Values
    • Constructor Detail

      • AsyncRabbitTemplate

        public AsyncRabbitTemplate​(ConnectionFactory connectionFactory,
                                   java.lang.String exchange,
                                   java.lang.String routingKey,
                                   java.lang.String replyQueue)
        Construct an instance using the provided arguments. Replies will be routed to the default exchange using the reply queue name as the routing key.
        Parameters:
        connectionFactory - the connection factory.
        exchange - the default exchange to which requests will be sent.
        routingKey - the default routing key.
        replyQueue - the name of the reply queue to listen for replies.
      • AsyncRabbitTemplate

        public AsyncRabbitTemplate​(ConnectionFactory connectionFactory,
                                   java.lang.String exchange,
                                   java.lang.String routingKey,
                                   java.lang.String replyQueue,
                                   java.lang.String replyAddress)
        Construct an instance using the provided arguments. If 'replyAddress' is null, replies will be routed to the default exchange using the reply queue name as the routing key. Otherwise it should have the form exchange/routingKey and must cause messages to be routed to the reply queue.
        Parameters:
        connectionFactory - the connection factory.
        exchange - the default exchange to which requests will be sent.
        routingKey - the default routing key.
        replyQueue - the name of the reply queue to listen for replies.
        replyAddress - the reply address (exchange/routingKey).
      • AsyncRabbitTemplate

        public AsyncRabbitTemplate​(RabbitTemplate template,
                                   AbstractMessageListenerContainer container,
                                   java.lang.String replyAddress)
        Construct an instance using the provided arguments. The first queue the container is configured to listen to will be used as the reply queue. If 'replyAddress' is null, replies will be routed using the default exchange with that queue name as the routing key. Otherwise it should have the form exchange/routingKey and must cause messages to be routed to the reply queue.
        Parameters:
        template - a RabbitTemplate.
        container - a AbstractMessageListenerContainer.
        replyAddress - the reply address.
      • AsyncRabbitTemplate

        public AsyncRabbitTemplate​(ConnectionFactory connectionFactory,
                                   java.lang.String exchange,
                                   java.lang.String routingKey)
        Construct an instance using the provided arguments. "Direct replyTo" is used for replies.
        Parameters:
        connectionFactory - the connection factory.
        exchange - the default exchange to which requests will be sent.
        routingKey - the default routing key.
        Since:
        2.0
      • AsyncRabbitTemplate

        public AsyncRabbitTemplate​(RabbitTemplate template)
        Construct an instance using the provided arguments. "Direct replyTo" is used for replies.
        Parameters:
        template - a RabbitTemplate
        Since:
        2.0
    • Method Detail

      • setAutoStartup

        public void setAutoStartup​(boolean autoStartup)
        Parameters:
        autoStartup - true for auto start.
        See Also:
        isAutoStartup()
      • setPhase

        public void setPhase​(int phase)
        Parameters:
        phase - the phase.
        See Also:
        getPhase()
      • setMandatory

        public void setMandatory​(boolean mandatory)
        Set to true to enable the receipt of returned messages that cannot be delivered in the form of a AmqpMessageReturnedException.
        Parameters:
        mandatory - true to enable returns.
      • setMandatoryExpression

        public void setMandatoryExpression​(org.springframework.expression.Expression mandatoryExpression)
        Parameters:
        mandatoryExpression - a SpEL Expression to evaluate against each request message. The result of the evaluation must be a boolean value.
        Since:
        2.0
      • setMandatoryExpressionString

        public void setMandatoryExpressionString​(java.lang.String mandatoryExpression)
        Parameters:
        mandatoryExpression - a SpEL Expression to evaluate against each request message. The result of the evaluation must be a boolean value.
        Since:
        2.0
      • setEnableConfirms

        public void setEnableConfirms​(boolean enableConfirms)
        Set to true to enable publisher confirms. When enabled, the AsyncRabbitTemplate.RabbitFuture returned by the send and receive operation will have a ListenableFuture<Boolean> in its confirm property.
        Parameters:
        enableConfirms - true to enable publisher confirms.
      • getBeanName

        public java.lang.String getBeanName()
      • setBeanName

        public void setBeanName​(java.lang.String beanName)
        Specified by:
        setBeanName in interface org.springframework.beans.factory.BeanNameAware
      • setReceiveTimeout

        public void setReceiveTimeout​(long receiveTimeout)
        Set the receive timeout - the future returned by the send and receive methods will be canceled when this timeout expires. <= 0 means futures never expire. Beware that this will cause a memory leak if a reply is not received. Default: 30000 (30 seconds).
        Parameters:
        receiveTimeout - the timeout in milliseconds.
      • setTaskScheduler

        public void setTaskScheduler​(org.springframework.scheduling.TaskScheduler taskScheduler)
        Set the task scheduler to expire timed out futures.
        Parameters:
        taskScheduler - the task scheduler
        See Also:
        setReceiveTimeout(long)
      • getMessageConverter

        public org.springframework.amqp.support.converter.MessageConverter getMessageConverter()
        Returns:
        a reference to the underlying RabbitTemplate's MessageConverter.
      • getRabbitTemplate

        public RabbitTemplate getRabbitTemplate()
        Return the underlying RabbitTemplate used for sending.
        Returns:
        the template.
        Since:
        2.2
      • sendAndReceive

        public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive​(org.springframework.amqp.core.Message message)
        Specified by:
        sendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • sendAndReceive

        public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive​(java.lang.String routingKey,
                                                                      org.springframework.amqp.core.Message message)
        Specified by:
        sendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • sendAndReceive

        public AsyncRabbitTemplate.RabbitMessageFuture sendAndReceive​(java.lang.String exchange,
                                                                      java.lang.String routingKey,
                                                                      org.springframework.amqp.core.Message message)
        Specified by:
        sendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceive

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive​(java.lang.Object object)
        Specified by:
        convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceive

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive​(java.lang.String routingKey,
                                                                                      java.lang.Object object)
        Specified by:
        convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceive

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive​(java.lang.String exchange,
                                                                                      java.lang.String routingKey,
                                                                                      java.lang.Object object)
        Specified by:
        convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceive

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

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive​(java.lang.String routingKey,
                                                                                      java.lang.Object object,
                                                                                      org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
        Specified by:
        convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceive

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceive​(java.lang.String exchange,
                                                                                      java.lang.String routingKey,
                                                                                      java.lang.Object object,
                                                                                      org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
        Specified by:
        convertSendAndReceive in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceiveAsType

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

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType​(java.lang.String routingKey,
                                                                                            java.lang.Object object,
                                                                                            org.springframework.core.ParameterizedTypeReference<C> responseType)
        Specified by:
        convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceiveAsType

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType​(java.lang.String exchange,
                                                                                            java.lang.String routingKey,
                                                                                            java.lang.Object object,
                                                                                            org.springframework.core.ParameterizedTypeReference<C> responseType)
        Specified by:
        convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceiveAsType

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType​(java.lang.Object object,
                                                                                            org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
                                                                                            org.springframework.core.ParameterizedTypeReference<C> responseType)
        Specified by:
        convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceiveAsType

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType​(java.lang.String routingKey,
                                                                                            java.lang.Object object,
                                                                                            org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
                                                                                            org.springframework.core.ParameterizedTypeReference<C> responseType)
        Specified by:
        convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • convertSendAndReceiveAsType

        public <C> AsyncRabbitTemplate.RabbitConverterFuture<C> convertSendAndReceiveAsType​(java.lang.String exchange,
                                                                                            java.lang.String routingKey,
                                                                                            java.lang.Object object,
                                                                                            org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
                                                                                            org.springframework.core.ParameterizedTypeReference<C> responseType)
        Specified by:
        convertSendAndReceiveAsType in interface org.springframework.amqp.core.AsyncAmqpTemplate
      • start

        public void start()
        Specified by:
        start in interface org.springframework.context.Lifecycle
      • stop

        public void stop()
        Specified by:
        stop in interface org.springframework.context.Lifecycle
      • isRunning

        public boolean isRunning()
        Specified by:
        isRunning in interface org.springframework.context.Lifecycle
      • getPhase

        public int getPhase()
        Specified by:
        getPhase in interface org.springframework.context.Phased
        Specified by:
        getPhase in interface org.springframework.context.SmartLifecycle
      • isAutoStartup

        public boolean isAutoStartup()
        Specified by:
        isAutoStartup in interface org.springframework.context.SmartLifecycle
      • onMessage

        public void onMessage​(org.springframework.amqp.core.Message message,
                              com.rabbitmq.client.Channel channel)
        Description copied from interface: ChannelAwareMessageListener
        Callback for processing a received Rabbit message.

        Implementors are supposed to process the given Message, typically sending reply messages through the given Session.

        Specified by:
        onMessage in interface ChannelAwareMessageListener
        Parameters:
        message - the received AMQP message (never null)
        channel - the underlying Rabbit Channel (never null unless called by the stream listener container).
      • confirm

        public void confirm​(@NonNull
                            CorrelationData correlationData,
                            boolean ack,
                            @Nullable
                            java.lang.String cause)
        Description copied from interface: RabbitTemplate.ConfirmCallback
        Confirmation callback.
        Specified by:
        confirm in interface RabbitTemplate.ConfirmCallback
        Parameters:
        correlationData - correlation data for the callback.
        ack - true for ack, false for nack
        cause - An optional cause, for nack, when available, otherwise null.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object