Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 4f05a8a

Browse files
committed
Move tests from AsyncHttpClient library
0 parents  commit 4f05a8a

File tree

98 files changed

+4675
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4675
-0
lines changed

Diff for: README

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Async Http Client
2+
Copyright 2010 Ning Inc
3+
4+
DESCRIPTION
5+
-----------
6+
Getting started: http://is.gd/ja6My
7+
8+
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:
9+
10+
<repository>
11+
<id>Sonatype</id>
12+
<name>Sonatype Release</name>
13+
<url>http://oss.sonatype.org/content/repositories/releases </url>
14+
</repository>
15+
16+
and then define the dependency as:
17+
18+
<dependency>
19+
<groupId>com.ning</groupId>
20+
<artifactId>async-http-client</artifactId>
21+
<version>1.0.0</version>
22+
</dependency>
23+
24+
You can also download the artifact
25+
26+
http://oss.sonatype.org/content/repositories/releases
27+
28+
Then in your code you can simply do:
29+
30+
import com.ning.http.client.*;
31+
import java.util.concurrent.Future;
32+
33+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
34+
Future<Response> f = asyncHttpClient.prepareGet("http://www.ning.com/ ").execute();
35+
Response r = f.get();
36+
37+
You can also accomplish asynchronous operation without using a Future if you want to receive and process the response in your handler:
38+
39+
import com.ning.http.client.*;
40+
import java.util.concurrent.Future;
41+
42+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
43+
asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Response>(){
44+
45+
@Override
46+
public Response onCompleted(Response response) throws Exception{
47+
// Do something with the Response
48+
// ...
49+
return response;
50+
}
51+
52+
@Override
53+
public void onThrowable(Throwable t){
54+
// Something wrong happened.
55+
}
56+
});
57+
58+
You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response
59+
60+
import com.ning.http.client.*;
61+
import java.util.concurrent.Future;
62+
63+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
64+
Future<Integer> f = asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Integer>(){
65+
66+
@Override
67+
public Integer onCompleted(Response response) throws Exception{
68+
// Do something with the Response
69+
return response.getStatusCode();
70+
}
71+
72+
@Override
73+
public void onThrowable(Throwable t){
74+
// Something wrong happened.
75+
}
76+
});
77+
78+
int statuѕCode = f.get();
79+
80+
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:
81+
82+
import com.ning.http.client.*;
83+
import java.util.concurrent.Future;
84+
85+
AsyncHttpClient c = new AsyncHttpClient();
86+
Future<String> f = c.prepareGet("http://www.ning.com/ ").execute(new AsyncHandler<String>() {
87+
private StringBuilder builder = new StringBuilder();
88+
89+
@Override
90+
public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
91+
int statusCode = status.getStatusCode();
92+
// The Status have been read
93+
// If you don't want to read the headers,body or stop processing the response
94+
return STATE.ABORT;
95+
}
96+
97+
@Override
98+
public STATE onHeadersReceived(HttpResponseHeaders h) throws Exception {
99+
Headers headers = h.getHeaders();
100+
// The headers have been read
101+
// If you don't want to read the body, or stop processing the response
102+
return STATE.ABORT;
103+
}
104+
105+
@Override
106+
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
107+
builder.append(new String(bodyPart.getBodyPartBytes()));
108+
return STATE.CONTINU
109+
}
110+
111+
@Override
112+
public String onCompleted() throws Exception {
113+
// Will be invoked once the response has been fully read or a ResponseComplete exception
114+
// has been thrown.
115+
return builder.toString();
116+
}
117+
118+
@Override
119+
public void onThrowable(Throwable t) {
120+
}
121+
});
122+
123+
String bodyResponse = f.get();
124+
125+
126+
Finally, you can also configure the AsyncHttpClient via it's AsyncHttpClientConfig object:
127+
128+
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer("127.0.0.1", 38080)).build();
129+
AsyncHttpClient c = new AsyncHttpClient(cf);
130+
131+
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.
132+
133+
Keep up to date on the library development by joining the Asynchronous HTTP Client discussion group
134+
135+
http://groups.google.com/group/asynchttpclient
136+

0 commit comments

Comments
 (0)