Class ZookeeperServer


  • @ThreadSafe
    public class ZookeeperServer
    extends Object
    A lightweight embeddable Zookeeper server useful for unit testing.
    Author:
    Randall Hauch
    See Also:
    KafkaCluster
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • DEFAULT_TICK_TIME

        public static int DEFAULT_TICK_TIME
      • tickTime

        private volatile int tickTime
        The basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
      • port

        private volatile int port
      • factory

        private volatile org.apache.zookeeper.server.ServerCnxnFactory factory
      • dataDir

        private volatile File dataDir
      • snapshotDir

        private volatile File snapshotDir
      • logDir

        private volatile File logDir
      • server

        private volatile org.apache.zookeeper.server.ZooKeeperServer server
    • Constructor Detail

      • ZookeeperServer

        public ZookeeperServer()
        Create a new server instance.
    • Method Detail

      • shutdown

        public void shutdown()
        Shutdown the embedded Zookeeper server and delete all data.
      • shutdown

        public void shutdown​(boolean deleteData)
        Shutdown the embedded Kafka server.
        Parameters:
        deleteData - true if the data should be removed, or false otherwise
      • 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
      • setPort

        public ZookeeperServer 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
      • setTickTime

        public ZookeeperServer setTickTime​(int tickTime)
        Set the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
        Parameters:
        tickTime - the desired value, or non-positive if the default of DEFAULT_TICK_TIME be used
        Returns:
        this instance to allow chaining methods; never null
      • getPort

        public int getPort()
        Get the current port.
        Returns:
        the port number, or -1 if the port should be discovered upon startup()
      • getTickTime

        public int getTickTime()
        Get the basic time unit in milliseconds used by ZooKeeper.
        Returns:
        tick time; always positive
      • 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
      • getSnapshotDirectory

        File getSnapshotDirectory()
        Get the parent directory where the server's snapshots are kept.
        Returns:
        the parent directory for the server's snapshots; never null once the server is running
      • getLogDirectory

        File getLogDirectory()
        Get the parent directory where the server's logs are kept.
        Returns:
        the parent directory for the server's logs; never null once the server is running
      • getStateDirectory

        public File getStateDirectory()
        Get the parent directory where the server's logs and snapshots will be kept.
        Returns:
        the parent directory for the server's logs and snapshots; may be null if a temporary directory will be used
      • setStateDirectory

        public ZookeeperServer setStateDirectory​(File dataDir)
        Set the parent directory where the server's logs and snapshots will be kept.
        Parameters:
        dataDir - the parent directory for the server's logs and snapshots; may be null if a temporary directory will be used
        Returns:
        this instance to allow method chaining; never null
        Throws:
        IllegalArgumentException - if the supplied file is not a directory or not writable