Class KafkaServer


  • @ThreadSafe
    public class KafkaServer
    extends Object
    A small embedded Kafka server.
    Author:
    Randall Hauch
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • brokerId

        private final int brokerId
      • logsDir

        private volatile File logsDir
      • 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 null
        brokerId - the unique broker ID
      • KafkaServer

        public KafkaServer​(Supplier<String> zookeeperConnection,
                           int brokerId,
                           int port)
        Create a new server instance.
        Parameters:
        zookeeperConnection - the supplier of the Zookeeper connection string; may not be null
        brokerId - the unique broker ID
        port - the desired port
    • 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, 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 null
        value - 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 -1 if 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 not running and 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 topic
        replicationFactor - the replication factor for each topic
        topics - 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 create
        numPartitions - the number of partitions for the topic
        replicationFactor - 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