Package pw.avvero.test.kafka
Class KafkaSupport
java.lang.Object
pw.avvero.test.kafka.KafkaSupport
Utility class providing support functions for Kafka in Spring applications.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intstatic int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetOffsetsForConsumerGroups(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> consumerGroups, Set<org.apache.kafka.common.TopicPartition> topicPartitions) getOffsetsForPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<org.apache.kafka.common.TopicPartition> topicPartitions) static Set<org.apache.kafka.common.TopicPartition>getPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> topics) static voidwaitForPartitionAssignment(org.springframework.context.ApplicationContext applicationContext) Waits for the partition assignment for all Kafka listener containers in the application context.static voidwaitForPartitionOffsetCommit(List<String> bootstrapServers) Waits for the offset commit across all consumer groups for all topics in the provided list of bootstrap servers.static voidwaitForPartitionOffsetCommit(org.apache.kafka.clients.admin.AdminClient adminClient) static voidwaitForPartitionOffsetCommit(org.springframework.context.ApplicationContext applicationContext) Waits for the offset commit for a given list of bootstrap servers retrieved from the application context.static voidwaitForPartitionOffsetCommitForPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<org.apache.kafka.common.TopicPartition> topicPartitions) static voidwaitForPartitionOffsetCommitForPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<org.apache.kafka.common.TopicPartition> topicPartitions, Set<String> consumerGroups) static voidwaitForPartitionOffsetCommitForTopics(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> topics)
-
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
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.
-
waitForPartitionOffsetCommit
public static void waitForPartitionOffsetCommit(org.apache.kafka.clients.admin.AdminClient adminClient) throws ExecutionException, InterruptedException -
waitForPartitionOffsetCommitForTopics
public static void waitForPartitionOffsetCommitForTopics(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> topics) throws ExecutionException, InterruptedException -
waitForPartitionOffsetCommitForPartitions
public static void waitForPartitionOffsetCommitForPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<org.apache.kafka.common.TopicPartition> topicPartitions) throws ExecutionException, InterruptedException -
waitForPartitionOffsetCommitForPartitions
public static void waitForPartitionOffsetCommitForPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<org.apache.kafka.common.TopicPartition> topicPartitions, Set<String> consumerGroups) throws InterruptedException, ExecutionException -
getPartitions
public static Set<org.apache.kafka.common.TopicPartition> getPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> topics) throws ExecutionException, InterruptedException -
getOffsetsForPartitions
public static Map<org.apache.kafka.common.TopicPartition,Long> getOffsetsForPartitions(org.apache.kafka.clients.admin.AdminClient adminClient, Set<org.apache.kafka.common.TopicPartition> topicPartitions) throws ExecutionException, InterruptedException -
getOffsetsForConsumerGroups
public static Map<String,Map<org.apache.kafka.common.TopicPartition, getOffsetsForConsumerGroupsLong>> (org.apache.kafka.clients.admin.AdminClient adminClient, Set<String> consumerGroups, Set<org.apache.kafka.common.TopicPartition> topicPartitions) throws ExecutionException, InterruptedException
-