Class KafkaSupport

java.lang.Object
pw.avvero.test.kafka.KafkaSupport

public class KafkaSupport extends Object
Utility class providing support functions for Kafka in Spring applications.
  • Field Details

    • OFFSET_COMMIT_WAIT_ATTEMPTS_MAX

      public static int OFFSET_COMMIT_WAIT_ATTEMPTS_MAX
    • OFFSET_COMMIT_WAIT_TIME

      public static int OFFSET_COMMIT_WAIT_TIME
  • Constructor Details

    • KafkaSupport

      public KafkaSupport()
  • Method Details

    • waitForPartitionAssignment

      public static void waitForPartitionAssignment(org.springframework.context.ApplicationContext applicationContext) throws Exception
      Waits for the partition assignment for all Kafka listener containers in the application context. This method ensures that each Kafka listener container is assigned at least one partition before proceeding. It also initializes Kafka producer by sending a test message.

      This method is useful in scenarios where the application needs to wait for the Kafka consumers to be fully set up and ready before performing certain operations.

      Parameters:
      applicationContext - the Spring application context containing the Kafka listener containers.
      Throws:
      Exception - if an error occurs during the process.
    • waitForPartitionOffsetCommit

      public static void waitForPartitionOffsetCommit(org.springframework.context.ApplicationContext applicationContext) throws ExecutionException, InterruptedException
      Waits for the offset commit for a given list of bootstrap servers retrieved from the application context.
      Parameters:
      applicationContext - The Spring application context from which to retrieve Kafka connection details.
      Throws:
      ExecutionException - if an error occurs during the fetching of consumer group or topic information.
      InterruptedException - if the current thread is interrupted while waiting.
    • waitForPartitionOffsetCommit

      public static void waitForPartitionOffsetCommit(List<String> bootstrapServers) throws InterruptedException, ExecutionException
      Waits for the offset commit across all consumer groups for all topics in the provided list of bootstrap servers. This method checks the offset commit for each partition of each topic and ensures that all consumer groups have committed their offsets. It continuously checks the offsets until they are committed or until a maximum number of attempts is reached.
      Parameters:
      bootstrapServers - The list of bootstrap servers for the Kafka cluster.
      Throws:
      InterruptedException - if the thread is interrupted while waiting for the offsets to commit.
      ExecutionException - if an error occurs during the fetching of consumer group or topic information.
    • getOffsetsForTopics

      public static Map<org.apache.kafka.common.TopicPartition,Long> getOffsetsForTopics(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> topics) throws ExecutionException, InterruptedException
      Throws:
      ExecutionException
      InterruptedException
    • getOffsetsForConsumerGroups

      public static Map<String,Long> getOffsetsForConsumerGroups(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> consumerGroups, org.apache.kafka.common.TopicPartition topicPartition) throws ExecutionException, InterruptedException
      Throws:
      ExecutionException
      InterruptedException