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

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

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

6条回答
  •  离开以前
    2020-11-30 22:47

    Building on 1's answer by adding the use of an ephemeral port (shown by zkPort) and updated for latest ZK API:

    int tickTime = 2000;
    int numConnections = 5000;
    String dataDirectory = System.getProperty("java.io.tmpdir");
    
    File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();
    
    ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
    standaloneServerFactory = ServerCnxnFactory.createFactory(0, numConnections);
    int zkPort = standaloneServerFactory.getLocalPort();
    
    standaloneServerFactory.startup(server);
    

提交回复
热议问题