SlideShare a Scribd company logo
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	
  
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	
  
•  News	
  and	
  Update	
  
•  Why	
  	
  
•  The	
  Async	
  H@p	
  Client	
  API	
  
•  Conclusion	
  	
  
•  Q	
  &	
  A	
  
Since	
  released,	
  we’ve	
  got	
  an	
  
    impressive	
  amount	
  of	
  
contribu$ons	
  and	
  adop$ons.	
  
   Library	
  is	
  evolving	
  fast!	
  
JDK’s	
  
UrlConnec$on	
  
Totally	
  Broken	
  



                        Why	
  
JDK’s	
                          Apache	
  
UrlConnec$on	
                    H@pClient	
  high	
  
Totally	
  Broken	
                 on	
  steroid	
  



                        Why	
  
JDK’s	
                           Apache	
  
 UrlConnec$on	
                     H@pClient	
  high	
  
 Totally	
  Broken	
                  on	
  steroid	
  



                          Why	
  
Apache	
  H@p	
  is	
  
  blocking	
  
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	
  
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	
  
AsyncH@pClient	
  
 supports	
  the	
  
  “zero-­‐copy”	
  	
  



                          Why	
  
AsyncH@pClient	
            Large	
  Upload	
  
    supports	
  the	
           using	
  na$ve	
  
                                 memory	
  
     “zero-­‐copy”	
  	
  
 Small	
  
memory	
  	
  



                             Why	
  
AsyncH@pClient	
               AsyncH@pClient	
  
 supports	
  the	
             is	
  not	
  $ed	
  to	
  it’s	
  
  “zero-­‐copy”	
  	
                 I/O	
  layer	
  	
  



                          Why	
  
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	
  
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	
                                     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	
  
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	
  
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	
  
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	
  
Fully	
  Asynchronous	
  
 Calling	
  Thread	
                    Server	
  Thread	
  Pool	
  (*)	
  


                         request	
  


                                                  Response	
  chunk	
  
                         callback	
  



                                                   Response	
  chunk	
  
                         callback	
  
Comple$onHandler	
  
 Calling	
  Thread	
                    Server	
  Thread	
  Pool	
  (*)	
  


                         request	
  


                                                  Response	
  start	
  




                                                   Response	
  end	
  
                         callback	
  
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();	
  	
  
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){	
  	
  

                                               	
  	
  	
  	
  	
  	
  	
  }	
  	
  
	
                                 });	
  
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	
  
Request	
  Builder	
  

AsyncH@pClient	
  c=	
  new	
  AsyncH@pClient();	
  

c.preparePost(“h@ps://….”)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setBody(body)	
  
	
   	
   	
   	
  .setHeader(header)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setProxy(proxy)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .execute();	
  
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();	
  
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;	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  
)	
  
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);	
  
Zero	
  Copy	
  -­‐	
  Upload	
  

AsyncH@pClient	
  c=	
  new	
  AsyncH@pClient();	
  

c.preparePost(“h@ps://….”)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setBody(body)	
  
	
   	
   	
  	
  	
  .setFile(header)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setProxy(proxy).execute();	
  
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();	
  
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();	
  
Scrip$ng	
  Support	
  -­‐	
  Clojure	
  
•  (ns	
  twi@er-­‐stream	
  
   	
  	
  (:require	
  [h@p.async.client	
  :as	
  c]	
  
   	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [org.danlarkin.json	
  :as	
  j]	
  
   	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [clojure.contrib.logging	
  :as	
  l]))	
  
       (doseq	
  [twit-­‐str	
  (c/string	
  
       	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  (c/stream-­‐seq	
  :get	
  "h@p://stream.twi@er.com/1/statuses/
       sample.json"	
  
       	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  :auth	
  {:user	
  "u"	
  :password	
  "p"}	
  
       	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  :$meout	
  -­‐1))]	
  
       	
  	
  (try	
  
       	
  	
  	
  	
  (let	
  [twit	
  ( j/decode-­‐from-­‐str	
  twit-­‐str)]	
  
       	
  	
  	
  	
  	
  	
  (l/info	
  (str	
  (get-­‐in	
  twit	
  [:user	
  :screen_name])	
  "=>"	
  (:text	
  twit))))	
  
       	
  	
  	
  	
  (catch	
  Excep$on	
  e	
  
       	
  	
  	
  	
  	
  	
  (l/warn	
  (str	
  "Failed	
  to	
  parse:	
  "	
  twit-­‐str)	
  e))))	
  
Scrip$ng	
  Support	
  -­‐	
  JRuby	
  
client	
  =	
  AsyncH@pClient.new	
  	
  
future	
  =	
  client.prepare_get("h@p://…")	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  .set_header("X-­‐Trace",	
  "true").execute	
  
	
  	
  	
  	
  response	
  =	
  future.get	
  
	
  	
  	
  	
  	
  	
  	
  	
  response.headers.each	
  
	
  	
  	
  	
  	
  {	
  |pair|	
  puts	
  "#{pair.key}	
  =>	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #{pair.value.join(",	
  ")}"	
  }	
  client.close	
  	
  
Scrip$ng	
  Support	
  -­‐	
  Jython	
  
	
  	
  	
  	
  client	
  =	
  AsyncH@pClient()	
  future	
  =	
  
               client.prepareGet("h@p://www.google.com/")	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setHeaders({	
  "User-­‐Agent"	
  :	
  [	
  "Async	
  HTTP	
  
               Client/1.0.0"	
  ]})	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .execute()	
  	
  
response	
  =	
  future.get()	
  for	
  entry	
  in	
  
               response.headers:	
  print	
  '%s	
  =>	
  %s'	
  %	
  (entry.key,	
  
               ",	
  ".join(entry.value))	
  client.close()	
  	
  
What’s	
  Next	
  
•  NTLM	
  support	
  
•  Websocket	
  support	
  
•  New	
  Providers:	
  
   – Grizzly	
  2.0	
  
   – Apache	
  H@pClient	
  
•  H@p	
  Pipelining	
  
•  Annota$on	
  based	
  support.	
  
Fancy	
  
@BaseURL("h@p://localhost:12345")	
  	
  
public	
  interface	
  FooClient	
  	
  
{	
  	
  
	
        @GET("/")	
  	
  
	
        public	
  Future<Response>	
  getRoot();	
  

	
  	
     @GET("/")	
  	
  
	
         public	
  Future<String>	
  getRootAsString();	
  

	
        @GET("/")	
  	
  
	
        public	
  Future<String>	
  
	
  	
    	
     getRootWithParam(@QueryParam("name")	
  String	
  name);	
  	
  
}	
  	
  
FancyClientBuilder	
  builder	
  =	
  new	
  FancyClientBuilder(asyncClient);	
  FooClient	
  
client	
  =	
  builder.build(FooClient.class);	
  	
  

More Related Content

Similar to Async Http Client for Java and Scripting Language (20)

PPTX
Everything you wanted to know about writing async, concurrent http apps in java
Baruch Sadogursky
 
PPTX
Think async
Bhakti Mehta
 
PDF
Tomcatx performance-tuning
Vladimir Khokhryakov
 
PPTX
Asif
Shaik Asif
 
PPTX
Aumentando a eficiência do Web Container usando chamadas Assíncronas
Rafael T. C. Soares (tuelho)
 
PDF
Creating an Arduino Web Server from scratch hardware and software
Justin Mclean
 
PDF
Indroduction to Web Application
torny10
 
PDF
HTTP / 1, HTTP / 2 and HTTP / 3: Past, present and the future of APIs
Roan Brasil Monteiro
 
PDF
Netty: asynchronous data transfer
Victor Cherkassky
 
KEY
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Tatsuhiko Miyagawa
 
PDF
Operating Systems - Concurrency
Emery Berger
 
PPTX
Web technologies: HTTP
Piero Fraternali
 
PDF
RESTEasy
Massimiliano Dessì
 
KEY
Java web programming
Ching Yi Chan
 
PDF
The 1990s Called. They Want Their Code Back.
Jonathan Oliver
 
PPTX
HTTP2 and gRPC
Guo Jing
 
PDF
May 2010 - RestEasy
JBug Italy
 
ZIP
Performance and Fault Tolerance for the Netflix API
Ben Christensen
 
PDF
Httpcore tutorial
Aravindharamanan S
 
KEY
Pushing the web — WebSockets
Roland M
 
Everything you wanted to know about writing async, concurrent http apps in java
Baruch Sadogursky
 
Think async
Bhakti Mehta
 
Tomcatx performance-tuning
Vladimir Khokhryakov
 
Aumentando a eficiência do Web Container usando chamadas Assíncronas
Rafael T. C. Soares (tuelho)
 
Creating an Arduino Web Server from scratch hardware and software
Justin Mclean
 
Indroduction to Web Application
torny10
 
HTTP / 1, HTTP / 2 and HTTP / 3: Past, present and the future of APIs
Roan Brasil Monteiro
 
Netty: asynchronous data transfer
Victor Cherkassky
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Tatsuhiko Miyagawa
 
Operating Systems - Concurrency
Emery Berger
 
Web technologies: HTTP
Piero Fraternali
 
Java web programming
Ching Yi Chan
 
The 1990s Called. They Want Their Code Back.
Jonathan Oliver
 
HTTP2 and gRPC
Guo Jing
 
May 2010 - RestEasy
JBug Italy
 
Performance and Fault Tolerance for the Netflix API
Ben Christensen
 
Httpcore tutorial
Aravindharamanan S
 
Pushing the web — WebSockets
Roland M
 

More from jfarcand (7)

PPTX
Websockets on the JVM: Atmosphere to the rescue!
jfarcand
 
PPTX
Building WebSocket and Server Side Events Applications using Atmosphere
jfarcand
 
PPTX
Writing Portable WebSockets in Java
jfarcand
 
PPTX
Introduction au WebSockets via le projet Atmosphere
jfarcand
 
PPT
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
jfarcand
 
PDF
Servlet Async I/O Proposal (NIO.2)
jfarcand
 
PPTX
The Atmosphere Framework
jfarcand
 
Websockets on the JVM: Atmosphere to the rescue!
jfarcand
 
Building WebSocket and Server Side Events Applications using Atmosphere
jfarcand
 
Writing Portable WebSockets in Java
jfarcand
 
Introduction au WebSockets via le projet Atmosphere
jfarcand
 
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
jfarcand
 
Servlet Async I/O Proposal (NIO.2)
jfarcand
 
The Atmosphere Framework
jfarcand
 
Ad

Recently uploaded (20)

PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Digital Circuits, important subject in CS
contactparinay1
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Ad

Async Http Client for Java and Scripting Language

  • 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://[email protected]/jfarcand   h@p://[email protected]/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  
  • 10. AsyncH@pClient   supports  the   “zero-­‐copy”     Why  
  • 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  =            [email protected]("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();   [email protected]("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  
  • 24. Request  Builder   AsyncH@pClient  c=  new  AsyncH@pClient();   c.preparePost(“h@ps://….”)                                  .setBody(body)          .setHeader(header)                                  .setProxy(proxy)                                  .execute();  
  • 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  =    [email protected](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();  
  • 31. Scrip$ng  Support  -­‐  Clojure   •  (ns  twi@er-­‐stream      (:require  [[email protected]  :as  c]                          [org.danlarkin.json  :as  j]                          [clojure.contrib.logging  :as  l]))   (doseq  [twit-­‐str  (c/string                                      (c/stream-­‐seq  :get  "h@p://[email protected]/1/statuses/ sample.json"                                                                  :auth  {:user  "u"  :password  "p"}                                                                  :$meout  -­‐1))]      (try          (let  [twit  ( j/decode-­‐from-­‐str  twit-­‐str)]              (l/info  (str  (get-­‐in  twit  [:user  :screen_name])  "=>"  (:text  twit))))          (catch  Excep$on  e              (l/warn  (str  "Failed  to  parse:  "  twit-­‐str)  e))))  
  • 32. Scrip$ng  Support  -­‐  JRuby   client  =  [email protected]     future  =  client.prepare_get("h@p://…")                    .set_header("X-­‐Trace",  "true").execute          response  =  future.get                  response.headers.each            {  |pair|  puts  "#{pair.key}  =>                                          #{pair.value.join(",  ")}"  }  client.close    
  • 33. Scrip$ng  Support  -­‐  Jython          client  =  AsyncH@pClient()  future  =   client.prepareGet("h@p://www.google.com/")                          .setHeaders({  "User-­‐Agent"  :  [  "Async  HTTP   Client/1.0.0"  ]})                          .execute()     response  =  future.get()  for  entry  in   response.headers:  print  '%s  =>  %s'  %  (entry.key,   ",  ".join(entry.value))  client.close()    
  • 34. What’s  Next   •  NTLM  support   •  Websocket  support   •  New  Providers:   – Grizzly  2.0   – Apache  H@pClient   •  H@p  Pipelining   •  Annota$on  based  support.  
  • 35. Fancy   @BaseURL("h@p://localhost:12345")     public  interface  FooClient     {       @GET("/")       public  Future<Response>  getRoot();       @GET("/")       public  Future<String>  getRootAsString();     @GET("/")       public  Future<String>         getRootWithParam(@QueryParam("name")  String  name);     }     FancyClientBuilder  builder  =  new  FancyClientBuilder(asyncClient);  FooClient   client  =  builder.build(FooClient.class);