@Override public void awaitTermination(final Timeout timeout) throws InterruptedException { Args.notNull(timeout, "Timeout"); executorService.awaitTermination(timeout.getDuration(), timeout.getTimeUnit()); }
public Builder() { super(); this.requests = 1; this.concurrencyLevel = 1; this.keepAlive = false; this.verbosity = 0; this.headInsteadOfGet = false; this.socketTimeout = Timeout.ofSeconds(60); }
/** * Creates a Timeout. * * @param days the duration in days and the given {@code timeUnit}. * @return a Timeout */ public static Timeout ofDays(final long days) { return of(days, TimeUnit.DAYS); }
@Override public void setSocketTimeout(final Timeout timeout) { final SocketHolder socketHolder = this.socketHolderRef.get(); if (socketHolder != null) { try { socketHolder.getSocket().setSoTimeout(Timeout.defaultsToDisabled(timeout).toMillisIntBound()); } catch (final SocketException ignore) { // It is not quite clear from the Sun's documentation if there are any // other legitimate cases for a socket exception to be thrown when setting // SO_TIMEOUT besides the socket being already closed } } }
@Override public Timeout getSocketTimeout() { final SocketHolder socketHolder = this.socketHolderRef.get(); if (socketHolder != null) { try { return Timeout.ofMilliseconds(socketHolder.getSocket().getSoTimeout()); } catch (final SocketException ignore) { } } return Timeout.DISABLED; }
final Future<PoolEntry<HttpHost, HttpClientConnection>> leaseFuture = connPool.lease(targetHost, null, connectTimeout, null); final PoolEntry<HttpHost, HttpClientConnection> poolEntry; final Timeout timeout = Timeout.defaultsToDisabled(connectTimeout); try { poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit()); } catch (final InterruptedException ex) { throw new InterruptedIOException(ex.getMessage());
private Socket createSocket(final HttpHost targetHost) throws IOException { final Socket sock = new Socket(); sock.setSoTimeout(socketConfig.getSoTimeout().toMillisIntBound()); sock.setReuseAddress(socketConfig.isSoReuseAddress()); sock.setTcpNoDelay(socketConfig.isTcpNoDelay()); sock.setKeepAlive(socketConfig.isSoKeepAlive()); if (socketConfig.getRcvBufSize() > 0) { sock.setReceiveBufferSize(socketConfig.getRcvBufSize()); } if (socketConfig.getSndBufSize() > 0) { sock.setSendBufferSize(socketConfig.getSndBufSize()); } final int linger = socketConfig.getSoLinger().toMillisIntBound(); if (linger >= 0) { sock.setSoLinger(true, linger); } final InetSocketAddress targetAddress = addressResolver.resolve(targetHost); sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisIntBound()); if (URIScheme.HTTPS.same(targetHost.getSchemeName())) { return sslSocketFactory.createSocket(sock, targetHost.getHostName(), targetAddress.getPort(), true); } return sock; }
@Override public void close() { this.session.lock().lock(); try { if (this.status >= CLOSING) { return; } this.status = CLOSING; if (this.session.getSocketTimeout().isDisabled()) { this.session.setSocketTimeout(Timeout.ofMilliseconds(1000)); } try { updateEventMask(); } catch (final CancelledKeyException ex) { close(CloseMode.GRACEFUL); } } finally { this.session.lock().unlock(); } }
private void testFactory(final TimeUnit timeUnit) { Assert.assertEquals(timeUnit, Timeout.of(1, timeUnit).getTimeUnit()); }
final boolean checkTimeout(final long currentTimeMillis) { final Timeout timeout = getTimeout(); if (!timeout.isDisabled()) { final long timeoutMillis = timeout.toMillis(); final long deadlineMillis = getLastReadTime() + timeoutMillis; if (currentTimeMillis > deadlineMillis) { try { onTimeout(timeout); } catch (final CancelledKeyException ex) { close(CloseMode.GRACEFUL); } catch (final Exception ex) { onException(ex); close(CloseMode.IMMEDIATE); } return false; } } return true; }
@Test public void testFromString() throws ParseException { Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807 SECONDS")); Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807 Seconds")); Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807 Seconds")); Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807\tSeconds")); Assert.assertEquals(Timeout.ZERO_MILLISECONDS, Timeout.parse("0 MILLISECONDS")); }
/** * Whether this timeout is disabled. * * @return Whether this timeout is disabled. */ public boolean isDisabled() { return getDuration() == 0; }
public void awaitTermination(final Timeout timeout) throws InterruptedException { thread.join(timeout != null ? timeout.toMillis() : Long.MAX_VALUE); }
@Test public void testDisabled() { Assert.assertTrue(Timeout.DISABLED.isDisabled()); Assert.assertFalse(Timeout.DISABLED.isEnabled()); }
/** * Whether this timeout is enabled. * * @return Whether this timeout is disabled. */ public boolean isEnabled() { return !isDisabled(); }