Skip to content

Commit

Permalink
Replace usage of transitive shaded dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Ross <[email protected]>
  • Loading branch information
andrross committed Jan 29, 2025
1 parent fd37ac8 commit 9a4214b
Showing 1 changed file with 7 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,11 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;

import static org.awaitility.Awaitility.await;

public class KafkaUtils {
private static final Logger LOGGER = LogManager.getLogger(KafkaUtils.class);

/**
* Creates kafka topic
*
* @param topicName the topic name
* @param bootstrapServer kafka bootstrap server list
*/
public static void createTopic(String topicName, String bootstrapServer) {
createTopic(topicName, 1, bootstrapServer);
}

public static void createTopic(String topicName, int numOfPartitions, String bootstrapServers) {
try {
getAdminClient(bootstrapServers, (client -> {
Expand All @@ -60,28 +48,24 @@ public static void createTopic(String topicName, int numOfPartitions, String boo

public static boolean checkTopicExistence(String topicName, String bootstrapServers) {
return getAdminClient(bootstrapServers, (client -> {
Map<String, KafkaFuture<TopicDescription>> topics = client.describeTopics(List.of(topicName)).values();
Map<String, KafkaFuture<TopicDescription>> topics = client.describeTopics(List.of(topicName)).topicNameValues();

try {
return topics.containsKey(topicName) && topics.get(topicName).get().name().equals(topicName);
} catch (InterruptedException e) {
LOGGER.error("error on checkTopicExistence", e);
return false;
} catch (ExecutionException e) {
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("error on checkTopicExistence", e);
return false;
}
}));
}

private static <Rep> Rep getAdminClient(String bootstrapServer, Function<AdminClient, Rep> function) {
AdminClient adminClient = KafkaAdminClient.create(
ImmutableMap.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer, AdminClientConfig.CLIENT_ID_CONFIG, "test")
);
try {
try (
AdminClient adminClient = KafkaAdminClient.create(
Map.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer, AdminClientConfig.CLIENT_ID_CONFIG, "test")
)
) {
return function.apply(adminClient);
} finally {
adminClient.close();
}
}
}

0 comments on commit 9a4214b

Please sign in to comment.