org.apache.curator.framework.api.UnhandledErrorListener Java Examples

The following examples show how to use org.apache.curator.framework.api.UnhandledErrorListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModeledFrameworkImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
public static <T> ModeledFrameworkImpl<T> build(AsyncCuratorFramework client, ModelSpec<T> model, WatchMode watchMode, UnaryOperator<WatchedEvent> watcherFilter, UnhandledErrorListener unhandledErrorListener, UnaryOperator<CuratorEvent> resultFilter, Set<ModeledOptions> modeledOptions)
{
    boolean isWatched = (watchMode != null);

    Objects.requireNonNull(client, "client cannot be null");
    Objects.requireNonNull(model, "model cannot be null");
    modeledOptions = ImmutableSet.copyOf(Objects.requireNonNull(modeledOptions, "modeledOptions cannot be null"));

    watchMode = (watchMode != null) ? watchMode : WatchMode.stateChangeAndSuccess;

    AsyncCuratorFrameworkDsl dslClient = client.with(watchMode, unhandledErrorListener, resultFilter, watcherFilter);
    WatchableAsyncCuratorFramework watchableClient = isWatched ? dslClient.watched() : dslClient;

    return new ModeledFrameworkImpl<>(
        client,
        dslClient,
        watchableClient,
        model,
        watchMode,
        watcherFilter,
        unhandledErrorListener,
        resultFilter,
        isWatched,
        modeledOptions
    );
}
 
Example #2
Source File: Zookeeper.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * connect ZK, register watchers
 */
public CuratorFramework mkClient(Map conf, List<String> servers, Object port,
                                 String root, final WatcherCallBack watcher) {

    CuratorFramework fk = Utils.newCurator(conf, servers, port, root);

    fk.getCuratorListenable().addListener(new CuratorListener() {
        @Override
        public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
            if (e.getType().equals(CuratorEventType.WATCHED)) {
                WatchedEvent event = e.getWatchedEvent();

                watcher.execute(event.getState(), event.getType(), event.getPath());
            }

        }
    });

    fk.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {
        @Override
        public void unhandledError(String msg, Throwable error) {
            String errmsg = "Unrecoverable zookeeper error, halting process: " + msg;
            LOG.error(errmsg, error);
            JStormUtils.halt_process(1, "Unrecoverable zookeeper error");

        }
    });
    fk.start();
    return fk;
}
 
Example #3
Source File: Backgrounding.java    From xian with Apache License 2.0 5 votes vote down vote up
Backgrounding(Backgrounding rhs, UnhandledErrorListener errorListener)
{
    if ( rhs == null )
    {
        rhs = new Backgrounding();
    }
    this.inBackground = rhs.inBackground;
    this.context = rhs.context;
    this.callback = rhs.callback;
    this.errorListener = errorListener;
}
 
Example #4
Source File: ModeledFrameworkImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
private ModeledFrameworkImpl(AsyncCuratorFramework client, AsyncCuratorFrameworkDsl dslClient, WatchableAsyncCuratorFramework watchableClient, ModelSpec<T> modelSpec, WatchMode watchMode, UnaryOperator<WatchedEvent> watcherFilter, UnhandledErrorListener unhandledErrorListener, UnaryOperator<CuratorEvent> resultFilter, boolean isWatched, Set<ModeledOptions> modeledOptions)
{
    this.client = client;
    this.dslClient = dslClient;
    this.watchableClient = watchableClient;
    this.modelSpec = modelSpec;
    this.watchMode = watchMode;
    this.watcherFilter = watcherFilter;
    this.unhandledErrorListener = unhandledErrorListener;
    this.resultFilter = resultFilter;
    this.isWatched = isWatched;
    this.modeledOptions = modeledOptions;
}
 
Example #5
Source File: Backgrounding.java    From curator with Apache License 2.0 5 votes vote down vote up
public Backgrounding(BackgroundCallback callback, UnhandledErrorListener errorListener)
{
    this.callback = callback;
    this.errorListener = errorListener;
    inBackground = true;
    context = null;
}
 
Example #6
Source File: Backgrounding.java    From curator with Apache License 2.0 5 votes vote down vote up
Backgrounding(Backgrounding rhs, UnhandledErrorListener errorListener)
{
    if ( rhs == null )
    {
        rhs = new Backgrounding();
    }
    this.inBackground = rhs.inBackground;
    this.context = rhs.context;
    this.callback = rhs.callback;
    this.errorListener = errorListener;
}
 
Example #7
Source File: TestFrameworkBackground.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorListener() throws Exception
{
    //The first call to the ACL provider will return a reasonable
    //value. The second will throw an error. This is because the ACL
    //provider is accessed prior to the backgrounding call.
    final AtomicBoolean aclProviderCalled = new AtomicBoolean(false);
    
    ACLProvider badAclProvider = new ACLProvider()
    {
        @Override
        public List<ACL> getDefaultAcl()
        {
            if(aclProviderCalled.getAndSet(true))
            {
                throw new UnsupportedOperationException();
            }
            else
            {
                return new ArrayList<>();
            }
        }

        @Override
        public List<ACL> getAclForPath(String path)
        {
            if(aclProviderCalled.getAndSet(true))
            {
                throw new UnsupportedOperationException();
            }
            else
            {
                return new ArrayList<>();
            }
        }
    };
    CuratorFramework client = CuratorFrameworkFactory.builder()
        .connectString(server.getConnectString())
        .retryPolicy(new RetryOneTime(1))
        .aclProvider(badAclProvider)
        .build();
    try
    {
        client.start();

        final CountDownLatch errorLatch = new CountDownLatch(1);
        UnhandledErrorListener listener = new UnhandledErrorListener()
        {
            @Override
            public void unhandledError(String message, Throwable e)
            {
                if ( e instanceof UnsupportedOperationException )
                {
                    errorLatch.countDown();
                }
            }
        };
        client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
        Assert.assertTrue(new Timing().awaitLatch(errorLatch));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #8
Source File: GetACLBuilderImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public Pathable<List<ACL>> withUnhandledErrorListener(UnhandledErrorListener listener)
{
    backgrounding = new Backgrounding(backgrounding, listener);
    return this;
}
 
Example #9
Source File: TestFrameworkBackground.java    From curator with Apache License 2.0 4 votes vote down vote up
/**
 * CURATOR-126
 * Shutdown the Curator client while there are still background operations running.
 */
@Test
public void testShutdown() throws Exception
{
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory
        .builder()
        .connectString(server.getConnectString())
        .sessionTimeoutMs(timing.session())
        .connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1))
        .maxCloseWaitMs(timing.forWaiting().milliseconds())
        .build();
    try
    {
        final AtomicBoolean hadIllegalStateException = new AtomicBoolean(false);
        ((CuratorFrameworkImpl)client).debugUnhandledErrorListener = new UnhandledErrorListener()
        {
            @Override
            public void unhandledError(String message, Throwable e)
            {
                if ( e instanceof IllegalStateException )
                {
                    hadIllegalStateException.set(true);
                }
            }
        };
        client.start();

        final CountDownLatch operationReadyLatch = new CountDownLatch(1);
        ((CuratorFrameworkImpl)client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener()
        {
            @Override
            public void listen(OperationAndData<?> data)
            {
                try
                {
                    operationReadyLatch.await();
                }
                catch ( InterruptedException e )
                {
                    Thread.currentThread().interrupt();
                }
            }
        };

        // queue a background operation that will block due to the debugListener
        client.create().inBackground().forPath("/hey");
        timing.sleepABit();

        // close the client while the background is still blocked
        client.close();

        // unblock the background
        operationReadyLatch.countDown();
        timing.sleepABit();

        // should not generate an exception
        Assert.assertFalse(hadIllegalStateException.get());
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #10
Source File: Filters.java    From curator with Apache License 2.0 4 votes vote down vote up
public Filters(UnhandledErrorListener listener, UnaryOperator<CuratorEvent> resultFilter, UnaryOperator<WatchedEvent> watcherFilter)
{
    this.listener = listener;
    this.resultFilter = resultFilter;
    this.watcherFilter = watcherFilter;
}
 
Example #11
Source File: Filters.java    From curator with Apache License 2.0 4 votes vote down vote up
public UnhandledErrorListener getListener()
{
    return listener;
}
 
Example #12
Source File: AsyncCuratorFrameworkImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncCuratorFrameworkDsl with(WatchMode mode, UnhandledErrorListener listener, UnaryOperator<CuratorEvent> resultFilter, UnaryOperator<WatchedEvent> watcherFilter)
{
    return new AsyncCuratorFrameworkImpl(client, new Filters(listener, filters.getResultFilter(), filters.getWatcherFilter()), mode, watched);
}
 
Example #13
Source File: AsyncCuratorFrameworkImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncCuratorFrameworkDsl with(UnhandledErrorListener listener)
{
    return new AsyncCuratorFrameworkImpl(client, new Filters(listener, filters.getResultFilter(), filters.getWatcherFilter()), watchMode, watched);
}
 
Example #14
Source File: AsyncCuratorFrameworkImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncCuratorFrameworkDsl with(UnhandledErrorListener listener, UnaryOperator<CuratorEvent> resultFilter, UnaryOperator<WatchedEvent> watcherFilter)
{
    return new AsyncCuratorFrameworkImpl(client, new Filters(listener, resultFilter, watcherFilter), watchMode, watched);
}
 
Example #15
Source File: TestFrameworkBackground.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorListener() throws Exception
{
    //The first call to the ACL provider will return a reasonable
    //value. The second will throw an error. This is because the ACL
    //provider is accessed prior to the backgrounding call.
    final AtomicBoolean aclProviderCalled = new AtomicBoolean(false);
    
    ACLProvider badAclProvider = new ACLProvider()
    {
        @Override
        public List<ACL> getDefaultAcl()
        {
            if(aclProviderCalled.getAndSet(true))
            {
                throw new UnsupportedOperationException();
            }
            else
            {
                return new ArrayList<>();
            }
        }

        @Override
        public List<ACL> getAclForPath(String path)
        {
            if(aclProviderCalled.getAndSet(true))
            {
                throw new UnsupportedOperationException();
            }
            else
            {
                return new ArrayList<>();
            }
        }
    };
    CuratorFramework client = CuratorFrameworkFactory.builder()
        .connectString(server.getConnectString())
        .retryPolicy(new RetryOneTime(1))
        .aclProvider(badAclProvider)
        .build();
    try
    {
        client.start();
        AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);

        final CountDownLatch errorLatch = new CountDownLatch(1);
        UnhandledErrorListener listener = (message, e) -> {
            if ( e instanceof UnsupportedOperationException )
            {
                errorLatch.countDown();
            }
        };
        async.with(listener).create().forPath("/foo");
        Assert.assertTrue(new Timing().awaitLatch(errorLatch));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #16
Source File: TreeCache.java    From curator with Apache License 2.0 4 votes vote down vote up
/**
 * Allows catching unhandled errors in asynchronous operations.
 *
 * TODO: consider making public.
 */
@VisibleForTesting
public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
{
    return errorListeners;
}
 
Example #17
Source File: TestNodeCache.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void     testDeleteThenCreate() throws Exception
{
    NodeCache           cache = null;
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try
    {
        client.create().creatingParentsIfNeeded().forPath("/test/foo", "one".getBytes());

        final AtomicReference<Throwable>        error = new AtomicReference<Throwable>();
        client.getUnhandledErrorListenable().addListener
        (
            new UnhandledErrorListener()
            {
                @Override
                public void unhandledError(String message, Throwable e)
                {
                    error.set(e);
                }
            }
        );

        final Semaphore         semaphore = new Semaphore(0);
        cache = new NodeCache(client, "/test/foo");
        cache.getListenable().addListener
        (
            new NodeCacheListener()
            {
                @Override
                public void nodeChanged() throws Exception
                {
                    semaphore.release();
                }
            }
        );
        cache.start(true);

        Assert.assertEquals(cache.getCurrentData().getData(), "one".getBytes());

        client.delete().forPath("/test/foo");
        Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));
        client.create().forPath("/test/foo", "two".getBytes());
        Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));

        Throwable t = error.get();
        if ( t != null )
        {
            Assert.fail("Assert", t);
        }

        Assert.assertEquals(cache.getCurrentData().getData(), "two".getBytes());

        cache.close();
    }
    finally
    {
        CloseableUtils.closeQuietly(cache);
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example #18
Source File: CuratorMultiTransactionImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorMultiTransactionMain withUnhandledErrorListener(UnhandledErrorListener listener)
{
    backgrounding = new Backgrounding(backgrounding, listener);
    return this;
}
 
Example #19
Source File: TestNodeCache.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testDeleteThenCreate() throws Exception
{
    NodeCache           cache = null;
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try
    {
        client.create().creatingParentsIfNeeded().forPath("/test/foo", "one".getBytes());

        final AtomicReference<Throwable>        error = new AtomicReference<Throwable>();
        client.getUnhandledErrorListenable().addListener
        (
            new UnhandledErrorListener()
            {
                @Override
                public void unhandledError(String message, Throwable e)
                {
                    error.set(e);
                }
            }
        );

        final Semaphore         semaphore = new Semaphore(0);
        cache = new NodeCache(client, "/test/foo");
        cache.getListenable().addListener
        (
            new NodeCacheListener()
            {
                @Override
                public void nodeChanged() throws Exception
                {
                    semaphore.release();
                }
            }
        );
        cache.start(true);

        Assert.assertEquals(cache.getCurrentData().getData(), "one".getBytes());

        client.delete().forPath("/test/foo");
        Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));
        client.create().forPath("/test/foo", "two".getBytes());
        Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));

        Throwable t = error.get();
        if ( t != null )
        {
            Assert.fail("Assert", t);
        }

        Assert.assertEquals(cache.getCurrentData().getData(), "two".getBytes());

        cache.close();
    }
    finally
    {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #20
Source File: SyncBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public Pathable<Void> withUnhandledErrorListener(UnhandledErrorListener listener)
{
    backgrounding = new Backgrounding(backgrounding, listener);
    return this;
}
 
Example #21
Source File: NamespaceFacade.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
{
    return client.getUnhandledErrorListenable();
}
 
Example #22
Source File: GetChildrenBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public Pathable<List<String>> withUnhandledErrorListener(UnhandledErrorListener listener)
{
    backgrounding = new Backgrounding(backgrounding, listener);
    return this;
}
 
Example #23
Source File: GetACLBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public Pathable<List<ACL>> withUnhandledErrorListener(UnhandledErrorListener listener)
{
    backgrounding = new Backgrounding(backgrounding, listener);
    return this;
}
 
Example #24
Source File: TestFrameworkBackground.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorListener() throws Exception
{
    ACLProvider badAclProvider = new ACLProvider()
    {
        @Override
        public List<ACL> getDefaultAcl()
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<ACL> getAclForPath(String path)
        {
            throw new UnsupportedOperationException();
        }
    };
    CuratorFramework client = CuratorFrameworkFactory.builder()
        .connectString(server.getConnectString())
        .retryPolicy(new RetryOneTime(1))
        .aclProvider(badAclProvider)
        .build();
    try
    {
        client.start();

        final CountDownLatch errorLatch = new CountDownLatch(1);
        UnhandledErrorListener listener = new UnhandledErrorListener()
        {
            @Override
            public void unhandledError(String message, Throwable e)
            {
                if ( e instanceof UnsupportedOperationException )
                {
                    errorLatch.countDown();
                }
            }
        };
        client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
        Assert.assertTrue(new Timing().awaitLatch(errorLatch));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #25
Source File: TestFrameworkBackground.java    From xian with Apache License 2.0 4 votes vote down vote up
/**
 * CURATOR-126
 * Shutdown the Curator client while there are still background operations running.
 */
@Test
public void testShutdown() throws Exception
{
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory
        .builder()
        .connectString(server.getConnectString())
        .sessionTimeoutMs(timing.session())
        .connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1))
        .maxCloseWaitMs(timing.forWaiting().milliseconds())
        .build();
    try
    {
        final AtomicBoolean hadIllegalStateException = new AtomicBoolean(false);
        ((CuratorFrameworkImpl)client).debugUnhandledErrorListener = new UnhandledErrorListener()
        {
            @Override
            public void unhandledError(String message, Throwable e)
            {
                if ( e instanceof IllegalStateException )
                {
                    hadIllegalStateException.set(true);
                }
            }
        };
        client.start();

        final CountDownLatch operationReadyLatch = new CountDownLatch(1);
        ((CuratorFrameworkImpl)client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener()
        {
            @Override
            public void listen(OperationAndData<?> data)
            {
                try
                {
                    operationReadyLatch.await();
                }
                catch ( InterruptedException e )
                {
                    Thread.currentThread().interrupt();
                }
            }
        };

        // queue a background operation that will block due to the debugListener
        client.create().inBackground().forPath("/hey");
        timing.sleepABit();

        // close the client while the background is still blocked
        client.close();

        // unblock the background
        operationReadyLatch.countDown();
        timing.sleepABit();

        // should not generate an exception
        Assert.assertFalse(hadIllegalStateException.get());
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #26
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public Listenable<UnhandledErrorListener> getUnhandledErrorListenable() {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #27
Source File: TreeCache.java    From xian with Apache License 2.0 4 votes vote down vote up
/**
 * Allows catching unhandled errors in asynchornous operations.
 *
 * TODO: consider making public.
 */
@VisibleForTesting
public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
{
    return errorListeners;
}
 
Example #28
Source File: SyncBuilderImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public Pathable<Void> withUnhandledErrorListener(UnhandledErrorListener listener)
{
    backgrounding = new Backgrounding(backgrounding, listener);
    return this;
}
 
Example #29
Source File: NamespaceFacade.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
{
    return client.getUnhandledErrorListenable();
}
 
Example #30
Source File: WatcherRemovalFacade.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
{
    return client.getUnhandledErrorListenable();
}