Skip to content

Commit fc94526

Browse files
committed
Revive the project
1 parent 900cd27 commit fc94526

File tree

1 file changed

+101
-95
lines changed

1 file changed

+101
-95
lines changed

README.md

Lines changed: 101 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Looking for a new maintainer
2-
3-
Due to lack of time on my end and this repo being dead for most of the last couple of years, I am bringing the repo back up for maintenance. Reach out to me on Twitter - @TomGranot - for more info.
4-
51
# Async Http Client [![Build Status](https://travis-ci.org/AsyncHttpClient/async-http-client.svg?branch=master)](https://travis-ci.org/AsyncHttpClient/async-http-client) [![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/)
62

73
Follow [@AsyncHttpClient](https://twitter.com/AsyncHttpClient) on Twitter.
@@ -13,7 +9,8 @@ It's built on top of [Netty](https://github.com/netty/netty). It's currently com
139

1410
## New Roadmap RFCs!
1511

16-
Well, not really RFCs, but as [I](https://github.com/TomGranot) am ramping up to release a new version, I would appreciate the comments from the community. Please add an issue and [label it RFC](https://github.com/AsyncHttpClient/async-http-client/labels/RFC) and I'll take a look!
12+
Well, not really RFCs, but as [I](https://github.com/TomGranot) am ramping up to release a new version, I would appreciate the comments from the community. Please add an issue
13+
and [label it RFC](https://github.com/AsyncHttpClient/async-http-client/labels/RFC) and I'll take a look!
1714

1815
## Installation
1916

@@ -22,6 +19,7 @@ Binaries are deployed on Maven Central.
2219
Import the AsyncHttpClient Bill of Materials (BOM) to add dependency management for AsyncHttpClient artifacts to your project:
2320

2421
```xml
22+
2523
<dependencyManagement>
2624
<dependencies>
2725
<dependency>
@@ -35,13 +33,14 @@ Import the AsyncHttpClient Bill of Materials (BOM) to add dependency management
3533
</dependencyManagement>
3634
```
3735

38-
Add a dependency on the main AsyncHttpClient artifact:
36+
Add a dependency on the main AsyncHttpClient artifact:
3937

4038
```xml
39+
4140
<dependencies>
4241
<dependency>
43-
<groupId>org.asynchttpclient</groupId>
44-
<artifactId>async-http-client</artifactId>
42+
<groupId>org.asynchttpclient</groupId>
43+
<artifactId>async-http-client</artifactId>
4544
</dependency>
4645
</dependencies>
4746
```
@@ -75,15 +74,16 @@ import static org.asynchttpclient.Dsl.*;
7574
```java
7675
import static org.asynchttpclient.Dsl.*;
7776

78-
AsyncHttpClient asyncHttpClient = asyncHttpClient();
77+
AsyncHttpClient asyncHttpClient=asyncHttpClient();
7978
```
8079

8180
AsyncHttpClient instances must be closed (call the `close` method) once you're done with them, typically when shutting down your application.
8281
If you don't, you'll experience threads hanging and resource leaks.
8382

8483
AsyncHttpClient instances are intended to be global resources that share the same lifecycle as the application.
8584
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.
86-
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.
85+
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
86+
those shared resources.
8787

8888
## Configuration
8989

@@ -92,7 +92,7 @@ Finally, you can also configure the AsyncHttpClient instance via its AsyncHttpCl
9292
```java
9393
import static org.asynchttpclient.Dsl.*;
9494

95-
AsyncHttpClient c = asyncHttpClient(config().setProxyServer(proxyServer("127.0.0.1", 38080)));
95+
AsyncHttpClient c=asyncHttpClient(config().setProxyServer(proxyServer("127.0.0.1",38080)));
9696
```
9797

9898
## HTTP
@@ -108,18 +108,19 @@ AHC provides 2 APIs for defining requests: bound and unbound.
108108
import org.asynchttpclient.*;
109109

110110
// bound
111-
Future<Response> whenResponse = asyncHttpClient.prepareGet("http://www.example.com/").execute();
111+
Future<Response> whenResponse=asyncHttpClient.prepareGet("http://www.example.com/").execute();
112112

113113
// unbound
114-
Request request = get("http://www.example.com/").build();
115-
Future<Response> whenResponse = asyncHttpClient.executeRequest(request);
114+
Request request=get("http://www.example.com/").build();
115+
Future<Response> whenResponse=asyncHttpClient.executeRequest(request);
116116
```
117117

118118
#### Setting Request Body
119119

120120
Use the `setBody` method to add a body to the request.
121121

122122
This body can be of type:
123+
123124
* `java.io.File`
124125
* `byte[]`
125126
* `List<byte[]>`
@@ -130,13 +131,14 @@ This body can be of type:
130131
* `org.asynchttpclient.request.body.generator.BodyGenerator`
131132

132133
`BodyGenerator` is a generic abstraction that let you create request bodies on the fly.
133-
Have a look at `FeedableBodyGenerator` if you're looking for a way to pass requests chunks on the fly.
134+
Have a look at `FeedableBodyGenerator` if you're looking for a way to pass requests chunks on the fly.
134135

135136
#### Multipart
136137

137138
Use the `addBodyPart` method to add a multipart part to the request.
138139

139140
This part can be of type:
141+
140142
* `ByteArrayPart`
141143
* `FilePart`
142144
* `InputStreamPart`
@@ -149,8 +151,8 @@ This part can be of type:
149151
`execute` methods return a `java.util.concurrent.Future`. You can simply block the calling thread to get the response.
150152

151153
```java
152-
Future<Response> whenResponse = asyncHttpClient.prepareGet("http://www.example.com/").execute();
153-
Response response = whenResponse.get();
154+
Future<Response> whenResponse=asyncHttpClient.prepareGet("http://www.example.com/").execute();
155+
Response response=whenResponse.get();
154156
```
155157

156158
This is useful for debugging but you'll most likely hurt performance or create bugs when running such code on production.
@@ -159,20 +161,20 @@ The point of using a non blocking client is to *NOT BLOCK* the calling thread!
159161
### Setting callbacks on the ListenableFuture
160162

161163
`execute` methods actually return a `org.asynchttpclient.ListenableFuture` similar to Guava's.
162-
You can configure listeners to be notified of the Future's completion.
164+
You can configure listeners to be notified of the Future's completion.
163165

164166
```java
165-
ListenableFuture<Response> whenResponse = ???;
166-
Runnable callback = () -> {
167-
try {
168-
Response response = whenResponse.get();
169-
System.out.println(response);
170-
} catch (InterruptedException | ExecutionException e) {
171-
e.printStackTrace();
172-
}
173-
};
174-
java.util.concurrent.Executor executor = ???;
175-
whenResponse.addListener(() -> ???, executor);
167+
ListenableFuture<Response> whenResponse=???;
168+
Runnable callback=()->{
169+
try{
170+
Response response=whenResponse.get();
171+
System.out.println(response);
172+
}catch(InterruptedException|ExecutionException e){
173+
e.printStackTrace();
174+
}
175+
};
176+
java.util.concurrent.Executor executor=???;
177+
whenResponse.addListener(()->???,executor);
176178
```
177179

178180
If the `executor` parameter is null, callback will be executed in the IO thread.
@@ -183,7 +185,8 @@ You *MUST NEVER PERFORM BLOCKING* operations in there, typically sending another
183185
`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.
184186
When you don't specify one, AHC will use a `org.asynchttpclient.AsyncCompletionHandler`;
185187

186-
`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.
188+
`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
189+
as the Future's result.
187190
See `AsyncCompletionHandler` implementation as an example.
188191

189192
The below sample just capture the response status and skips processing the response body chunks.
@@ -192,35 +195,36 @@ Note that returning `ABORT` closes the underlying connection.
192195

193196
```java
194197
import static org.asynchttpclient.Dsl.*;
198+
195199
import org.asynchttpclient.*;
196200
import io.netty.handler.codec.http.HttpHeaders;
197201

198-
Future<Integer> whenStatusCode = asyncHttpClient.prepareGet("http://www.example.com/")
199-
.execute(new AsyncHandler<Integer>() {
200-
private Integer status;
201-
@Override
202-
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
203-
status = responseStatus.getStatusCode();
204-
return State.ABORT;
205-
}
206-
@Override
207-
public State onHeadersReceived(HttpHeaders headers) throws Exception {
208-
return State.ABORT;
209-
}
210-
@Override
211-
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
212-
return State.ABORT;
213-
}
214-
@Override
215-
public Integer onCompleted() throws Exception {
216-
return status;
217-
}
218-
@Override
219-
public void onThrowable(Throwable t) {
220-
}
221-
});
222-
223-
Integer statusCode = whenStatusCode.get();
202+
Future<Integer> whenStatusCode=asyncHttpClient.prepareGet("http://www.example.com/")
203+
.execute(new AsyncHandler<Integer>(){
204+
private Integer status;
205+
@Override
206+
public State onStatusReceived(HttpResponseStatus responseStatus)throws Exception{
207+
status=responseStatus.getStatusCode();
208+
return State.ABORT;
209+
}
210+
@Override
211+
public State onHeadersReceived(HttpHeaders headers)throws Exception{
212+
return State.ABORT;
213+
}
214+
@Override
215+
public State onBodyPartReceived(HttpResponseBodyPart bodyPart)throws Exception{
216+
return State.ABORT;
217+
}
218+
@Override
219+
public Integer onCompleted()throws Exception{
220+
return status;
221+
}
222+
@Override
223+
public void onThrowable(Throwable t){
224+
}
225+
});
226+
227+
Integer statusCode=whenStatusCode.get();
224228
```
225229

226230
#### Using Continuations
@@ -230,45 +234,46 @@ Beware that canceling this `CompletableFuture` won't properly cancel the ongoing
230234
There's a very good chance we'll return a `CompletionStage` instead in the next release.
231235

232236
```java
233-
CompletableFuture<Response> whenResponse = asyncHttpClient
234-
.prepareGet("http://www.example.com/")
235-
.execute()
236-
.toCompletableFuture()
237-
.exceptionally(t -> { /* Something wrong happened... */ } )
238-
.thenApply(response -> { /* Do something with the Response */ return resp; });
239-
whenResponse.join(); // wait for completion
237+
CompletableFuture<Response> whenResponse=asyncHttpClient
238+
.prepareGet("http://www.example.com/")
239+
.execute()
240+
.toCompletableFuture()
241+
.exceptionally(t->{ /* Something wrong happened... */ })
242+
.thenApply(response->{ /* Do something with the Response */ return resp;});
243+
whenResponse.join(); // wait for completion
240244
```
241245

242-
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)
246+
You may get the complete maven project for this simple demo
247+
from [org.asynchttpclient.example](https://github.com/AsyncHttpClient/async-http-client/tree/master/example/src/main/java/org/asynchttpclient/example)
243248

244249
## WebSocket
245250

246251
Async Http Client also supports WebSocket.
247252
You need to pass a `WebSocketUpgradeHandler` where you would register a `WebSocketListener`.
248253

249254
```java
250-
WebSocket websocket = c.prepareGet("ws://demos.kaazing.com/echo")
251-
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
252-
new WebSocketListener() {
253-
254-
@Override
255-
public void onOpen(WebSocket websocket) {
256-
websocket.sendTextFrame("...").sendTextFrame("...");
257-
}
258-
259-
@Override
260-
public void onClose(WebSocket websocket) {
261-
}
262-
263-
@Override
264-
public void onTextFrame(String payload, boolean finalFragment, int rsv) {
265-
System.out.println(payload);
266-
}
267-
268-
@Override
269-
public void onError(Throwable t) {
270-
}
271-
}).build()).get();
255+
WebSocket websocket=c.prepareGet("ws://demos.kaazing.com/echo")
256+
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
257+
new WebSocketListener(){
258+
259+
@Override
260+
public void onOpen(WebSocket websocket){
261+
websocket.sendTextFrame("...").sendTextFrame("...");
262+
}
263+
264+
@Override
265+
public void onClose(WebSocket websocket){
266+
}
267+
268+
@Override
269+
public void onTextFrame(String payload,boolean finalFragment,int rsv){
270+
System.out.println(payload);
271+
}
272+
273+
@Override
274+
public void onError(Throwable t){
275+
}
276+
}).build()).get();
272277
```
273278

274279
## Reactive Streams
@@ -287,21 +292,22 @@ AsyncHttpClient has build in support for the WebDAV protocol.
287292
The API can be used the same way normal HTTP request are made:
288293

289294
```java
290-
Request mkcolRequest = new RequestBuilder("MKCOL").setUrl("http://host:port/folder1").build();
291-
Response response = c.executeRequest(mkcolRequest).get();
295+
Request mkcolRequest=new RequestBuilder("MKCOL").setUrl("http://host:port/folder1").build();
296+
Response response=c.executeRequest(mkcolRequest).get();
292297
```
298+
293299
or
294300

295301
```java
296-
Request propFindRequest = new RequestBuilder("PROPFIND").setUrl("http://host:port").build();
297-
Response response = c.executeRequest(propFindRequest, new AsyncHandler() {
298-
// ...
299-
}).get();
302+
Request propFindRequest=new RequestBuilder("PROPFIND").setUrl("http://host:port").build();
303+
Response response=c.executeRequest(propFindRequest,new AsyncHandler(){
304+
// ...
305+
}).get();
300306
```
301307

302308
## More
303309

304-
You can find more information on Jean-François Arcand's blog. Jean-François is the original author of this library.
310+
You can find more information on Jean-François Arcand's blog. Jean-François is the original author of this library.
305311
Code is sometimes not up-to-date but gives a pretty good idea of advanced features.
306312

307313
* http://web.archive.org/web/20111224171448/http://jfarcand.wordpress.com/2011/01/12/going-asynchronous-using-asynchttpclient-for-dummies/
@@ -324,5 +330,5 @@ Here are the few rules we'd like you to respect if you do so:
324330
* Only edit the code related to the suggested change, so DON'T automatically format the classes you've edited.
325331
* Use IntelliJ default formatting rules.
326332
* Regarding licensing:
327-
* You must be the original author of the code you suggest.
328-
* You must give the copyright to "the AsyncHttpClient Project"
333+
* You must be the original author of the code you suggest.
334+
* You must give the copyright to "the AsyncHttpClient Project"

0 commit comments

Comments
 (0)