How to call Java API to create a basic Zookerper
How to call Java API to create a basic Zookerper, for this problem, this article details the corresponding analysis and solution, hoping to help more friends who want to solve this problem find a simpler and easier way.
Here's how to invoke the Java API to create a basic Zookerper session instance
import java.util.concurrent.CountDownLatch;import org.apache.zookeeper.WatchedEvent;import org. apache.zookeeper.Watcher;import org. apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.Zookeeper;public class zookeeper_simple_usage implements Watcher { private static CountDownLatch connectedSemaphore = new CountDownLatch(1); public static void main (String[] args) throws Exception{ ZooKeeper zookeeper = new ZooKeeper("202.201.13.55:2100", 5000, new zookeeper_simple_usage()); System.out.println(zookeeper.getState()); try{ connectedSemaphore.await(); } catch (InterruptedException e){} System.out.println("ZooKeeper session established. "); } public void process(WatchedEvent event) { System.out.println("Received watched evnet: " + event); if (KeeperState.SyncConnected == event.getState()) { connectedSemaphore.countDown(); } }}
The process method in WatchedEvent must be overloaded.
execution result
Received watched evnet: WatchedEvent state:SyncConnected type:None path:nullZooKeeper session established.
Four packages need to be added to Build Path
zookerper-3.4.6.jar
log4j-1.2.14.jar
slf4j-api-1.7.12.jar
slf4j-simple-1.7.12.jar
Otherwise, you will report a class not found.
About how to call Java API to create a basic Zookerper question answer to share here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.