This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
README
136 lines (99 loc) · 4.99 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Async Http Client
Copyright 2010 Ning Inc
DESCRIPTION
-----------
Getting started: http://is.gd/ja6My
Async Http Client library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses. The Async HTTP Client library is simple to use. First, in order to add it to your Maven project, simply add this dependency:
<repository>
<id>Sonatype</id>
<name>Sonatype Release</name>
<url>http://oss.sonatype.org/content/repositories/releases </url>
</repository>
and then define the dependency as:
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<version>1.0.0</version>
</dependency>
You can also download the artifact
http://oss.sonatype.org/content/repositories/releases
Then in your code you can simply do:
import com.ning.http.client.*;
import java.util.concurrent.Future;
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Future<Response> f = asyncHttpClient.prepareGet("http://www.ning.com/ ").execute();
Response r = f.get();
You can also accomplish asynchronous operation without using a Future if you want to receive and process the response in your handler:
import com.ning.http.client.*;
import java.util.concurrent.Future;
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Response>(){
@Override
public Response onCompleted(Response response) throws Exception{
// Do something with the Response
// ...
return response;
}
@Override
public void onThrowable(Throwable t){
// Something wrong happened.
}
});
You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response
import com.ning.http.client.*;
import java.util.concurrent.Future;
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Future<Integer> f = asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Integer>(){
@Override
public Integer onCompleted(Response response) throws Exception{
// Do something with the Response
return response.getStatusCode();
}
@Override
public void onThrowable(Throwable t){
// Something wrong happened.
}
});
int statuѕCode = f.get();
You have full control on the Response life cycle, so you can decide at any moment to stop processing what the server is sending back:
import com.ning.http.client.*;
import java.util.concurrent.Future;
AsyncHttpClient c = new AsyncHttpClient();
Future<String> f = c.prepareGet("http://www.ning.com/ ").execute(new AsyncHandler<String>() {
private StringBuilder builder = new StringBuilder();
@Override
public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
int statusCode = status.getStatusCode();
// The Status have been read
// If you don't want to read the headers,body or stop processing the response
return STATE.ABORT;
}
@Override
public STATE onHeadersReceived(HttpResponseHeaders h) throws Exception {
Headers headers = h.getHeaders();
// The headers have been read
// If you don't want to read the body, or stop processing the response
return STATE.ABORT;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
builder.append(new String(bodyPart.getBodyPartBytes()));
return STATE.CONTINU
}
@Override
public String onCompleted() throws Exception {
// Will be invoked once the response has been fully read or a ResponseComplete exception
// has been thrown.
return builder.toString();
}
@Override
public void onThrowable(Throwable t) {
}
});
String bodyResponse = f.get();
Finally, you can also configure the AsyncHttpClient via it's AsyncHttpClientConfig object:
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer("127.0.0.1", 38080)).build();
AsyncHttpClient c = new AsyncHttpClient(cf);
The library uses Java non blocking I/O for supporting asynchronous operations. The default asynchronous provider is build on top of Netty (http://www.jboss.org/netty), the Java NIO Client Server Socket Framework from JBoss, but the library exposes a configurable provider SPI which allows to easily plug in other frameworks.
Keep up to date on the library development by joining the Asynchronous HTTP Client discussion group
http://groups.google.com/group/asynchttpclient