1. Wri$ng
Powerful
HTTP
Apps
Using
the
New
Asynchronous
HTTP
Client
Library
h@p://github.com/AsyncH@pClient/async-‐h@p-‐client
Jeanfrancois
Arcand
Hubert
Iwaniuk
h@p://twi@er.com/jfarcand
h@p://twi@er.com/neotyk
2. Creator
of
Creator
of
the
Grizzly
GlassFish
v3
Creator
and
Co-‐ Framework
Grizzly
Micro
Leader
of
Kernel
Atmosphere
Jeanfrancois
9
years
@
Sun
Creator
Async
Microsystem
H@p
Client
Major
contributor
to
GlassFish
3. • News
and
Update
• Why
• The
Async
H@p
Client
API
• Conclusion
• Q
&
A
4. Since
released,
we’ve
got
an
impressive
amount
of
contribu$ons
and
adop$ons.
Library
is
evolving
fast!
6. JDK’s
Apache
UrlConnec$on
H@pClient
high
Totally
Broken
on
steroid
Why
7. JDK’s
Apache
UrlConnec$on
H@pClient
high
Totally
Broken
on
steroid
Why
Apache
H@p
is
blocking
8. JDK’s
Apache
UrlConnec$on
H@pClient
high
Totally
Broken
on
steroid
Why
Apache
H@p
is
blocking
Apache
H@pClient
buffer
the
bytes
in
memory
9. JDK’s
Apache
UrlConnec$on
H@pClient
high
Totally
Broken
on
steroid
Why
Apache
H@p
is
Apache
H@pClient
blocking
is
$ed
to
its
own
I/O
layer.
Apache
H@pClient
buffer
the
bytes
in
memory
11. AsyncH@pClient
Large
Upload
supports
the
using
na$ve
memory
“zero-‐copy”
Small
memory
Why
12. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Why
13. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Ne@y
JDK
UrlConnec
$on
Why
Apache
H@pClient
Grizzly
14. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Why
AsyncH@pClient
is
blocking
or
non
blocking
15. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Why
AsyncH@pClient
is
blocking
or
non
blocking
Use
the
well
Allow
efficient
know
streaming
Future<?>
API
16. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Why
AsyncH@pClient
is
blocking
or
non
blocking
AsyncH@pClient
doesn’t
buffer
the
bytes
in
memory
17. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Why
AsyncH@pClient
is
blocking
or
non
blocking
Your
applica$on
have
control
AsyncH@pClient
doesn’t
buffer
the
bytes
in
memory
18. AsyncH@pClient
AsyncH@pClient
supports
the
is
not
$ed
to
it’s
“zero-‐copy”
I/O
layer
Why
AsyncH@pClient
is
blocking
or
AsyncH@pClient
is
non
blocking
easy
to
use
AsyncH@pClient
doesn’t
buffer
the
bytes
in
memory
19. Fully
Asynchronous
Calling
Thread
Server
Thread
Pool
(*)
request
Response
chunk
callback
Response
chunk
callback
20. Comple$onHandler
Calling
Thread
Server
Thread
Pool
(*)
request
Response
start
Response
end
callback
21. Future
based
API
AsyncH@pClient
asyncH@pClient
=
new
AsyncH@pClient();
Future<Response>
f
=
asyncH@pClient.prepareGet("h@p://www.sonatype.com/").execute();
Response
r
=
f.get();
22. Handler
based
API
AsyncH@pClient
asyncH@pClient
=
new
AsyncH@pClient();
Future<Response>
asyncH@pClient.
prepareGet("h@p://www.sonatype.com/").execute(
new
AsyncComple=onHandler<Response>(
{
@Override
public
Response
onCompleted(Response
r){
return
response;
}
@Override
public
void
onThrowable(Throwable
t){
}
});
23. Typed
AsyncH@pClient
asyncH@pClient
=
new
AsyncH@pClient();
asyncH@pClient.prepareGet("h@p://www.sonatype.com/").execute(
new
AsyncComple$onHandler<Integer>(
{
@Override
public
Integer
onCompleted(Response
r){
return
response.getStatus();
}
@Override
public
void
onThrowable(Throwable
t){
}
}).get();
//
Return
Integer
25. Streaming
Handler
AsyncH@pClient
c
=
new
AsyncH@pClient();
Future<String>
f
=
c.prepareGet("h@p://www.sonatype.com/").execute(
new
AsyncHandler<String>()
{
@Override
public
STATE
onStatusReceived(H@pResponseStatus<String>
status)
throws
Excep$on
{
//
return
STATE.ABORT
to
stop
}
@Override
public
STATE
onHeadersReceived(H@pResponseHeaders<String>
headers)
throws
Excep$on
{
…}
@Override
public
STATE
onBodyPartReceived(H@pResponseBodyPart<String>
bodyPart)
throws
Excep$on
{…}
}.get();
26. Progress
Handler
Future<String>
f
=
h@pClient.preparePut(uri)
.setRealm(createRealm())
.setProxyServer(createProxyServer())
.setFile(file)
.execute(comple$onHandler);
("….").setFile(.execute(
new
AsyncComple=onHandler<String>()
{
@Override
public
STATE
onHeaderWriteCompleted()
{
return
STATE.CONTINUE;
}
@Override
public
STATE
onContentWriteCompleted()
{
return
STATE.CONTINUE;
}
@Override
public
STATE
onContentWriteProgess(long
amount,
long
current,
long
total)
{
return
STATE.CONTINUE;
}
)
27. Configurable
AsyncH@pClientConfig
c
=
new
AsyncH@pClientConfig.Builder().
setResponseTimeoutInMs(30000).build();
AsyncH@pClient
asyncH@pClient
=
new
AsyncH@pClient(c);
Realm
realm
=
new
Realm.RealmBuilder()
.setPrincipal(“me”)
.setPassword(“xxxx”).build();
c.setRealm(realm);
AsyncH@pClient
asyncH@pClient
=
new
AsyncH@pClient(c);
28. Zero
Copy
-‐
Upload
AsyncH@pClient
c=
new
AsyncH@pClient();
c.preparePost(“h@ps://….”)
.setBody(body)
.setFile(header)
.setProxy(proxy).execute();
29. Zero
Copy
-‐
Download
AsyncH@pClient
c
=
new
AsyncH@pClient();
Future<String>
f
=
c.prepareGet("h@p://www.sonatype.com/").execute(
new
AsyncHandler<String>()
{
@Override
public
STATE
onBodyPartReceived(H@pResponseBodyPart<String>
bodyPart)
throws
Excep$on
{
bodyPart.write(OutputStream);
}
}.get();
30. TLS
Support
//
Op$onal
AsyncH@pClientConfig
c
=
new
AsyncH@pClientConfig.Builder().
setSSLEngine(…).build();
AsyncH@pClient
asyncH@pClient
=
new
AsyncH@pClient(c);
Future<String>
f
=
c.prepareGet("hBps://….").get();