@ThreadSafe public class KafkaCluster extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
KafkaCluster.InteractiveConsumer<K,V>
A simple interactive Kafka consumer for use with the cluster.
|
static interface |
KafkaCluster.InteractiveProducer<K,V>
A simple interactive Kafka producer for use with the cluster.
|
class |
KafkaCluster.Usage
A set of methods to use a running KafkaCluster.
|
| Modifier and Type | Field and Description |
|---|---|
private File |
dataDir |
static boolean |
DEFAULT_DELETE_DATA_PRIOR_TO_STARTUP |
static boolean |
DEFAULT_DELETE_DATA_UPON_SHUTDOWN |
private boolean |
deleteDataPriorToStartup |
private boolean |
deleteDataUponShutdown |
private Properties |
kafkaConfig |
private ConcurrentMap<Integer,KafkaServer> |
kafkaServers |
private static org.slf4j.Logger |
LOGGER |
private AtomicLong |
nextKafkaPort |
private boolean |
running |
private int |
startingKafkaPort |
private ZookeeperServer |
zkServer |
| Constructor and Description |
|---|
KafkaCluster()
Create a new embedded cluster.
|
| Modifier and Type | Method and Description |
|---|---|
KafkaCluster |
addBrokers(int count)
Add a number of new Kafka broker to the cluster.
|
String |
brokerList()
Get the list of brokers.
|
void |
createTopic(String topic,
int numPartitions,
int replicationFactor)
Create the specified topic.
|
void |
createTopics(int numPartitions,
int replicationFactor,
Set<String> topics)
Create the specified topics.
|
void |
createTopics(int numPartitions,
int replicationFactor,
String... topics)
Create the specified topics.
|
void |
createTopics(Set<String> topics)
Create the specified topics.
|
void |
createTopics(String... topics)
Create the specified topics.
|
KafkaCluster |
deleteDataPriorToStartup(boolean delete)
Specify whether the data is to be deleted prior to
startup(). |
KafkaCluster |
deleteDataUponShutdown(boolean delete)
Specify whether the data is to be deleted upon
shutdown(). |
boolean |
isRunning()
Determine if the cluster is running.
|
(package private) void |
onEachDirectory(Consumer<File> consumer)
Perform the supplied function on each directory used by this cluster.
|
KafkaCluster |
shutdown()
Shutdown the embedded Zookeeper server and the Kafka servers
in the cluster. |
private void |
shutdownReliably(KafkaServer server) |
KafkaCluster |
startup()
Start the embedded Zookeeper server and the Kafka servers
in the cluster. |
KafkaCluster.Usage |
useTo()
Obtain the interface for using this cluster.
|
KafkaCluster |
usingDirectory(File dataDir)
Set the parent directory where the brokers logs and server's logs and snapshots will be kept.
|
KafkaCluster |
withKafkaConfiguration(Properties properties)
Set the configuration properties for each of the brokers.
|
KafkaCluster |
withPorts(int zkPort,
int firstKafkaPort)
Set the port numbers for Zookeeper and the Kafka brokers.
|
int |
zkPort()
Get the Zookeeper port.
|
public static final boolean DEFAULT_DELETE_DATA_UPON_SHUTDOWN
public static final boolean DEFAULT_DELETE_DATA_PRIOR_TO_STARTUP
private static final org.slf4j.Logger LOGGER
private final ConcurrentMap<Integer,KafkaServer> kafkaServers
private final ZookeeperServer zkServer
private volatile File dataDir
private volatile boolean deleteDataUponShutdown
private volatile boolean deleteDataPriorToStartup
private volatile boolean running
private volatile Properties kafkaConfig
private volatile int startingKafkaPort
private final AtomicLong nextKafkaPort
public KafkaCluster deleteDataUponShutdown(boolean delete)
shutdown().delete - true if the data is to be deleted upon shutdown, or false otherwiseIllegalStateException - if the cluster is runningpublic KafkaCluster deleteDataPriorToStartup(boolean delete)
startup().delete - true if the data is to be deleted upon shutdown, or false otherwiseIllegalStateException - if the cluster is runningpublic KafkaCluster addBrokers(int count)
count - the number of new brokers to addIllegalStateException - if the cluster is runningpublic KafkaCluster usingDirectory(File dataDir)
dataDir - the parent directory for the server's logs and snapshots; may be null if a temporary directory will be usedIllegalStateException - if the cluster is runningIllegalArgumentException - if the supplied file is not a directory or not writablepublic KafkaCluster withKafkaConfiguration(Properties properties)
properties - the Kafka configuration propertiesIllegalStateException - if the cluster is runningpublic KafkaCluster withPorts(int zkPort, int firstKafkaPort)
zkPort - the port number that Zookeeper should use; may be -1 if an available port should be discoveredfirstKafkaPort - the port number for the first Kafka broker (additional brokers will use subsequent port numbers);
may be -1 if available ports should be discoveredIllegalStateException - if the cluster is runningpublic boolean isRunning()
public KafkaCluster startup() throws IOException
in the cluster.
This method does nothing if the cluster is already running.IOException - if there is an error during startuppublic KafkaCluster shutdown()
in the cluster.
This method does nothing if the cluster is not running.public void createTopics(String... topics)
topics - the names of the topics to createIllegalStateException - if the cluster is not runningpublic void createTopics(Set<String> topics)
topics - the names of the topics to createIllegalStateException - if the cluster is not runningpublic void createTopics(int numPartitions,
int replicationFactor,
String... topics)
numPartitions - the number of partitions for each topicreplicationFactor - the replication factor for each topictopics - the names of the topics to createpublic void createTopics(int numPartitions,
int replicationFactor,
Set<String> topics)
numPartitions - the number of partitions for each topicreplicationFactor - the replication factor for each topictopics - the names of the topics to createpublic void createTopic(String topic, int numPartitions, int replicationFactor)
topic - the name of the topic to createnumPartitions - the number of partitions for the topicreplicationFactor - the replication factor for the topicvoid onEachDirectory(Consumer<File> consumer)
consumer - the consumer function; may not be nullpublic String brokerList()
public int zkPort()
private void shutdownReliably(KafkaServer server)
public KafkaCluster.Usage useTo()
IllegalStateException - if the cluster is not runningCopyright © 2020 JBoss by Red Hat. All rights reserved.