{"id":2484,"date":"2012-04-05T23:41:57","date_gmt":"2012-04-05T22:41:57","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=2484"},"modified":"2021-03-11T09:19:12","modified_gmt":"2021-03-11T08:19:12","slug":"async-http-client-and-akka","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2012\/04\/async-http-client-and-akka\/","title":{"rendered":"Async HTTP Client and Akka"},"content":{"rendered":"<p><strong><span style=\"color: #ff0000;\">UPDATE: <\/span>I just discovered \t<a href=\"https:\/\/github.com\/spray\/spray\/wiki\">Spray<\/a>, an asynchronous REST library. It also has a nice <a href=\"https:\/\/github.com\/spray\/spray\/wiki\/spray-client\">HTTP client<\/a> and is fully integrated to Akka. So I would recommend to to used that and ignore this post. For regular Java the Async HTTP Client is still a great choise<\/strong><\/p>\n<p>HTTP requests (and network requests in general) are extremely slow compared to other operations. So they can introduce a lot of latency in your system when you application is waiting for the response. This especially adds up when you fetch stuff from multiple servers. Therefore you often want to do such an operation asynchronously instead of blocking and waiting.<\/p>\n<div id=\"attachment_2487\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/04\/http-can-phone.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2487\" class=\"size-medium wp-image-2487\" title=\"http-can-phone\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/04\/http-can-phone-300x196.png\" alt=\"HTTP over tincan-phones, hmm, why not IETF?\" width=\"300\" height=\"196\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/04\/http-can-phone-300x196.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/04\/http-can-phone.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-2487\" class=\"wp-caption-text\">HTTP over tincan-phones, hmm, why not IETF?<\/p><\/div>\n<p>The wonderful <a href=\"https:\/\/github.com\/sonatype\/async-http-client\">Async Http Client<\/a> provides exactly this. It makes it very easy to do asynchronous HTTP and HTTPS requests. The API is easy to use and let\u2019s you do what you need. I recommend it.<\/p>\n<h2>Async HTTP in Akka<\/h2>\n<p>For the asynchronous world of Akka the Async HTTP API is a good fit. For some more convenience I wrapped the main API so that it integrates a little better with Akka. Mainly the execute operations now returns an Akka <a href=\"http:\/\/doc.akka.io\/docs\/akka\/2.0\/scala\/futures.html\">future<\/a>. And the client is a simple Akka extension. So you can do a simple request like this:<\/p>\n<script src=\"https:\/\/gist.github.com\/2314636.js?file=SimpleRequest.scala\"><\/script><noscript><pre><code class=\"language-scala scala\">\/\/ Our actor system\nvar config = ConfigFactory.load()\nval actorSystem = ActorSystem(&quot;myapp&quot;, config.getConfig(&quot;myapp&quot;).withFallback(config))\n\n\/\/ build up a new request\nval responseFromBlog = WebClient(actorSystem).prepareGet(&quot;http:\/\/www.gamlor.info&quot;)\n   \/\/ add headers, body etc\n  .addHeader(&quot;Accept-Charset&quot;, &quot;utf-8&quot;)\n  .addHeader(&quot;Accept-Language&quot;, &quot;en-US&quot;)\n  .execute()\n\n\/\/ do something with the result\nresponseFromBlog.onSuccess {\n  case response: Response =&gt; {\n\tif(response.getStatusCode==200){\n\t  println(response.getStatusCode)\n\t} else{\n\t  println(&quot;Oh boy&quot;+response.getStatusCode)\n\t}\n  }\n}.onFailure {\n  case ex: Exception =&gt; {\n\tex.printStackTrace()\n  }\n}<\/code><\/pre><\/noscript>\n<p>Otherwise API is more or less the same or exactly the same as in raw library.<\/p>\n<h2>Configuration<\/h2>\n<p>The wrapper also reads <a href=\"http:\/\/doc.akka.io\/docs\/akka\/2.0\/general\/configuration.html\">the configuration<\/a> from the regular Akka configuration of the given Actor system. For example when you want to enable redirects:<\/p>\n<script src=\"https:\/\/gist.github.com\/2314636.js?file=myapp.conf\"><\/script><noscript><pre><code class=\"language- \">myapp{\n     # Web client configuration for our app\n     webclient{\n         redirectsEnabled = true\n         maxRedirects = 3\n     }\n }<\/code><\/pre><\/noscript>\n<p>Here\u2019s the default reference configuration:<\/p>\n<script src=\"https:\/\/gist.github.com\/2314636.js?file=reference.conf\"><\/script><noscript><pre><code class=\"language- \"># Defaults for the web client\nwebclient{ \n   maxTotalConnections = -1\n   maxConnectionsPerHost = -1\n   connectionTimeoutInMS = 60s\n   websocketTimoutInMS = 15m\n   idleConnectionInPoolTimeoutInMS = 60s\n   idleConnectionTimeoutInMS = 60s\n   requestTimeoutInMS = 60s\n   redirectsEnabled = false\n   maxRedirects = 5\n   compressionEnabled = false\n   userAgent = &quot;NING\/1.0&quot;\n   useProxyProperties = false\n }<\/code><\/pre><\/noscript>\n<h2>Get The Stuff<\/h2>\n<p>The tiny \u2018wrapper\u2019 is on my github repo here: <a title=\"https:\/\/github.com\/gamlerhart\/akka-async-apis\" href=\"https:\/\/github.com\/gamlerhart\/akka-async-apis\">https:\/\/github.com\/gamlerhart\/akka-async-apis<\/a>. Also the stuff is on my github hosted Maven Snapshot repository. You can grab it via SBT or Maven:<\/p>\n<p>Repository for Maven: <a href=\"https:\/\/github.com\/gamlerhart\/gamlor-mvn\/raw\/master\/snapshots\">https:\/\/github.com\/gamlerhart\/gamlor-mvn\/raw\/master\/snapshots<\/a><br \/>\nGroupID: info.gamlor.akkaasync<br \/>\nArtifactID: akka-webclient_2.9.1<br \/>\nVersion: 1.0-SNAPSHOT<\/p>\n<p>So via SBT:<\/p>\n<script src=\"https:\/\/gist.github.com\/2314636.js?file=SBT.sbt\"><\/script><noscript><pre><code class=\"language-scala scala\">resolvers += &quot;Gamlor-Repo&quot; at &quot;https:\/\/github.com\/gamlerhart\/gamlor-mvn\/raw\/master\/snapshots&quot;\n\nlibraryDependencies += &quot;com.typesafe.akka&quot; % &quot;akka-actor&quot; % &quot;2.0&quot;\nlibraryDependencies += &quot;info.gamlor.akkaasync&quot;  %% &quot;akka-webclient&quot; % &quot;1.0-SNAPSHOT&quot;<\/code><\/pre><\/noscript>\n<h2>That\u2019s it.<\/h2>\n<p>Improvements will follow when I see a need. Or just use the raw Async HTTP library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: I just discovered Spray, an asynchronous REST library. It also has a nice HTTP client and is fully integrated to Akka. So I would recommend to to used that&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[243,15],"tags":[245,264],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2484"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2484"}],"version-history":[{"count":8,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2484\/revisions"}],"predecessor-version":[{"id":3757,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2484\/revisions\/3757"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}