Alex Rivera | Logout

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

Asked 2012-02-14T23:59:45.653
44

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

Edit
Report

1 Answer

12

You can use something like this.

int clientPort = 21818; // none-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);
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections);

standaloneServerFactory.startup(server); // start the server.

And to shut it down just call standaloneServerFactory.shutdown()

answered 2012-02-15T00:36:16.197

Your Answer