List of usage examples for com.google.common.util.concurrent SimpleTimeLimiter SimpleTimeLimiter
public SimpleTimeLimiter(ExecutorService executor)
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()); }//from ww w . j a v 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; }
From source file:com.eucalyptus.cloud.ws.TCPHandler.java
public void run() { try (final Socket socket = this.socket) { socket.setSoTimeout(timeout_seconds * 1000); final TimeLimiter limiter = new SimpleTimeLimiter(Threads.lookup(Dns.class, TCPHandler.class)); final byte[] inBytes = limiter.callWithTimeout(new Callable<byte[]>() { @Override/*from w w w .ja v a2 s . c o m*/ public byte[] call() throws Exception { final DataInputStream inStream = new DataInputStream(socket.getInputStream()); final int inputLength = inStream.readUnsignedShort(); byte[] inBytes = new byte[inputLength]; inStream.readFully(inBytes); return inBytes; } }, timeout_seconds, TimeUnit.SECONDS, false); byte[] response = null; try { final Message query = new Message(inBytes); ConnectionHandler.setLocalAndRemoteInetAddresses(socket.getLocalAddress(), socket.getInetAddress()); try { response = generateReply(query, inBytes, inBytes.length, socket); } catch (RuntimeException ex) { response = errorMessage(query, Rcode.SERVFAIL); throw ex; } finally { ConnectionHandler.clearInetAddresses(); } if (response == null) return; } catch (IOException exception) { LOG.error(exception); } final DataOutputStream outStream = new DataOutputStream(socket.getOutputStream()); outStream.writeShort(response.length); outStream.write(response); outStream.flush(); } catch (UncheckedTimeoutException e) { LOG.debug("Timeout reading request."); } catch (Exception ex) { LOG.error(ex); } }
From source file:com.netflix.servo.publish.MonitorRegistryMetricPoller.java
/** * Creates a new instance using the specified registry. * * @param registry registry to query for annotated objects * @param cacheTTL how long to cache the filtered monitor list from the registry * @param unit time unit for the cache ttl * @param useLimiter whether to use a time limiter for getting the values from the monitors * @param clock clock instance to use to get the time *//*from w w w . ja va 2 s . co m*/ public MonitorRegistryMetricPoller(MonitorRegistry registry, long cacheTTL, TimeUnit unit, boolean useLimiter, Clock clock) { this.registry = registry; this.cacheTTL = TimeUnit.MILLISECONDS.convert(cacheTTL, unit); this.clock = clock; if (useLimiter) { final ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true) .setNameFormat("ServoMonitorGetValueLimiter-%d").build(); service = Executors.newSingleThreadExecutor(factory); limiter = new SimpleTimeLimiter(service); } else { service = null; limiter = null; } }