Use Timeout instead of long / TimeUnit
This commit is contained in:
parent
160bac0cbb
commit
64470a970d
|
@ -28,7 +28,6 @@
|
||||||
package org.apache.hc.client5.http.impl;
|
package org.apache.hc.client5.http.impl;
|
||||||
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.apache.hc.core5.annotation.Contract;
|
import org.apache.hc.core5.annotation.Contract;
|
||||||
import org.apache.hc.core5.annotation.ThreadingBehavior;
|
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.pool.ConnPoolControl;
|
||||||
import org.apache.hc.core5.util.Args;
|
import org.apache.hc.core5.util.Args;
|
||||||
import org.apache.hc.core5.util.TimeValue;
|
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
|
* 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();
|
return thread.isAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void awaitTermination(final long time, final TimeUnit timeUnit) throws InterruptedException {
|
public void awaitTermination(final Timeout timeout) throws InterruptedException {
|
||||||
thread.join((timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS).toMillis(time));
|
thread.join(timeout != null ? timeout.toMillis() : Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@ import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.apache.hc.client5.http.AuthenticationStrategy;
|
import org.apache.hc.client5.http.AuthenticationStrategy;
|
||||||
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
|
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.SystemDefaultDnsResolver;
|
||||||
import org.apache.hc.client5.http.UserTokenHandler;
|
import org.apache.hc.client5.http.UserTokenHandler;
|
||||||
import org.apache.hc.client5.http.auth.AuthSchemeProvider;
|
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.CredentialsProvider;
|
||||||
import org.apache.hc.client5.http.auth.KerberosConfig;
|
import org.apache.hc.client5.http.auth.KerberosConfig;
|
||||||
import org.apache.hc.client5.http.classic.BackoffManager;
|
import org.apache.hc.client5.http.classic.BackoffManager;
|
||||||
import org.apache.hc.client5.http.classic.ConnectionBackoffStrategy;
|
import org.apache.hc.client5.http.classic.ConnectionBackoffStrategy;
|
||||||
import org.apache.hc.client5.http.classic.ExecChainHandler;
|
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.config.RequestConfig;
|
||||||
import org.apache.hc.client5.http.cookie.BasicCookieStore;
|
import org.apache.hc.client5.http.cookie.BasicCookieStore;
|
||||||
import org.apache.hc.client5.http.cookie.CookieSpecProvider;
|
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.pool.ConnPoolControl;
|
||||||
import org.apache.hc.core5.util.Args;
|
import org.apache.hc.core5.util.Args;
|
||||||
import org.apache.hc.core5.util.TimeValue;
|
import org.apache.hc.core5.util.TimeValue;
|
||||||
|
import org.apache.hc.core5.util.Timeout;
|
||||||
import org.apache.hc.core5.util.VersionInfo;
|
import org.apache.hc.core5.util.VersionInfo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -993,7 +993,7 @@ public class HttpClientBuilder {
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
connectionEvictor.shutdown();
|
connectionEvictor.shutdown();
|
||||||
try {
|
try {
|
||||||
connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS);
|
connectionEvictor.awaitTermination(Timeout.ofSeconds(1));
|
||||||
} catch (final InterruptedException interrupted) {
|
} catch (final InterruptedException interrupted) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,6 @@ package org.apache.hc.client5.http.impl.io;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
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) {
|
public synchronized void closeIdle(final TimeValue idleTime) {
|
||||||
Args.notNull(timeUnit, "Time unit");
|
Args.notNull(idleTime, "Idle time");
|
||||||
if (this.closed.get()) {
|
if (this.closed.get()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!this.leased) {
|
if (!this.leased) {
|
||||||
long time = timeUnit.toMillis(idletime);
|
long time = idleTime.toMillis();
|
||||||
if (time < 0) {
|
if (time < 0) {
|
||||||
time = 0;
|
time = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,10 +27,9 @@
|
||||||
|
|
||||||
package org.apache.hc.client5.http.impl;
|
package org.apache.hc.client5.http.impl;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.apache.hc.core5.pool.ConnPoolControl;
|
import org.apache.hc.core5.pool.ConnPoolControl;
|
||||||
import org.apache.hc.core5.util.TimeValue;
|
import org.apache.hc.core5.util.TimeValue;
|
||||||
|
import org.apache.hc.core5.util.Timeout;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.ArgumentMatchers;
|
import org.mockito.ArgumentMatchers;
|
||||||
|
@ -56,7 +55,7 @@ public class TestIdleConnectionEvictor {
|
||||||
Assert.assertTrue(connectionEvictor.isRunning());
|
Assert.assertTrue(connectionEvictor.isRunning());
|
||||||
|
|
||||||
connectionEvictor.shutdown();
|
connectionEvictor.shutdown();
|
||||||
connectionEvictor.awaitTermination(1, TimeUnit.SECONDS);
|
connectionEvictor.awaitTermination(Timeout.ofSeconds(1));
|
||||||
Assert.assertFalse(connectionEvictor.isRunning());
|
Assert.assertFalse(connectionEvictor.isRunning());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +74,7 @@ public class TestIdleConnectionEvictor {
|
||||||
Assert.assertTrue(connectionEvictor.isRunning());
|
Assert.assertTrue(connectionEvictor.isRunning());
|
||||||
|
|
||||||
connectionEvictor.shutdown();
|
connectionEvictor.shutdown();
|
||||||
connectionEvictor.awaitTermination(1, TimeUnit.SECONDS);
|
connectionEvictor.awaitTermination(Timeout.ofSeconds(1));
|
||||||
Assert.assertFalse(connectionEvictor.isRunning());
|
Assert.assertFalse(connectionEvictor.isRunning());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,6 @@ package org.apache.hc.client5.http.impl.io;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.apache.hc.client5.http.DnsResolver;
|
import org.apache.hc.client5.http.DnsResolver;
|
||||||
import org.apache.hc.client5.http.HttpRoute;
|
import org.apache.hc.client5.http.HttpRoute;
|
||||||
|
@ -269,7 +268,7 @@ public class TestBasicHttpClientConnectionManager {
|
||||||
|
|
||||||
// Should have no effect
|
// Should have no effect
|
||||||
mgr.closeExpired();
|
mgr.closeExpired();
|
||||||
mgr.closeIdle(0L, TimeUnit.MILLISECONDS);
|
mgr.closeIdle(TimeValue.ZERO_MILLISECONDS);
|
||||||
mgr.close();
|
mgr.close();
|
||||||
|
|
||||||
Mockito.verify(conn, Mockito.times(1)).close(CloseMode.GRACEFUL);
|
Mockito.verify(conn, Mockito.times(1)).close(CloseMode.GRACEFUL);
|
||||||
|
@ -324,7 +323,7 @@ public class TestBasicHttpClientConnectionManager {
|
||||||
|
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
mgr.closeIdle(50, TimeUnit.MILLISECONDS);
|
mgr.closeIdle(TimeValue.ofMillis(50));
|
||||||
|
|
||||||
Mockito.verify(conn).close(CloseMode.GRACEFUL);
|
Mockito.verify(conn).close(CloseMode.GRACEFUL);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue