Async HTTP Client and Akka

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 and ignore this post. For regular Java the Async HTTP Client is still a great choise

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.

HTTP over tincan-phones, hmm, why not IETF?

HTTP over tincan-phones, hmm, why not IETF?

The wonderful Async Http Client provides exactly this. It makes it very easy to do asynchronous HTTP and HTTPS requests. The API is easy to use and let’s you do what you need. I recommend it.

Async HTTP in Akka

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 future. And the client is a simple Akka extension. So you can do a simple request like this:

// Our actor system
var config = ConfigFactory.load()
val actorSystem = ActorSystem("myapp", config.getConfig("myapp").withFallback(config))
// build up a new request
val responseFromBlog = WebClient(actorSystem).prepareGet("http://www.gamlor.info")
// add headers, body etc
.addHeader("Accept-Charset", "utf-8")
.addHeader("Accept-Language", "en-US")
.execute()
// do something with the result
responseFromBlog.onSuccess {
case response: Response => {
if(response.getStatusCode==200){
println(response.getStatusCode)
} else{
println("Oh boy"+response.getStatusCode)
}
}
}.onFailure {
case ex: Exception => {
ex.printStackTrace()
}
}

Otherwise API is more or less the same or exactly the same as in raw library.

Configuration

The wrapper also reads the configuration from the regular Akka configuration of the given Actor system. For example when you want to enable redirects:

myapp{
# Web client configuration for our app
webclient{
redirectsEnabled = true
maxRedirects = 3
}
}

Here’s the default reference configuration:

# Defaults for the web client
webclient{
maxTotalConnections = -1
maxConnectionsPerHost = -1
connectionTimeoutInMS = 60s
websocketTimoutInMS = 15m
idleConnectionInPoolTimeoutInMS = 60s
idleConnectionTimeoutInMS = 60s
requestTimeoutInMS = 60s
redirectsEnabled = false
maxRedirects = 5
compressionEnabled = false
userAgent = "NING/1.0"
useProxyProperties = false
}

Get The Stuff

The tiny ‘wrapper’ is on my github repo here: https://github.com/gamlerhart/akka-async-apis. Also the stuff is on my github hosted Maven Snapshot repository. You can grab it via SBT or Maven:

Repository for Maven: https://github.com/gamlerhart/gamlor-mvn/raw/master/snapshots
GroupID: info.gamlor.akkaasync
ArtifactID: akka-webclient_2.9.1
Version: 1.0-SNAPSHOT

So via SBT:

resolvers += "Gamlor-Repo" at "https://github.com/gamlerhart/gamlor-mvn/raw/master/snapshots"
libraryDependencies += "com.typesafe.akka" % "akka-actor" % "2.0"
libraryDependencies += "info.gamlor.akkaasync" %% "akka-webclient" % "1.0-SNAPSHOT"
view raw SBT.sbt hosted with ❤ by GitHub

That’s it.

Improvements will follow when I see a need. Or just use the raw Async HTTP library.

Tagged on: ,