com.google.common.util.concurrent.SimpleTimeLimiter

Here are the examples of the java api class com.google.common.util.concurrent.SimpleTimeLimiter taken from open source projects.

1. ClientWorkFlow#callWithTimeout()

Project: zanata-server
Source File: ClientWorkFlow.java
View license
public List<String> callWithTimeout(final File workingDirectory, String command) {
    log.info("=== about to call ===\n{}", command);
    final List<String> commands = Lists.newArrayList(Splitter.on(" ").split(command));
    SimpleTimeLimiter timeLimiter = new SimpleTimeLimiter();
    Callable<List<String>> work = () -> {
        Process process = ClientWorkFlow.invokeClient(workingDirectory, commands);
        process.waitFor();
        List<String> output = ClientWorkFlow.getOutput(process);
        logOutputLines(output);
        return output;
    };
    try {
        return timeLimiter.callWithTimeout(work, timeoutDuration, TimeUnit.SECONDS, true);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

2. LdapConnector#connect()

Project: graylog2-server
Source File: LdapConnector.java
View license
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());
    }
    // 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) {
        throw new LdapException("Unexpected error connecting to LDAP", e);
    }
    connection.bind();
    return connection;
}