Skip to content

Commit 1fada78

Browse files
author
chenhuawei
committed
HttpAsyncClient
1 parent 826f0af commit 1fada78

3 files changed

Lines changed: 176 additions & 0 deletions

File tree

http-async-client/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>biz.chenxu</groupId>
7+
<artifactId>tutorial-java</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>http-async-client</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.apache.httpcomponents</groupId>
17+
<artifactId>httpasyncclient</artifactId>
18+
<version>4.1.3</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>commons-io</groupId>
22+
<artifactId>commons-io</artifactId>
23+
<version>2.4</version>
24+
</dependency>
25+
</dependencies>
26+
27+
<build>
28+
29+
<plugins>
30+
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-jar-plugin</artifactId>
34+
<configuration>
35+
<archive>
36+
<manifest>
37+
<mainClass>biz.chenxu.tutorial.HttpAsyncClientApplication</mainClass>
38+
</manifest>
39+
</archive>
40+
</configuration>
41+
</plugin>
42+
43+
44+
</plugins>
45+
</build>
46+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package biz.chenxu.tutorial;
2+
3+
import org.apache.commons.io.IOUtils;
4+
import org.apache.http.HttpResponse;
5+
import org.apache.http.client.methods.HttpPost;
6+
import org.apache.http.concurrent.FutureCallback;
7+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
8+
import org.apache.http.impl.nio.client.HttpAsyncClients;
9+
10+
import java.io.IOException;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.Future;
13+
14+
public class HttpAsyncClientApplication {
15+
16+
public static void main(String[] args) {
17+
18+
CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
19+
try {
20+
// start reactorThread
21+
httpClient.start();
22+
23+
HttpPost post = new HttpPost("http://www.baidu.com");
24+
Future<HttpResponse> future = httpClient.execute(post, new FutureCallback<HttpResponse>() {
25+
26+
@Override
27+
public void completed(HttpResponse httpResponse) {
28+
System.out.println("completed");
29+
}
30+
31+
@Override
32+
public void failed(Exception e) {
33+
e.printStackTrace();
34+
}
35+
36+
@Override
37+
public void cancelled() {
38+
System.out.println("cancelled");
39+
}
40+
});
41+
HttpResponse httpResponse = future.get();
42+
String content = IOUtils.toString(httpResponse.getEntity().getContent(), "utf-8");
43+
System.out.println(content);
44+
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
} catch (ExecutionException e) {
48+
e.printStackTrace();
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
}
52+
53+
try {
54+
// join reactorThread
55+
httpClient.close();
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
}

pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>biz.chenxu</groupId>
8+
<artifactId>tutorial-java</artifactId>
9+
<packaging>pom</packaging>
10+
<version>1.0.0-SNAPSHOT</version>
11+
12+
<modules>
13+
<module>http-async-client</module>
14+
</modules>
15+
16+
<properties>
17+
<maven.test.skip>true</maven.test.skip>
18+
<maven.javadoc.skip>true</maven.javadoc.skip>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<scm>
23+
<url>https://github.com/chenhuawei/tutorial-java</url>
24+
<connection>scm:git:https://github.com/chenhuawei/tutorial-java.git</connection>
25+
<developerConnection>scm:git:https://github.com/chenhuawei/tutorial-java.git</developerConnection>
26+
</scm>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
<version>3.1</version>
34+
<configuration>
35+
<source>1.8</source>
36+
<target>1.8</target>
37+
</configuration>
38+
</plugin>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-source-plugin</artifactId>
42+
<version>2.4</version>
43+
<executions>
44+
<execution>
45+
<phase>package</phase>
46+
<goals>
47+
<goal>jar-no-fork</goal>
48+
</goals>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-release-plugin</artifactId>
55+
<version>2.5.3</version>
56+
<configuration>
57+
<tagNameFormat>v@{project.version}</tagNameFormat>
58+
<autoVersionSubmodules>true</autoVersionSubmodules>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.codehaus.mojo</groupId>
63+
<artifactId>versions-maven-plugin</artifactId>
64+
<version>2.5</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
</project>

0 commit comments

Comments
 (0)