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:
parent
2d5e7da1fa
commit
4e18fd0aa9
6
apache-kafka-2/README.md
Normal file
6
apache-kafka-2/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
## Apache Kafka
|
||||||
|
|
||||||
|
This module contains articles about Apache Kafka.
|
||||||
|
|
||||||
|
##### Building the project
|
||||||
|
You can build the project from the command line using: *mvn clean install*, or in an IDE.
|
1
apache-kafka-2/log4j.properties
Normal file
1
apache-kafka-2/log4j.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
log4j.rootLogger=INFO, stdout
|
69
apache-kafka-2/pom.xml
Normal file
69
apache-kafka-2/pom.xml
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>apache-kafka-2</artifactId>
|
||||||
|
<name>apache-kafka-2</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.kafka</groupId>
|
||||||
|
<artifactId>kafka-clients</artifactId>
|
||||||
|
<version>${kafka.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>${guava.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>kafka</artifactId>
|
||||||
|
<version>${testcontainers-kafka.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${testcontainers-jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.java.dev.jna</groupId>
|
||||||
|
<artifactId>jna</artifactId>
|
||||||
|
<version>5.7.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<kafka.version>2.8.0</kafka.version>
|
||||||
|
<testcontainers-kafka.version>1.15.3</testcontainers-kafka.version>
|
||||||
|
<testcontainers-jupiter.version>1.15.3</testcontainers-jupiter.version>
|
||||||
|
<lombok.version>1.18.20</lombok.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.kafka;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.testcontainers.containers.KafkaContainer;
|
||||||
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||||
|
import org.testcontainers.utility.DockerImageName;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
// This live test needs a running Docker instance so that a kafka container can be created
|
||||||
|
|
||||||
|
@Testcontainers
|
||||||
|
class KafkaConnectionLiveTest {
|
||||||
|
|
||||||
|
@Container
|
||||||
|
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3"));
|
||||||
|
private KafkaAdminClient kafkaAdminClient;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
KAFKA_CONTAINER.addExposedPort(9092);
|
||||||
|
this.kafkaAdminClient = new KafkaAdminClient(KAFKA_CONTAINER.getBootstrapServers());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void destroy() {
|
||||||
|
KAFKA_CONTAINER.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenKafkaIsRunning_whenCheckedForConnection_thenConnectionIsVerified() throws Exception {
|
||||||
|
boolean alive = kafkaAdminClient.verifyConnection();
|
||||||
|
assertThat(alive).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
2
pom.xml
2
pom.xml
@ -338,6 +338,7 @@
|
|||||||
|
|
||||||
<module>apache-cxf-modules</module>
|
<module>apache-cxf-modules</module>
|
||||||
<module>apache-kafka</module>
|
<module>apache-kafka</module>
|
||||||
|
<module>apache-kafka-2</module>
|
||||||
<module>apache-libraries</module>
|
<module>apache-libraries</module>
|
||||||
<module>apache-olingo</module>
|
<module>apache-olingo</module>
|
||||||
<module>apache-poi</module>
|
<module>apache-poi</module>
|
||||||
@ -769,6 +770,7 @@
|
|||||||
|
|
||||||
<module>apache-cxf-modules</module>
|
<module>apache-cxf-modules</module>
|
||||||
<module>apache-kafka</module>
|
<module>apache-kafka</module>
|
||||||
|
<module>apache-kafka-2</module>
|
||||||
<module>apache-libraries</module>
|
<module>apache-libraries</module>
|
||||||
<module>apache-olingo</module>
|
<module>apache-olingo</module>
|
||||||
<module>apache-poi</module>
|
<module>apache-poi</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user