Package io.debezium.kafka
Class KafkaServer
- java.lang.Object
-
- io.debezium.kafka.KafkaServer
-
@ThreadSafe public class KafkaServer extends Object
A small embedded Kafka server.- Author:
- Randall Hauch
-
-
Field Summary
Fields Modifier and Type Field Description private kafka.zk.AdminZkClientadminZkClientprivate intbrokerIdprivate Propertiesconfigstatic intDEFAULT_BROKER_IDprivate intdesiredPortprivate static org.slf4j.LoggerLOGGERprivate FilelogsDirprivate intportprivate kafka.server.KafkaServerserverprivate Supplier<String>zkConnection
-
Constructor Summary
Constructors Constructor Description KafkaServer(Supplier<String> zookeeperConnection)Create a new server instance.KafkaServer(Supplier<String> zookeeperConnection, int brokerId)Create a new server instance.KafkaServer(Supplier<String> zookeeperConnection, int brokerId, int port)Create a new server instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intbrokerId()Propertiesconfig()Get a copy of the complete configuration that is or will be used by the running server.voidcreateTopic(String topic, int numPartitions, int replicationFactor)Create the specified topic.voidcreateTopics(int numPartitions, int replicationFactor, String... topics)Create the specified topics.voidcreateTopics(String... topics)Create the specified topics.voiddeleteData()Delete all of the data associated with this server.kafka.zk.AdminZkClientgetAdminZkClient()Get the Zookeeper admin client used by the running Kafka server.StringgetConnection()Get the connection string.FilegetStateDirectory()Get the parent directory where the broker's state will be kept.(package private) voidonEachDirectory(Consumer<File> consumer)Perform the supplied function on each directory used by this server.protected voidpopulateDefaultConfiguration(Properties props)Set the initial default configuration properties.KafkaServersetPort(int port)Set the port for the server.KafkaServersetProperties(Properties properties)Set multiple configuration properties.KafkaServersetProperty(String name, String value)Set a configuration property.voidsetStateDirectory(File stateDirectory)Set the parent directory where the broker's state will be kept.voidshutdown(boolean deleteLogs)Shutdown the embedded Kafka server and delete all data.KafkaServerstartup()Start the embedded Kafka server.StringtoString()protected StringzookeeperConnection()
-
-
-
Field Detail
-
DEFAULT_BROKER_ID
public static final int DEFAULT_BROKER_ID
- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
brokerId
private final int brokerId
-
logsDir
private volatile File logsDir
-
config
private final Properties config
-
desiredPort
private volatile int desiredPort
-
port
private volatile int port
-
server
private volatile kafka.server.KafkaServer server
-
adminZkClient
private volatile kafka.zk.AdminZkClient adminZkClient
-
-
Constructor Detail
-
KafkaServer
public KafkaServer(Supplier<String> zookeeperConnection)
Create a new server instance.- Parameters:
zookeeperConnection- the supplier of the Zookeeper connection string; may not be null
-
KafkaServer
public KafkaServer(Supplier<String> zookeeperConnection, int brokerId)
Create a new server instance.- Parameters:
zookeeperConnection- the supplier of the Zookeeper connection string; may not be nullbrokerId- the unique broker ID
-
-
Method Detail
-
brokerId
protected int brokerId()
-
zookeeperConnection
protected String zookeeperConnection()
-
populateDefaultConfiguration
protected void populateDefaultConfiguration(Properties props)
Set the initial default configuration properties. This method is called from the constructors and can be overridden to customize these properties.- Parameters:
props- the configuration properties; never null
-
setProperty
public KafkaServer setProperty(String name, String value)
Set a configuration property. Several key properties that deal with Zookeeper, the host name, and the broker ID, may not be set via this method and are ignored since they are controlled elsewhere in this instance.- Parameters:
name- the property name; may not be nullvalue- the property value; may be null- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the server is running when this method is called
-
setProperties
public KafkaServer setProperties(Properties properties)
Set multiple configuration properties. Several key properties that deal with Zookeeper, the host name, and the broker ID, may not be set via this method and are ignored since they are controlled elsewhere in this instance.- Parameters:
properties- the configuration properties; may be null or empty- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the server is running when this method is called
-
setPort
public KafkaServer setPort(int port)
Set the port for the server.- Parameters:
port- the desired port, or-1if a random available port should be found and used- Returns:
- this instance to allow chaining methods; never null
-
config
public Properties config()
Get a copy of the complete configuration that is or will be used by the running server.- Returns:
- the properties for the currently-running server; may be empty if not running
-
getConnection
public String getConnection()
Get the connection string. If the server is notrunningand the port is to be dynamically discovered upon startup, then this method returns "localhost:-1".- Returns:
- the connection string; never null
-
startup
public KafkaServer startup()
Start the embedded Kafka server.- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the server is already running
-
shutdown
public void shutdown(boolean deleteLogs)
Shutdown the embedded Kafka server and delete all data.- Parameters:
deleteLogs- whether or not to remove all the log files after shutting down
-
deleteData
public void deleteData()
Delete all of the data associated with this server.
-
getAdminZkClient
public kafka.zk.AdminZkClient getAdminZkClient()
Get the Zookeeper admin client used by the running Kafka server.- Returns:
- the Zookeeper admin client, or null if the Kafka server is not running
-
createTopics
public void createTopics(String... topics)
Create the specified topics.- Parameters:
topics- the names of the topics to create
-
createTopics
public void createTopics(int numPartitions, int replicationFactor, String... topics)Create the specified topics.- Parameters:
numPartitions- the number of partitions for each topicreplicationFactor- the replication factor for each topictopics- the names of the topics to create
-
createTopic
public void createTopic(String topic, int numPartitions, int replicationFactor)
Create the specified topic.- Parameters:
topic- the name of the topic to createnumPartitions- the number of partitions for the topicreplicationFactor- the replication factor for the topic
-
onEachDirectory
void onEachDirectory(Consumer<File> consumer)
Perform the supplied function on each directory used by this server.- Parameters:
consumer- the consumer function; may not be null
-
getStateDirectory
public File getStateDirectory()
Get the parent directory where the broker's state will be kept. The broker will create a subdirectory for itself under this directory.- Returns:
- the parent directory for the broker's state; may be null if a temporary directory will be used
-
setStateDirectory
public void setStateDirectory(File stateDirectory)
Set the parent directory where the broker's state will be kept. The broker will create a subdirectory for itself under this directory.- Parameters:
stateDirectory- the parent directory for the broker's state; may be null if a temporary directory will be used- Throws:
IllegalArgumentException- if the supplied file is not a directory or not writable
-
-