Skip to content

Instantly share code, notes, and snippets.

@madawei2699
Created March 5, 2022 15:42
Show Gist options
  • Save madawei2699/b98e702773b53489173a8c25c52173c3 to your computer and use it in GitHub Desktop.
Save madawei2699/b98e702773b53489173a8c25c52173c3 to your computer and use it in GitHub Desktop.
Apache Http Client Time Test
package org.example;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.conn.NoopIOSessionStrategy;
import org.apache.http.nio.reactor.IOReactorException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws IOReactorException {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1)
.setSocketTimeout(500)
.setRedirectsEnabled(true)
.build();
HttpUriRequest httpUriRequest = new HttpGet("http://255.255.33.44:8001/");
httpUriRequest.addHeader("Connection", "close");
IOReactorConfig reactorConfig = IOReactorConfig.custom()
// .setConnectTimeout(500)
// .setSoTimeout(500)
.setShutdownGracePeriod(1)
.build();
DefaultConnectingIOReactor defaultConnectingIOReactor = new DefaultConnectingIOReactor(reactorConfig);
Registry registry = RegistryBuilder.create()
.register("http", NoopIOSessionStrategy.INSTANCE)
.build();
PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(defaultConnectingIOReactor, registry);
HttpClientContext context = new HttpClientContext();
context.setRequestConfig(requestConfig);
connectionManager.setMaxTotal(1);
CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.setConnectionManager(connectionManager)
.build();
client.start();
long startTime = System.nanoTime();
client.execute(httpUriRequest, context, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse httpResponse) {
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
long stopTime = System.nanoTime();
System.out.print("=====completed, timeElapsed: ");
System.out.print(TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS));
System.out.println(" ms");
System.out.print(statusCode);
}
@Override
public void failed(Exception e) {
System.out.print("=====failed, timeElapsed: ");
System.out.print(TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS));
System.out.println(" ms");
System.out.print("=====error msg: " + e.getMessage());
try {
connectionManager.shutdown();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void cancelled() {
httpUriRequest.abort();
try {
connectionManager.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test-appach-http-client</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.4</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment