Example usage for org.apache.zookeeper WatchedEvent WatchedEvent

List of usage examples for org.apache.zookeeper WatchedEvent WatchedEvent

Introduction

In this page you can find the example usage for org.apache.zookeeper WatchedEvent WatchedEvent.

Prototype

public WatchedEvent(EventType eventType, KeeperState keeperState, String path) 

Source Link

Document

Create a WatchedEvent with specified type, state and path

Usage

From source file:org.apache.camel.component.zookeeper.operations.FutureEventDrivenOperationTest.java

License:Apache License

@Test
public void shouldWaitForEvents() throws Exception {
    final FutureEventDrivenOperation<String> future = new FutureEventDrivenOperation<String>(null, "somepath",
            EventType.NodeCreated) {/*from ww w.  j  av  a 2s.  c  om*/

        @Override
        protected void installWatch() {
        }

        @Override
        public OperationResult<String> getResult() {
            return new OperationResult<String>(data, statistics);
        }
    };

    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, null, "somepath");
    fireEventIn(future, event, 100);
    assertEquals(data, future.get().getResult());
    assertEquals(statistics, future.get().getStatistics());
    assertEquals(event, future.getWatchedEvent());
}

From source file:org.apache.curator.framework.imps.CuratorFrameworkImpl.java

License:Apache License

private void performBackgroundOperation(OperationAndData<?> operationAndData) {
    try {//from  w w w .j  a  v  a2  s. c om
        if (client.isConnected()) {
            operationAndData.callPerformBackgroundOperation();
        } else {
            client.getZooKeeper(); // important - allow connection resets, timeouts, etc. to occur
            if (operationAndData.getElapsedTimeMs() >= client.getConnectionTimeoutMs()) {
                throw new CuratorConnectionLossException();
            }
            operationAndData.sleepFor(1, TimeUnit.SECONDS);
            queueOperation(operationAndData);
        }
    } catch (Throwable e) {
        ThreadUtils.checkInterrupted(e);

        /**
         * Fix edge case reported as CURATOR-52. ConnectionState.checkTimeouts() throws KeeperException.ConnectionLossException
         * when the initial (or previously failed) connection cannot be re-established. This needs to be run through the retry policy
         * and callbacks need to get invoked, etc.
         */
        if (e instanceof CuratorConnectionLossException) {
            WatchedEvent watchedEvent = new WatchedEvent(Watcher.Event.EventType.None,
                    Watcher.Event.KeeperState.Disconnected, null);
            CuratorEvent event = new CuratorEventImpl(this, CuratorEventType.WATCHED,
                    KeeperException.Code.CONNECTIONLOSS.intValue(), null, null, operationAndData.getContext(),
                    null, null, null, watchedEvent, null);
            if (checkBackgroundRetry(operationAndData, event)) {
                queueOperation(operationAndData);
            } else {
                logError("Background retry gave up", e);
            }
        } else {
            handleBackgroundOperationException(operationAndData, e);
        }
    }
}

From source file:org.apache.curator.utils.InjectSessionExpiration.java

License:Apache License

public static void injectSessionExpiration(ZooKeeper zooKeeper) {
    try {//  w  w  w  .  j  a  v a 2  s . com
        WatchedEvent event = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Expired,
                null);

        ClientCnxn clientCnxn = (ClientCnxn) cnxnField.get(zooKeeper);
        Object eventThread = eventThreadField.get(clientCnxn);
        queueEventMethod.invoke(eventThread, event);
        queueEventOfDeathMethod.invoke(eventThread);
        stateField.set(clientCnxn, ZooKeeper.States.CLOSED);
        Object sendThread = sendThreadField.get(clientCnxn);
        Object clientCnxnSocket = getClientCnxnSocketMethod.invoke(sendThread);
        wakeupCnxnMethod.invoke(clientCnxnSocket);
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException("Could not inject session expiration using reflection", e);
    }
}

From source file:org.apache.cxf.dosgi.discovery.zookeeper.InterfaceMonitorTest.java

License:Apache License

public void testInterfaceMonitor() throws KeeperException, InterruptedException {

    IMocksControl c = EasyMock.createNiceControl();

    BundleContext ctx = c.createMock(BundleContext.class);
    ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);

    ZooKeeper zk = c.createMock(ZooKeeper.class);
    EasyMock.expect(zk.getState()).andReturn(ZooKeeper.States.CONNECTED).anyTimes();

    EasyMock.expect(zkd.getZookeeper()).andReturn(zk).anyTimes();

    EndpointListenerTrackerCustomizer.Interest interest = new EndpointListenerTrackerCustomizer.Interest();

    String scope = "(myProp=test)";
    String interf = "es.schaaf.test";
    String node = Util.getZooKeeperPath(interf);

    final InterfaceDataMonitorListenerImpl idmli = c.createMock(InterfaceDataMonitorListenerImpl.class);

    InterfaceMonitor im = new InterfaceMonitor(zk, interf, interest, scope, ctx) {
        @Override/*from w  w  w.j a v a 2s  . c  om*/
        protected InterfaceDataMonitorListenerImpl createInterfaceDataMonitorListener(ZooKeeper zk, String intf,
                Interest zkd, String scope, BundleContext bctx) {
            return idmli;
        }
    };

    idmli.change();
    EasyMock.expectLastCall().once();

    zk.exists(EasyMock.eq(node), EasyMock.eq(im), EasyMock.eq(im), EasyMock.anyObject());
    EasyMock.expectLastCall().once();

    EasyMock.expect(zk.exists(EasyMock.eq(node), EasyMock.eq(false))).andReturn(new Stat()).anyTimes();

    EasyMock.expect(zk.getChildren(EasyMock.eq(node), EasyMock.eq(im))).andReturn(Collections.EMPTY_LIST)
            .once();

    c.replay();

    im.start();

    // simulate a zk callback
    WatchedEvent we = new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, node);
    im.process(we);

    c.verify();
}

From source file:org.apache.distributedlog.zk.TestZKWatcherManager.java

License:Apache License

@Test(timeout = 60000)
public void testRegisterUnregisterWatcher() throws Exception {
    ZKWatcherManager watcherManager = ZKWatcherManager.newBuilder().name("test-register-unregister-watcher")
            .zkc(null).statsLogger(NullStatsLogger.INSTANCE).build();
    String path = "/test-register-unregister-watcher";
    final List<WatchedEvent> events = new LinkedList<WatchedEvent>();
    final CountDownLatch latch = new CountDownLatch(2);
    Watcher watcher = new Watcher() {
        @Override/* w w  w .java2 s. c om*/
        public void process(WatchedEvent event) {
            events.add(event);
            latch.countDown();
        }
    };
    watcherManager.registerChildWatcher(path, watcher);

    // fire the event
    WatchedEvent event0 = new WatchedEvent(Watcher.Event.EventType.NodeCreated,
            Watcher.Event.KeeperState.SyncConnected, path);
    WatchedEvent event1 = new WatchedEvent(Watcher.Event.EventType.None,
            Watcher.Event.KeeperState.SyncConnected, path);
    WatchedEvent event2 = new WatchedEvent(Watcher.Event.EventType.NodeChildrenChanged,
            Watcher.Event.KeeperState.SyncConnected, path);
    watcher.process(event1);
    watcher.process(event2);

    latch.await();

    assertEquals(2, events.size());
    assertEquals(event1, events.get(0));
    assertEquals(event2, events.get(1));

    // unregister watcher
    watcherManager.unregisterChildWatcher(path, watcher, true);
    // unregister gauges
    watcherManager.unregisterGauges();
    assertEquals(0, watcherManager.childWatches.size());
}

From source file:org.apache.helix.ZkTestHelper.java

License:Apache License

/**
 * Simulate a zk state change by calling {@link ZkClient#process(WatchedEvent)} directly
 *//* ww w .  j  ava 2  s. c  om*/
public static void simulateZkStateDisconnected(ZkClient client) {
    WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Disconnected, null);
    client.process(event);
}

From source file:org.apache.niolex.zookeeper.core.ZKConnectorExceTest.java

License:Apache License

@Test
@Order(0)//from w w  w  .  j  a v  a2  s.  c om
public void testProcessEvent() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, KeeperState.Expired, "/a/ab/cc");
    ZKC.addAuthInfo("abc", "lex");
    ZKC.process(event);
    event = new WatchedEvent(EventType.NodeCreated, KeeperState.Disconnected, "/a/ab/cc");
    ZKC.process(event);
    event = new WatchedEvent(EventType.NodeCreated, KeeperState.AuthFailed, "/a/ab/cc");
    ZKC.process(event);
}

From source file:org.apache.niolex.zookeeper.watcher.CommonRecoverableWatcherTest.java

License:Apache License

@Test
public void testCommonRecoverableWatcher() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, KeeperState.AuthFailed, "/a/ab/cc");
    wcl.process(event);/*  w  w  w .  j ava 2 s .  c om*/
    verify(listn, never()).onDataChange(any(byte[].class));
    verify(listn, never()).onChildrenChange(any(List.class));
    verify(zk, never()).getData(any(String.class), any(Watcher.class), any(Stat.class));
    verify(zk, never()).getChildren(any(String.class), any(Watcher.class));
}

From source file:org.apache.niolex.zookeeper.watcher.CommonRecoverableWatcherTest.java

License:Apache License

@Test
public void testCommonRecoverableWatchera() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, KeeperState.AuthFailed, "/a/ab/cc");
    wda.process(event);//from  ww  w  .j a va2s  .  c om
    verify(listn, never()).onDataChange(any(byte[].class));
    verify(listn, never()).onChildrenChange(any(List.class));
    verify(zk, never()).getData(any(String.class), any(Watcher.class), any(Stat.class));
    verify(zk, never()).getChildren(any(String.class), any(Watcher.class));
}

From source file:org.apache.niolex.zookeeper.watcher.CommonRecoverableWatcherTest.java

License:Apache License

@Test
public void testProcess() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.AuthFailed, "/a/ab/cc");
    wcl.process(event);/*from  www . j a  va  2  s  .c om*/
    verify(listn, never()).onDataChange(any(byte[].class));
    verify(listn, times(1)).onChildrenChange(any(List.class));
    verify(zk, never()).getData(any(String.class), any(Watcher.class), any(Stat.class));
    verify(zk, times(1)).getChildren(any(String.class), any(Watcher.class));
}