1
- # Async Http Client [ ![ Build] ( https://github.com/AsyncHttpClient/async-http-client/actions/workflows/builds.yml/badge.svg )] ( https://github.com/AsyncHttpClient/async-http-client/actions/workflows/builds.yml ) [ ![ Maven Central] ( https://maven-badges.herokuapp.com/maven-central/org.asynchttpclient/async-http-client/badge.svg )] ( https://maven-badges.herokuapp.com/maven-central/org.asynchttpclient/async-http-client/ )
1
+ # Async Http Client
2
+ [ ![ Build] ( https://github.com/AsyncHttpClient/async-http-client/actions/workflows/builds.yml/badge.svg )] ( https://github.com/AsyncHttpClient/async-http-client/actions/workflows/builds.yml )
3
+ [ ![ Maven Central] ( https://maven-badges.herokuapp.com/maven-central/org.asynchttpclient/async-http-client/badge.svg )] ( https://maven-badges.herokuapp.com/maven-central/org.asynchttpclient/async-http-client/ )
2
4
3
5
Follow [ @AsyncHttpClient ] ( https://twitter.com/AsyncHttpClient ) on Twitter.
4
6
@@ -18,7 +20,7 @@ Add a dependency on the main AsyncHttpClient artifact:
18
20
<dependency >
19
21
<groupId >org.asynchttpclient</groupId >
20
22
<artifactId >async-http-client</artifactId >
21
- <version >3.0.0-SNAPSHOT </version >
23
+ <version >3.0.0.Beta1 </version >
22
24
</dependency >
23
25
</dependencies >
24
26
```
@@ -140,17 +142,18 @@ The point of using a non blocking client is to *NOT BLOCK* the calling thread!
140
142
You can configure listeners to be notified of the Future's completion.
141
143
142
144
``` java
143
- ListenableFuture<Response > whenResponse= ??? ;
144
- Runnable callback= () - > {
145
- try {
146
- Response response= whenResponse. get();
147
- System . out. println(response);
148
- } catch (InterruptedException | ExecutionException e){
149
- e. printStackTrace();
150
- }
145
+ ListenableFuture<Response > whenResponse = ??? ;
146
+ Runnable callback = () - > {
147
+ try {
148
+ Response response = whenResponse. get();
149
+ System . out. println(response);
150
+ } catch (InterruptedException | ExecutionException e) {
151
+ e. printStackTrace();
152
+ }
151
153
};
152
- java.util.concurrent. Executor executor= ??? ;
153
- whenResponse. addListener(()- > ??? ,executor);
154
+
155
+ java.util.concurrent. Executor executor = ??? ;
156
+ whenResponse. addListener(() - > ??? , executor);
154
157
```
155
158
156
159
If the ` executor ` parameter is null, callback will be executed in the IO thread.
@@ -175,32 +178,38 @@ import static org.asynchttpclient.Dsl.*;
175
178
import org.asynchttpclient.* ;
176
179
import io.netty.handler.codec.http.HttpHeaders ;
177
180
178
- Future<Integer > whenStatusCode= asyncHttpClient. prepareGet(" http://www.example.com/" )
179
- .execute(new AsyncHandler<Integer > (){
180
- private Integer status;
181
- @Override
182
- public State onStatusReceived (HttpResponseStatus responseStatus )throws Exception {
183
- status= responseStatus. getStatusCode();
184
- return State . ABORT ;
185
- }
186
- @Override
187
- public State onHeadersReceived (HttpHeaders headers )throws Exception {
188
- return State . ABORT ;
189
- }
190
- @Override
191
- public State onBodyPartReceived (HttpResponseBodyPart bodyPart )throws Exception {
192
- return State . ABORT ;
193
- }
194
- @Override
195
- public Integer onCompleted ()throws Exception {
196
- return status;
197
- }
198
- @Override
199
- public void onThrowable (Throwable t ){
200
- }
181
+ Future<Integer > whenStatusCode = asyncHttpClient. prepareGet(" http://www.example.com/" )
182
+ .execute(new AsyncHandler<Integer > () {
183
+ private Integer status;
184
+
185
+ @Override
186
+ public State onStatusReceived (HttpResponseStatus responseStatus ) throws Exception {
187
+ status = responseStatus. getStatusCode();
188
+ return State . ABORT ;
189
+ }
190
+
191
+ @Override
192
+ public State onHeadersReceived (HttpHeaders headers ) throws Exception {
193
+ return State . ABORT ;
194
+ }
195
+
196
+ @Override
197
+ public State onBodyPartReceived (HttpResponseBodyPart bodyPart ) throws Exception {
198
+ return State . ABORT ;
199
+ }
200
+
201
+ @Override
202
+ public Integer onCompleted () throws Exception {
203
+ return status;
204
+ }
205
+
206
+ @Override
207
+ public void onThrowable (Throwable t ) {
208
+ t. printStackTrace();
209
+ }
201
210
});
202
211
203
- Integer statusCode= whenStatusCode. get();
212
+ Integer statusCode = whenStatusCode. get();
204
213
```
205
214
206
215
#### Using Continuations
@@ -232,23 +241,25 @@ WebSocket websocket=c.prepareGet("ws://demos.kaazing.com/echo")
232
241
.execute(new WebSocketUpgradeHandler .Builder (). addWebSocketListener(
233
242
new WebSocketListener (){
234
243
235
- @Override
236
- public void onOpen (WebSocket websocket ){
237
- websocket. sendTextFrame(" ..." ). sendTextFrame(" ..." );
238
- }
239
-
240
- @Override
241
- public void onClose (WebSocket websocket ){
242
- }
243
-
244
- @Override
245
- public void onTextFrame (String payload ,boolean final Fragment ,int rsv ){
246
- System . out. println(payload);
247
- }
248
-
249
- @Override
250
- public void onError (Throwable t ){
251
- }
244
+ @Override
245
+ public void onOpen (WebSocket websocket ){
246
+ websocket. sendTextFrame(" ..." ). sendTextFrame(" ..." );
247
+ }
248
+
249
+ @Override
250
+ public void onClose (WebSocket websocket ) {
251
+ // ...
252
+ }
253
+
254
+ @Override
255
+ public void onTextFrame (String payload ,boolean final Fragment ,int rsv ){
256
+ System . out. println(payload);
257
+ }
258
+
259
+ @Override
260
+ public void onError (Throwable t ){
261
+ t. printStackTrace();
262
+ }
252
263
}). build()). get();
253
264
```
254
265
@@ -258,16 +269,16 @@ AsyncHttpClient has build in support for the WebDAV protocol.
258
269
The API can be used the same way normal HTTP request are made:
259
270
260
271
``` java
261
- Request mkcolRequest= new RequestBuilder (" MKCOL" ). setUrl(" http://host:port/folder1" ). build();
272
+ Request mkcolRequest= new RequestBuilder (" MKCOL" ). setUrl(" http://host:port/folder1" ). build();
262
273
Response response= c. executeRequest(mkcolRequest). get();
263
274
```
264
275
265
276
or
266
277
267
278
``` java
268
- Request propFindRequest= new RequestBuilder (" PROPFIND" ). setUrl(" http://host:port" ). build();
269
- Response response= c. executeRequest(propFindRequest,new AsyncHandler (){
270
- // ...
279
+ Request propFindRequest= new RequestBuilder (" PROPFIND" ). setUrl(" http://host:port" ). build();
280
+ Response response= c. executeRequest(propFindRequest,new AsyncHandler () {
281
+ // ...
271
282
}). get();
272
283
```
273
284
0 commit comments