下面列出了怎么用org.apache.http.conn.ConnectionPoolTimeoutException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Test that a large number of open/close does not lose connections;
* check for null response.
* This test uses an implicit HTTPSession.
*/
@Test
public void testClosing2() throws HTTPException {
// Set max # of connections
HTTPSession.setGlobalMaxConnections(201);
for (int i = 0; i < 200; i++) {
HTTPMethod m = HTTPFactory.Get(CLOSEFILE);
HttpResponse res = null;
try {
res = m.executeRaw();
} catch (HTTPException e) {
if (e.getCause() instanceof ConnectionPoolTimeoutException) {
System.err.println("TestMisc: timeout: " + i);
} else
throw e;
}
Assert.assertFalse("Null response", res == null);
m.close();
}
}
@Test(timeout = 60 * 1000)
public void leasing_a_new_connection_fails_with_connection_pool_timeout()
throws Exception {
String localhostEndpoint = "http://localhost:" + server.getPort();
AmazonHttpClient httpClient = new AmazonHttpClient(
new ClientConfiguration()
.withMaxConnections(1)
.withConnectionTimeout(100)
.withMaxErrorRetry(0));
Request<?> request = new EmptyHttpRequest(localhostEndpoint, HttpMethodName.GET);
// Block the first connection in the pool with this request.
httpClient.requestExecutionBuilder().request(request).execute(new EmptyAWSResponseHandler());
try {
// A new connection will be leased here which would fail in
// ConnectionPoolTimeoutException.
httpClient.requestExecutionBuilder().request(request).execute();
Assert.fail("Connection pool timeout exception is expected!");
} catch (AmazonClientException e) {
Assert.assertTrue(e.getCause() instanceof ConnectionPoolTimeoutException);
}
}
/**
* Sends the http request asynchronously to the API If the request is time out it creates a new
* response object with timeout set to true Otherwise it throws a run time exception
*
* @param request the request to send
* @throws AlgoliaRuntimeException When an error occurred while sending the request
*/
public CompletableFuture<HttpResponse> performRequestAsync(HttpRequest request) {
HttpRequestBase requestToSend = buildRequest(request);
return toCompletableFuture(fc -> asyncHttpClient.execute(requestToSend, fc))
.thenApplyAsync(this::buildResponse, config.getExecutor())
.exceptionally(
t -> {
if (t.getCause() instanceof ConnectTimeoutException
|| t.getCause() instanceof SocketTimeoutException
|| t.getCause() instanceof ConnectException
|| t.getCause() instanceof TimeoutException
|| t.getCause() instanceof ConnectionPoolTimeoutException
|| t.getCause() instanceof NoHttpResponseException) {
return new HttpResponse(true);
} else if (t.getCause() instanceof HttpException) {
return new HttpResponse().setNetworkError(true);
}
throw new AlgoliaRuntimeException(t);
});
}
public static CloseableHttpResponse generateHttpResponse(Exception exception) {
if (exception instanceof HttpHostConnectException) {
return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Connection refused");
} else if (exception instanceof ConnectionPoolTimeoutException) {
return generateHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, "Connection pool timeout");
} else if (exception instanceof ConnectTimeoutException) {
return generateHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, "Connect timeout");
} else if (exception instanceof SocketTimeoutException) {
return generateHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, "Socket timeout");
} else if (exception instanceof SocketException) {
return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Socket Exception");
} else if (exception instanceof ClientProtocolException) {
String message = exception.getMessage();
if (message == null && exception.getCause() != null) {
message = exception.getCause().getMessage();
}
return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Protocol error: " + message);
} else {
LOG.error("Error retrieving URL", exception);
return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Error retrieving URL");
}
}
private ElasticSearchException handleConnectionPoolTimeOutException(ConnectionPoolTimeoutException ex){
ClientConfiguration configuration = ClientConfiguration.getClientConfiguration(this.httpPool);
if(configuration == null){
return new ElasticSearchException(ex);
}
else{
StringBuilder builder = new StringBuilder();
builder.append("Wait timeout for ").append(configuration.getConnectionRequestTimeout()).append("ms for idle http connection from http connection pool.");
return new ElasticSearchException(builder.toString(),ex);
}
}
@Test(timeout = 60 * 1000)
public void leasing_a_new_connection_fails_with_connection_pool_timeout() {
AmazonSyncHttpClient httpClient = HttpTestUtils.testClientBuilder()
.retryPolicy(RetryPolicy.none())
.httpClient(ApacheHttpClient.builder()
.connectionTimeout(Duration.ofMillis(100))
.maxConnections(1)
.build())
.build();
SdkHttpFullRequest request = server.configureHttpEndpoint(SdkHttpFullRequest.builder())
.method(SdkHttpMethod.GET)
.build();
// Block the first connection in the pool with this request.
httpClient.requestExecutionBuilder()
.request(request)
.originalRequest(NoopTestRequest.builder().build())
.executionContext(executionContext(request))
.execute(combinedSyncResponseHandler(new EmptySdkResponseHandler(), null));
try {
// A new connection will be leased here which would fail in
// ConnectionPoolTimeoutException.
httpClient.requestExecutionBuilder()
.request(request)
.originalRequest(NoopTestRequest.builder().build())
.executionContext(executionContext(request))
.execute(combinedSyncResponseHandler(null, null));
Assert.fail("Connection pool timeout exception is expected!");
} catch (SdkClientException e) {
Assert.assertTrue(e.getCause() instanceof ConnectionPoolTimeoutException);
}
}
@Override
protected BasicPoolEntry getEntryBlocking(HttpRoute route, Object state,
long timeout, TimeUnit tunit, WaitingThreadAborter aborter)
throws ConnectionPoolTimeoutException, InterruptedException {
Stopwatch stopWatch = requestTimer.start();
try {
return super.getEntryBlocking(route, state, timeout, tunit, aborter);
} finally {
stopWatch.stop();
}
}
@Override
public HttpClientConnection get(long timeout, TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
return null;
}
public HttpClientConnection getConn(ConnectionRequest mConn)
throws InterruptedException, ConnectionPoolTimeoutException, ExecutionException {
HttpClientConnection conn = mConn.get(30, TimeUnit.SECONDS);
return conn;
}
private boolean isRemoteUnavailable(final Exception e) {
if (e instanceof ConnectionPoolTimeoutException) {
return false;
}
return true;
}