net.mguenther.kafka.junit.EmbeddedKafkaCluster Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kafka-junit Show documentation
Show all versions of kafka-junit Show documentation
Provides an embedded Kafka cluster consisting of Apache ZooKeeper, Apache Kafka Brokers and Kafka Connect
workers in distributed mode along with a rich set of convenient accessors and fault injectors to interact
with the embedded Kafka cluster. Supports working against external clusters as well.
package net.mguenther.kafka.junit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.mguenther.kafka.junit.provider.DefaultRecordConsumer;
import net.mguenther.kafka.junit.provider.DefaultRecordProducer;
import net.mguenther.kafka.junit.provider.DefaultTopicManager;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.junit.rules.ExternalResource;
import java.util.List;
@Slf4j
@RequiredArgsConstructor
public class EmbeddedKafkaCluster extends ExternalResource implements EmbeddedLifecycle, RecordProducer, RecordConsumer, TopicManager, AutoCloseable {
private final EmbeddedKafkaClusterConfig config;
private EmbeddedZooKeeper zooKeeper;
private EmbeddedKafka broker;
private EmbeddedConnect connect;
private RecordProducer producerDelegate;
private RecordConsumer consumerDelegate;
private TopicManager topicManagerDelegate;
@Override
protected void before() throws Throwable {
start();
}
@Override
protected void after() {
stop();
}
@Override
public void start() {
try {
zooKeeper = new EmbeddedZooKeeper(config.getZooKeeperConfig());
zooKeeper.start();
broker = new EmbeddedKafka(config.getKafkaConfig(), zooKeeper.getConnectString());
broker.start();
if (config.usesConnect()) {
connect = new EmbeddedConnect(config.getConnectConfig(), getBrokerList());
connect.start();
}
producerDelegate = new DefaultRecordProducer(getBrokerList());
consumerDelegate = new DefaultRecordConsumer(getBrokerList());
topicManagerDelegate = new DefaultTopicManager(zooKeeper.getConnectString());
} catch (Exception e) {
throw new RuntimeException("Unable to start the embedded Kafka cluster.", e);
}
}
@Override
public void stop() {
if (connect != null) {
connect.stop();
}
broker.stop();
zooKeeper.stop();
}
public String getBrokerList() {
return broker.getBrokerList();
}
public static EmbeddedKafkaCluster provisionWith(final EmbeddedKafkaClusterConfig config) {
return new EmbeddedKafkaCluster(config);
}
@Override
public List send(final SendKeyValues sendRequest) throws InterruptedException {
return producerDelegate.send(sendRequest);
}
@Override
public List send(final SendKeyValuesTransactional sendRequest) throws InterruptedException {
return producerDelegate.send(sendRequest);
}
@Override
public List send(final SendValues sendRequest) throws InterruptedException {
return producerDelegate.send(sendRequest);
}
@Override
public List send(final SendValuesTransactional sendRequest) throws InterruptedException {
return producerDelegate.send(sendRequest);
}
@Override
public List readValues(final ReadKeyValues readRequest) throws InterruptedException {
return consumerDelegate.readValues(readRequest);
}
@Override
public List> read(final ReadKeyValues readRequest) throws InterruptedException {
return consumerDelegate.read(readRequest);
}
@Override
public List observeValues(final ObserveKeyValues observeRequest) throws InterruptedException {
return consumerDelegate.observeValues(observeRequest);
}
@Override
public List> observe(final ObserveKeyValues observeRequest) throws InterruptedException {
return consumerDelegate.observe(observeRequest);
}
@Override
public void createTopic(final TopicConfig topicConfig) {
topicManagerDelegate.createTopic(topicConfig);
}
@Override
public void deleteTopic(final String topic) {
topicManagerDelegate.deleteTopic(topic);
}
@Override
public boolean exists(final String topic) {
return topicManagerDelegate.exists(topic);
}
@Override
public void close() throws Exception {
stop();
}
}