Package com.salesforce.kafka.test
Class KafkaTestServer
java.lang.Object
com.salesforce.kafka.test.KafkaTestServer
- All Implemented Interfaces:
KafkaCluster,KafkaProvider,AutoCloseable
public class KafkaTestServer extends Object implements KafkaCluster, KafkaProvider, AutoCloseable
This will spin up a ZooKeeper and Kafka server for use in integration tests. Simply
create an instance of KafkaTestServer and call start() and you can publish to Kafka
topics in an integration test. Be sure to call shutdown() when the test is complete
or use the AutoCloseable interface.
-
Constructor Summary
Constructors Constructor Description KafkaTestServer()Default constructor, no overridden broker properties.KafkaTestServer(Properties overrideBrokerProperties)Alternative constructor allowing override of brokerProperties.KafkaTestServer(Properties overrideBrokerProperties, Collection<BrokerListener> listeners)Alternative constructor allowing override of brokerProperties. -
Method Summary
Modifier and Type Method Description voidclose()Closes the internal servers.intgetBrokerId()Returns the brokers Id.KafkaBrokersgetKafkaBrokers()Returns an immutable list of broker hosts for the kafka cluster.StringgetKafkaConnectString()bootstrap.servers string to configure Kafka consumers or producers to access the Kafka cluster.List<ListenerProperties>getListenerProperties()Connection details about each of the registered listeners on the kafka broker.StringgetZookeeperConnectString()Returns properly formatted zookeeper connection string for zookeeper clients.voidstart()Creates and starts ZooKeeper and Kafka server instances.voidstop()Closes the internal servers.
-
Constructor Details
-
KafkaTestServer
public KafkaTestServer()Default constructor, no overridden broker properties. -
KafkaTestServer
Alternative constructor allowing override of brokerProperties.- Parameters:
overrideBrokerProperties- Define Kafka broker properties.- Throws:
IllegalArgumentException- if overrideBrokerProperties argument is null.
-
KafkaTestServer
public KafkaTestServer(Properties overrideBrokerProperties, Collection<BrokerListener> listeners) throws IllegalArgumentExceptionAlternative constructor allowing override of brokerProperties.- Parameters:
overrideBrokerProperties- Define Kafka broker properties.listeners- Listener definitions.- Throws:
IllegalArgumentException- if overrideBrokerProperties argument is null.
-
-
Method Details
-
getKafkaConnectString
bootstrap.servers string to configure Kafka consumers or producers to access the Kafka cluster.- Specified by:
getKafkaConnectStringin interfaceKafkaProvider- Returns:
- Connect string to use for Kafka clients.
-
getListenerProperties
Description copied from interface:KafkaProviderConnection details about each of the registered listeners on the kafka broker.- Specified by:
getListenerPropertiesin interfaceKafkaProvider- Returns:
- details about each of the registered listeners on the kafka broker.
-
getKafkaBrokers
Returns an immutable list of broker hosts for the kafka cluster.- Specified by:
getKafkaBrokersin interfaceKafkaProvider- Returns:
- immutable list of hosts for brokers within the cluster.
-
getBrokerId
public int getBrokerId()Returns the brokers Id.- Returns:
- This brokers Id.
-
getZookeeperConnectString
Returns properly formatted zookeeper connection string for zookeeper clients.- Specified by:
getZookeeperConnectStringin interfaceKafkaProvider- Returns:
- Connect string to use for Zookeeper clients.
-
start
Creates and starts ZooKeeper and Kafka server instances.- Specified by:
startin interfaceKafkaCluster- Throws:
Exception- on startup errors.
-
stop
Closes the internal servers. Failing to call this at the end of your tests will likely result in leaking instances. Provided alongside close() to stay consistent with start().- Throws:
Exception- on shutdown errors.
-
close
Closes the internal servers. Failing to call this at the end of your tests will likely result in leaking instances.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceKafkaCluster- Throws:
Exception- on shutdown errors.
-