You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Follow [@AsyncHttpClient](https://twitter.com/AsyncHttpClient) on Twitter.
5
4
6
-
[Getting](https://jfarcand.wordpress.com/2010/12/21/going-asynchronous-using-asynchttpclient-the-basic/)[started](https://jfarcand.wordpress.com/2011/01/04/going-asynchronous-using-asynchttpclient-the-complex/), and use [WebSockets](http://jfarcand.wordpress.com/2011/12/21/writing-websocket-clients-using-asynchttpclient/)
5
+
The AsyncHttpClient (AHC) library allows Java applications to easily execute HTTP requests and asynchronously process HTTP responses.
6
+
The library also supports the WebSocket Protocol.
7
7
8
-
The Async Http Client library's purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses.
9
-
The library also supports the WebSocket Protocol. The Async HTTP Client library is simple to use.
10
-
11
-
It's built on top of [Netty](https://github.com/netty/netty) and currently requires JDK8.
Future<Response> f = asyncHttpClient.prepareGet("http://www.example.com/").execute();
37
-
Response r = f.get();
38
-
```
28
+
Import the Dsl helpers to use convenient methods to bootstrap components:
39
29
40
-
Note that in this case all the content must be read fully in memory, even if you used `getResponseBodyAsStream()` method on returned `Response` object.
30
+
```java
31
+
import staticorg.asynchttpclient.Dsl.*;
32
+
```
41
33
42
-
You can also accomplish asynchronous (non-blocking) operation without using a Future if you want to receive and process the response in your handler:
(this will also fully read `Response` in memory before calling `onCompleted`)
42
+
AsyncHttpClient instances must be closed (call the `close` method) once you're done with them, typically when shutting down your application.
43
+
If you don't, you'll experience threads hanging and resource leaks.
44
+
45
+
AsyncHttpClient instances are intended to be global resources that share the same lifecycle as the application.
46
+
Typically, AHC will usually underperform if you create a new client for each request, as it will create new threads and connection pools for each.
47
+
It's possible to create shared resources (EventLoop and Timer) beforehand and pass them to multiple client instances in the config. You'll then be responsible for closing those shared resources.
48
+
49
+
## Configuration
66
50
67
-
Alternatively you may use continuations (through Java 8 class `CompletableFuture<T>`) to accomplish asynchronous (non-blocking) solution. The equivalent continuation approach to the previous example is:
51
+
Finally, you can also configure the AsyncHttpClient instance via its AsyncHttpClientConfig object:
68
52
69
53
```java
70
54
import staticorg.asynchttpclient.Dsl.*;
71
55
56
+
AsyncHttpClient c = asyncHttpClient(config().setProxyServer(proxyServer("127.0.0.1", 38080)));
57
+
```
58
+
59
+
## HTTP
60
+
61
+
### Sending Requests
62
+
63
+
### Basics
64
+
65
+
AHC provides 2 APIs for defining requests: bound and unbound.
66
+
`AsyncHttpClient` and Dls` provide methods for standard HTTP methods (POST, PUT, etc) but you can also pass a custom one.
You may get the complete maven project for this simple demo from [org.asynchttpclient.example](https://github.com/AsyncHttpClient/async-http-client/tree/master/example/src/main/java/org/asynchttpclient/example)
79
+
#### Setting Request Body
80
+
81
+
Use the `setBody` method to add a body to the request.
which is something you want to do for large responses: this way you can process content as soon as it becomes available, piece by piece, without having to buffer it all in memory.
138
+
If the `executor` parameter is null, callback will be executed in the IO thread.
139
+
You *MUST NEVER PERFORM BLOCKING* operations in there, typically sending another request and block on a future.
113
140
114
-
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:
141
+
#### Using custom AsyncHandlers
142
+
143
+
`execute` methods can take an `org.asynchttpclient.AsyncHandler` to be notified on the different events, such as receiving the status, the headers and body chunks.
144
+
When you don't specify one, AHC will use a `org.asynchttpclient.AsyncCompletionHandler`;
145
+
146
+
`AsyncHandler` methods can let you abort processing early (return `AsyncHandler.State.ABORT`) and can let you return a computation result from `onCompleted` that will be used as the Future's result.
147
+
See `AsyncCompletionHandler` implementation as an example.
148
+
149
+
The below sample just capture the response status and skips processing the response body chunks.
150
+
151
+
Note that returning `ABORT` closed the underlying connection.
115
152
116
153
```java
117
154
import staticorg.asynchttpclient.Dsl.*;
118
-
119
155
importorg.asynchttpclient.*;
120
-
importjava.util.concurrent.Future;
121
-
122
-
AsyncHttpClient c = asyncHttpClient();
123
-
Future<String> f = c.prepareGet("http://www.example.com/").execute(newAsyncHandler<String>() {
.thenApply(resp -> { /* Do something with the Response */return resp; });
199
+
promise.join(); // wait for completion
175
200
```
176
201
202
+
You may get the complete maven project for this simple demo from [org.asynchttpclient.example](https://github.com/AsyncHttpClient/async-http-client/tree/master/example/src/main/java/org/asynchttpclient/example)
203
+
177
204
## WebSocket
178
205
179
-
Async Http Client also supports WebSocket by simply doing:
206
+
Async Http Client also supports WebSocket.
207
+
You need to pass a `WebSocketUpgradeHandler` where you would register a `WebSocketListener`.
0 commit comments