BAEL-5680: Guide to Check if Apache Kafka Server is Running (#12600)

Co-authored-by: Tapan Avasthi <tavasthi@Tapans-MacBook-Air.local>
This commit is contained in:
Tapan Avasthi
2022-08-18 08:22:06 +05:30
committed by GitHub
parent 3a0c628d3f
commit 1894d7cb36
6 changed files with 152 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package com.baeldung.kafka;
import java.util.Collection;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.common.Node;
public class KafkaAdminClient {
private final AdminClient client;
public KafkaAdminClient(String bootstrap) {
Properties props = new Properties();
props.put("bootstrap.servers", bootstrap);
props.put("request.timeout.ms", 3000);
props.put("connections.max.idle.ms", 5000);
this.client = AdminClient.create(props);
}
public boolean verifyConnection() throws ExecutionException, InterruptedException {
Collection<Node> nodes = this.client.describeCluster()
.nodes()
.get();
return nodes != null && nodes.size() > 0;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
String defaultBootStrapServer = "localhost:9092";
KafkaAdminClient kafkaAdminClient = new KafkaAdminClient(defaultBootStrapServer);
System.out.println(kafkaAdminClient.verifyConnection());
}
}