Is it possible to start a zookeeper server instance in process, say for unit tests?

后端 未结 6 1117
难免孤独
难免孤独 2020-11-30 22:04

Calling org.apache.zookeeper.server.quorum.QuorumPeerMain.main() isn\'t working.

6条回答
  •  孤街浪徒
    2020-11-30 22:46

    An updated version of GeoffBourne's answer.

        int clientPort = 2199; // not standard
        int numConnections = 5000;
        int tickTime = 2000;
        String dataDirectory = System.getProperty("java.io.tmpdir");
    
        File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();
    
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
        ServerCnxnFactory factory = new NIOServerCnxnFactory();
        factory.configure(new InetSocketAddress(clientPort), numConnections);
    
        factory.startup(server); // start the server.
    
        // ...shutdown some time later
        factory.shutdown();
    

提交回复
热议问题