Example usage for com.google.common.util.concurrent UncheckedTimeoutException getCause

List of usage examples for com.google.common.util.concurrent UncheckedTimeoutException getCause

Introduction

In this page you can find the example usage for com.google.common.util.concurrent UncheckedTimeoutException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:me.defying.chili.timeout.TimeoutInterceptor.java

@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
    // get annotation, class, method and arguments
    Timeout annotation = InvocationUtils.getAnnotation(invocation, Timeout.class);

    // do nothing
    if (annotation.time() <= 0) {
        return invocation.proceed();
    }//ww  w .jav  a 2 s  . c  o m

    TimeLimiter limiter = new SimpleTimeLimiter();

    // underlying method invoker
    Callable<Object> invoker = new TimeoutInvoker(invocation);

    try {
        return limiter.callWithTimeout(invoker, annotation.time(), annotation.unit(), true);
    } catch (UncheckedTimeoutException ex) {
        throw new TimeoutException(ex);
    } catch (ChiliException ex) {
        // when the underlying method invokation throws an exception we need
        // to unrap it in order to existing code get the real exception
        throw ex.getCause();
    }
}

From source file:org.graylog2.security.ldap.LdapConnector.java

public LdapNetworkConnection connect(LdapConnectionConfig config) throws LdapException {
    final LdapNetworkConnection connection = new LdapNetworkConnection(config);
    connection.setTimeOut(connectionTimeout);

    if (LOG.isTraceEnabled()) {
        LOG.trace("Connecting to LDAP server {}:{}, binding with user {}", config.getLdapHost(),
                config.getLdapPort(), config.getName());
    }/*  w w w .jav  a2  s .  c om*/

    // this will perform an anonymous bind if there were no system credentials
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("ldap-connector-%d").build();
    final SimpleTimeLimiter timeLimiter = new SimpleTimeLimiter(
            Executors.newSingleThreadExecutor(threadFactory));
    @SuppressWarnings("unchecked")
    final Callable<Boolean> timeLimitedConnection = timeLimiter.newProxy(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            return connection.connect();
        }
    }, Callable.class, connectionTimeout, TimeUnit.MILLISECONDS);
    try {
        final Boolean connected = timeLimitedConnection.call();
        if (!connected) {
            return null;
        }
    } catch (UncheckedTimeoutException e) {
        LOG.error("Timed out connecting to LDAP server", e);
        throw new LdapException("Could not connect to LDAP server", e.getCause());
    } catch (LdapException e) {
        throw e;
    } catch (Exception e) {
        // unhandled different exception, should really not happen here.
        throw new LdapException("Unexpected error connecting to LDAP", e);
    }
    connection.bind();

    return connection;
}