Use Timeout instead of long / TimeUnit

This commit is contained in:
Oleg Kalnichevski 2018-10-22 11:28:08 +02:00
parent 160bac0cbb
commit 64470a970d
5 changed files with 14 additions and 17 deletions

View File

@ -28,7 +28,6 @@
package org.apache.hc.client5.http.impl;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@ -36,6 +35,7 @@ import org.apache.hc.core5.concurrent.DefaultThreadFactory;
import org.apache.hc.core5.pool.ConnPoolControl;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
/**
* This class maintains a background thread to enforce an eviction policy for expired / idle
@ -94,8 +94,8 @@ public final class IdleConnectionEvictor {
return thread.isAlive();
}
public void awaitTermination(final long time, final TimeUnit timeUnit) throws InterruptedException {
thread.join((timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS).toMillis(time));
public void awaitTermination(final Timeout timeout) throws InterruptedException {
thread.join(timeout != null ? timeout.toMillis() : Long.MAX_VALUE);
}
}

View File

@ -36,7 +36,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
@ -46,12 +45,12 @@ import org.apache.hc.client5.http.ServiceUnavailableRetryStrategy;
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeProvider;
import org.apache.hc.client5.http.auth.AuthSchemes;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.KerberosConfig;
import org.apache.hc.client5.http.classic.BackoffManager;
import org.apache.hc.client5.http.classic.ConnectionBackoffStrategy;
import org.apache.hc.client5.http.classic.ExecChainHandler;
import org.apache.hc.client5.http.auth.AuthSchemes;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.CookieSpecProvider;
@ -112,6 +111,7 @@ import org.apache.hc.core5.http.protocol.RequestUserAgent;
import org.apache.hc.core5.pool.ConnPoolControl;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.apache.hc.core5.util.VersionInfo;
/**
@ -993,7 +993,7 @@ public class HttpClientBuilder {
public void close() throws IOException {
connectionEvictor.shutdown();
try {
connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS);
connectionEvictor.awaitTermination(Timeout.ofSeconds(1));
} catch (final InterruptedException interrupted) {
Thread.currentThread().interrupt();
}

View File

@ -30,7 +30,6 @@ package org.apache.hc.client5.http.impl.io;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@ -340,13 +339,13 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
}
}
public synchronized void closeIdle(final long idletime, final TimeUnit timeUnit) {
Args.notNull(timeUnit, "Time unit");
public synchronized void closeIdle(final TimeValue idleTime) {
Args.notNull(idleTime, "Idle time");
if (this.closed.get()) {
return;
}
if (!this.leased) {
long time = timeUnit.toMillis(idletime);
long time = idleTime.toMillis();
if (time < 0) {
time = 0;
}

View File

@ -27,10 +27,9 @@
package org.apache.hc.client5.http.impl;
import java.util.concurrent.TimeUnit;
import org.apache.hc.core5.pool.ConnPoolControl;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
@ -56,7 +55,7 @@ public class TestIdleConnectionEvictor {
Assert.assertTrue(connectionEvictor.isRunning());
connectionEvictor.shutdown();
connectionEvictor.awaitTermination(1, TimeUnit.SECONDS);
connectionEvictor.awaitTermination(Timeout.ofSeconds(1));
Assert.assertFalse(connectionEvictor.isRunning());
}
@ -75,7 +74,7 @@ public class TestIdleConnectionEvictor {
Assert.assertTrue(connectionEvictor.isRunning());
connectionEvictor.shutdown();
connectionEvictor.awaitTermination(1, TimeUnit.SECONDS);
connectionEvictor.awaitTermination(Timeout.ofSeconds(1));
Assert.assertFalse(connectionEvictor.isRunning());
}

View File

@ -30,7 +30,6 @@ package org.apache.hc.client5.http.impl.io;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.HttpRoute;
@ -269,7 +268,7 @@ public class TestBasicHttpClientConnectionManager {
// Should have no effect
mgr.closeExpired();
mgr.closeIdle(0L, TimeUnit.MILLISECONDS);
mgr.closeIdle(TimeValue.ZERO_MILLISECONDS);
mgr.close();
Mockito.verify(conn, Mockito.times(1)).close(CloseMode.GRACEFUL);
@ -324,7 +323,7 @@ public class TestBasicHttpClientConnectionManager {
Thread.sleep(100);
mgr.closeIdle(50, TimeUnit.MILLISECONDS);
mgr.closeIdle(TimeValue.ofMillis(50));
Mockito.verify(conn).close(CloseMode.GRACEFUL);
}