Class DirectMessageListenerContainer

All Implemented Interfaces:
MessageListenerContainer, Aware, BeanNameAware, DisposableBean, InitializingBean, ApplicationContextAware, ApplicationEventPublisherAware, Lifecycle, Phased, SmartLifecycle
Direct Known Subclasses:
DirectReplyToMessageListenerContainer

public class DirectMessageListenerContainer extends AbstractMessageListenerContainer
The SimpleMessageListenerContainer is not so simple. Recent changes to the rabbitmq java client has facilitated a much simpler listener container that invokes the listener directly on the rabbit client consumer thread. There is no txSize property - each message is acked (or nacked) individually.
Since:
2.0
Author:
Gary Russell, Artem Bilan, Nicolas Ristock, Cao Weibo
  • Field Details

  • Constructor Details

    • DirectMessageListenerContainer

      public DirectMessageListenerContainer()
      Create an instance; RabbitAccessor.setConnectionFactory(ConnectionFactory) must be called before starting.
    • DirectMessageListenerContainer

      public DirectMessageListenerContainer(ConnectionFactory connectionFactory)
      Create an instance with the provided connection factory.
      Parameters:
      connectionFactory - the connection factory.
  • Method Details

    • setConsumersPerQueue

      public void setConsumersPerQueue(int consumersPerQueue)
      Each queue runs in its own consumer; set this property to create multiple consumers for each queue. If the container is already running, the number of consumers per queue will be adjusted up or down as necessary.
      Parameters:
      consumersPerQueue - the consumers per queue.
    • setExclusive

      public final void setExclusive(boolean exclusive)
      Set to true for an exclusive consumer - if true, the consumers per queue must be 1.
      Overrides:
      setExclusive in class AbstractMessageListenerContainer
      Parameters:
      exclusive - true for an exclusive consumer.
    • setTaskScheduler

      public void setTaskScheduler(TaskScheduler taskScheduler)
      Set the task scheduler to use for the task that monitors idle containers and failed consumers.
      Parameters:
      taskScheduler - the scheduler.
    • setMonitorInterval

      public void setMonitorInterval(long monitorInterval)
      Set how often to run a task to check for failed consumers and idle containers.
      Parameters:
      monitorInterval - the interval; default 10000 but it will be adjusted down to the smallest of this, idleEventInterval / 2 (if configured) or failedDeclarationRetryInterval.
    • setQueueNames

      public void setQueueNames(String... queueName)
      Description copied from class: AbstractMessageListenerContainer
      Set the name of the queue(s) to receive messages from.
      Specified by:
      setQueueNames in interface MessageListenerContainer
      Overrides:
      setQueueNames in class AbstractMessageListenerContainer
      Parameters:
      queueName - the desired queueName(s) (can not be null)
    • setMissingQueuesFatal

      public final void setMissingQueuesFatal(boolean missingQueuesFatal)
      If all the configured queue(s) are not available on the broker, this setting determines whether the condition is fatal. When true, and the queues are missing during startup, the context refresh() will fail.

      When false, the condition is not considered fatal and the container will continue to attempt to start the consumers.

      Defaults to false for this container.

      Overrides:
      setMissingQueuesFatal in class AbstractMessageListenerContainer
      Parameters:
      missingQueuesFatal - the missingQueuesFatal to set.
      See Also:
    • setMessagesPerAck

      public void setMessagesPerAck(int messagesPerAck)
      Set the number of messages to receive before acknowledging (success). A failed message will short-circuit this counter.
      Parameters:
      messagesPerAck - the number of messages.
      See Also:
    • setAckTimeout

      public void setAckTimeout(long ackTimeout)
      An approximate timeout; when messagesPerAck is greater than 1, and this time elapses since the last ack, the pending acks will be sent either when the next message arrives, or a short time later if no additional messages arrive. In that case, the actual time depends on the monitorInterval.
      Parameters:
      ackTimeout - the timeout in milliseconds (default 20000);
      See Also:
    • addQueueNames

      public void addQueueNames(String... queueNames)
      Description copied from class: AbstractMessageListenerContainer
      Add queue(s) to this container's list of queues.
      Overrides:
      addQueueNames in class AbstractMessageListenerContainer
      Parameters:
      queueNames - The queue(s) to add.
    • addQueues

      public void addQueues(Queue... queues)
      Description copied from class: AbstractMessageListenerContainer
      Add queue(s) to this container's list of queues.
      Overrides:
      addQueues in class AbstractMessageListenerContainer
      Parameters:
      queues - The queue(s) to add.
    • removeQueueNames

      public boolean removeQueueNames(String... queueNames)
      Description copied from class: AbstractMessageListenerContainer
      Remove queue(s) from this container's list of queues.
      Overrides:
      removeQueueNames in class AbstractMessageListenerContainer
      Parameters:
      queueNames - The queue(s) to remove.
      Returns:
      the boolean result of removal on the target queueNames List.
    • removeQueues

      public boolean removeQueues(Queue... queues)
      Description copied from class: AbstractMessageListenerContainer
      Remove queue(s) from this container's list of queues.
      Overrides:
      removeQueues in class AbstractMessageListenerContainer
      Parameters:
      queues - The queue(s) to remove.
      Returns:
      the boolean result of removal on the target queueNames List.
    • findIdleConsumer

      protected int findIdleConsumer()
      When adjusting down, return a consumer that can be canceled. Called while locked on AbstractMessageListenerContainer.consumersLock.
      Returns:
      the consumer index or -1 if non-idle.
      Since:
      2.0.6
    • doInitialize

      protected void doInitialize()
      Description copied from class: AbstractMessageListenerContainer
      Register any invokers within this container.

      Subclasses need to implement this method for their specific invoker management process.

      Specified by:
      doInitialize in class AbstractMessageListenerContainer
    • doStart

      protected void doStart()
      Description copied from class: AbstractMessageListenerContainer
      Start this container, and notify all invoker tasks.
      Overrides:
      doStart in class AbstractMessageListenerContainer
    • doStop

      protected void doStop()
      Description copied from class: AbstractMessageListenerContainer
      This method is invoked when the container is stopping.
      Overrides:
      doStop in class AbstractMessageListenerContainer
    • actualStart

      protected void actualStart()
    • checkConnect

      protected void checkConnect()
    • doRedeclareElementsIfNecessary

      protected void doRedeclareElementsIfNecessary()
    • processMonitorTask

      protected void processMonitorTask()
      Subclasses can override this to take additional actions when the monitor task runs.
    • shutdownAndWaitOrCallback

      protected void shutdownAndWaitOrCallback(@Nullable Runnable callback)
      Overrides:
      shutdownAndWaitOrCallback in class AbstractMessageListenerContainer
    • consumerRemoved

      protected void consumerRemoved(DirectMessageListenerContainer.SimpleConsumer consumer)
      Called whenever a consumer is removed.
      Parameters:
      consumer - the consumer.