public DefaultZKClientService(String zkStr, int sessionTimeout, Watcher connectionWatcher, Multimap<String, byte[]> authInfos) { this.zkStr = zkStr; this.sessionTimeout = sessionTimeout; this.connectionWatchers = new CopyOnWriteArrayList<>(); this.authInfos = copyAuthInfo(authInfos); addConnectionWatcher(connectionWatcher); this.zooKeeper = new AtomicReference<>(); serviceDelegate = new ServiceDelegate(); }
@Override protected void doStart() { // A single thread executor for all events ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), Threads.createDaemonThreadFactory("zk-client-EventThread")); // Just discard the execution if the executor is closed executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); eventExecutor = executor; try { zooKeeper.set(createZooKeeper()); } catch (IOException e) { notifyFailed(e); } }
@Override public void process(WatchedEvent event) { State state = state(); if (state == State.TERMINATED || state == State.FAILED) { return; if (event.getState() == Event.KeeperState.SyncConnected && state == State.STARTING) { LOG.debug("Connected to ZooKeeper: {}", zkStr); notifyStarted(); return;
@Override public void process(WatchedEvent event) { State state = state(); if (state == State.TERMINATED || state == State.FAILED) { return; if (event.getState() == Event.KeeperState.SyncConnected && state == State.STARTING) { LOG.debug("Connected to ZooKeeper: {}", zkStr); notifyStarted(); return;
private ServiceDelegate() { this.stopTask = createStopTask(); addListener(new Listener() { @Override public void starting() {
private ServiceDelegate() { this.stopTask = createStopTask(); addListener(new Listener() { @Override public void starting() {
@Override protected void doStart() { // A single thread executor for all events ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), Threads.createDaemonThreadFactory("zk-client-EventThread")); // Just discard the execution if the executor is closed executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); eventExecutor = executor; try { zooKeeper.set(createZooKeeper()); } catch (IOException e) { notifyFailed(e); } }
@Override public void run() { // Only reconnect if the current state is running if (state() != State.RUNNING) { return; } try { LOG.info("Reconnect to ZooKeeper due to expiration: {}", zkStr); closeZooKeeper(zooKeeper.getAndSet(createZooKeeper())); } catch (IOException e) { notifyFailed(e); } } });
@Override public void run() { // Only reconnect if the current state is running if (state() != State.RUNNING) { return; } try { LOG.info("Reconnect to ZooKeeper due to expiration: {}", zkStr); closeZooKeeper(zooKeeper.getAndSet(createZooKeeper())); } catch (IOException e) { notifyFailed(e); } } });
public DefaultZKClientService(String zkStr, int sessionTimeout, Watcher connectionWatcher, Multimap<String, byte[]> authInfos) { this.zkStr = zkStr; this.sessionTimeout = sessionTimeout; this.connectionWatchers = new CopyOnWriteArrayList<>(); this.authInfos = copyAuthInfo(authInfos); addConnectionWatcher(connectionWatcher); this.zooKeeper = new AtomicReference<>(); serviceDelegate = new ServiceDelegate(); }