Class ListenerUtils

java.lang.Object
org.springframework.kafka.listener.ListenerUtils

public final class ListenerUtils extends Object
Listener utilities.
Since:
2.0
Author:
Gary Russell, Francois Rosiere, Antonio Tomac, Wang Zhiyang, Sanghyeok An
  • Method Details

    • determineListenerType

      public static ListenerType determineListenerType(Object listener)
      Determine the type of the listener.
      Parameters:
      listener - the listener.
      Returns:
      the ListenerType.
    • unrecoverableBackOff

      public static void unrecoverableBackOff(BackOff backOff, Map<Thread, BackOffExecution> executions, Map<Thread,Long> lastIntervals, @Nullable MessageListenerContainer container) throws InterruptedException
      Sleep according to the BackOff; when the BackOffExecution returns BackOffExecution.STOP sleep for the previous backOff.
      Parameters:
      backOff - the BackOff to create a new BackOffExecution.
      executions - a thread local containing the BackOffExecution for this thread.
      lastIntervals - a thread local containing the previous BackOff interval for this thread.
      container - the container or parent container.
      Throws:
      InterruptedException - if the thread is interrupted.
      Since:
      3.1
    • stoppableSleep

      public static void stoppableSleep(@Nullable MessageListenerContainer container, long interval) throws InterruptedException
      Sleep for the desired timeout, as long as the container continues to run.
      Parameters:
      container - the container.
      interval - the timeout.
      Throws:
      InterruptedException - if the thread is interrupted.
      Since:
      2.7
    • conditionalSleep

      public static void conditionalSleep(Supplier<Boolean> shouldSleepCondition, long interval) throws InterruptedException
      Sleep for the desired timeout, as long as shouldSleepCondition supplies true.
      Parameters:
      shouldSleepCondition - to.
      interval - the timeout.
      Throws:
      InterruptedException - if the thread is interrupted.
      Since:
      3.0.9
    • conditionalSleepWithPoll

      public static void conditionalSleepWithPoll(Supplier<Boolean> shouldSleepCondition, long interval, org.apache.kafka.clients.consumer.Consumer<?,?> consumer) throws InterruptedException
      Sleep for the desired timeout, as long as shouldSleepCondition supplies true. This method requires that the consumer is paused; otherwise, ConsumerRecord may be lost. Periodically calls Consumer.poll(Duration.ZERO) to prevent a paused consumer from being rebalanced.
      Parameters:
      shouldSleepCondition - to.
      interval - the timeout.
      consumer - the kafka consumer to call poll().
      Throws:
      InterruptedException - if the thread is interrupted.
    • createOffsetAndMetadata

      public static org.apache.kafka.clients.consumer.OffsetAndMetadata createOffsetAndMetadata(@Nullable MessageListenerContainer container, long offset)
      Create a new OffsetAndMetadata using the given container and offset.
      Parameters:
      container - a container.
      offset - an offset.
      Returns:
      an offset and metadata.
      Since:
      2.8.6