Here are the examples of the java api class com.google.common.util.concurrent.SimpleTimeLimiter taken from open source projects.
1. ClientWorkFlow#callWithTimeout()
View licensepublic 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()
View licensepublic 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; }