Over a million developers have joined DZone.

For Java Developers: Akka HTTP In the Cloud With Maven and Docker

Learn how Akka can make your software more resilient with a common, easy Maven and Docker workflow.

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

From time to time it is necessary to just take the little effort to think outside the box. This is a good habit for every developer and even if you just spend 10% of your time, with new and noteworthy technology, you will gain experience and broaden your knowledge. I wanted to look into Scala and Akka since a while. Both well known old acquaintances on many conference agendas. But honestly, I've never felt the need to take a second look. This changed quite a bit when I started to look deeper into microservices and relevant concepts around it. Let's get started and see what's in there.

What Is Akka? And Why Scala?

But first some sentences about what Akka is. The name AKKA is the a palindrome of letters A and K as in Actor Kernel.

"Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM."

It was built with the idea in mind to make writing concurrent, fault-tolerant and scalable applications easier. With the help of the so called "Actor" model the abstraction level for those applications has been re-defined and by adopting the "let it crash" concept you can build applications that self-heal and systems that stand very high workloads.  Akka is Open Source, available under the Apache 2 License and can be downloaded from http://akka.io/downloads. Learn more about it in the official Akka documentation. Akka comes in two flavors: With a Java and a Scala API. So, you're basically free to choose which version you want to use in your projects. I went for Scala in this blog post  because I couldn't find enough Akka Java examples out there.

Why Should A Java Developer Care?

I don't know a lot about you, but I was just curious about it and started to browse the documentation a bit. The "Obligatory Hello World" didn't lead me anywhere. Maybe because I was (am) still thinking too much in Maven and Java but we're here to open our minds a bit so it was about time to change that. Resilient and message driven systems seem to be the most promising way of designing microservices based applications. And even if there are things like Vert.x which are a lot more accessible for Java developers it never is bad to look into something new. Because I didn't get anywhere close to where I wanted to be with the documentation, I gave Konrad `@ktosopl` Malawsk a ping and asked for help. He came up with a nice little Akka-HTTP example for me to take apart and learn. Thanks for your help!

Akka, Scala and now Akka-HTTP?

Another new name. The Akka HTTP modules implement a full server- and client-side HTTP stack on top of akka-actor and akka-stream. It's not a web-framework but rather a more general toolkit for providing and consuming HTTP-based services. And this is what I wanted to take a look at. Sick of reading? Let's get started:

Clone And Compile - A Smoke-Test

Git clone Konrad's example to a folder of choice and start to compile and run it:

git clone https://github.com/ktoso/example-akka-http.git
mvn exec:java

Congratulations. Now let's look at the code.

The Example Code

Looking at the pom.xml and the exec-maven-plugin configuration points us to the com.example.ExampleServer.scala class. It extends the ExampleRoutes.scala and obviously has some routes defined. Not surprisingly those map to the links you can use from the index page. It kinds of makes sense, even if you don't understand Scala. For the Java developers among us, Konrad was kind enough to add a Java Akka example (JavaExampleServer.java). If I compare both of them, I still like the Java example a lot better, but it is also probably also a little longer. Just choose what you like best.

There's one very cool thing in the example that you might want to check out. The line is emitting an Reactive Streams source of data which is pushed exactly as fast as the client can consume it, and it is only generated "on demand". Compare http://www.reactive-streams.org/ for more details.

The main advantage of the example is obviously that it provides a complete Maven based build for both languages and can be easily used to learn a lot more about Akka. A good jumping off point. And because there is not a lot more in this example from a feature perspective let's see if we can get this to run in the cloud.

Deploying Akka - In A Container

According to the documentation there are three different ways of deploying Akka applications:

  • As a library: used as a regular JAR on the classpath and/or in a web app, to be put into WEB-INF/lib
  • Package with sbt-native-packager which is able to build *.deb, *.rpm or docker images which are prepared to run your app.
  • Package and deploy using Typesafe ConductR.

I don't know anything about sbt and ConductR  so, I thought I just go with what I was playing around lately anyway: In a container. If it runs from a Maven build, I can easily package it as an image. Let's go.The first step is to add the Maven Shade Plugin to the pom.xml:

               <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
               <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

The shade plugin creates an Uber jar, which is exactly, what I want to have in this case. There are three little special cases in the configuration. We need to:

  1. attach the shaded artifact to access it from the Docker Maven Plugin
  2. use the AppendingTransformer, because the configuration file is concatenated and not a "pick one" thing during the build process.
  3. define the main class, that we want to run.

When this is done, it is about time to configure our Docker Maven Plugin accordingly:


Couple of things to notice:

  • the output file mapping, which needs to be the same in the entrypoint argument. 
  • the project attachment include "allinone" which we defined in the Maven shade plugin.
  • the user in the image assembly (needs to be one that has been defined in the base image. In this case jboss/base-jdk which only knows the user jboss.)

And while we're on it, we need to tweak the example a bit. The binding to localhost for the Akka-Http server is not really helpful on a containerized environment. So, we use the java.net library to find out about the actual IP of the container. And while we're at it: Comment out the post startup procedure. The new ExampleServer looks like this:

package com.example

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import java.net._

object ExampleServer extends ExampleRoutes {
  implicit val system = ActorSystem("ExampleServer")
  import system.dispatcher
  implicit val materializer = ActorMaterializer()
  // settings about bind host/port
  // could be read from application.conf (via system.settings):
  val localhost = InetAddress.getLocalHost
  val interface = localhost.getHostAddress
  val port = 8080

  def main(args: Array[String]): Unit = {
    // Start the Akka HTTP server!
   // Using the mixed-in testRoutes (we could mix in more routes here)
    val bindingFuture = Http().bindAndHandle(testRoutes, interface, port)
    println(s"Server online at http://$interface:$port/\nON Docker...")

Let's build the Akka Application and the Dockerfile by executing:

mvn package docker:build

and give it a test-run:

docker run myfear/akka-sample

Redirecting your browser to (Note: IP and port will be different in your environment. Make sure to list the container port mapping with docker ps and get the ip of your boot2docker or docker-machine instance) will show your the working example again.

Some Final Thoughts

The next step in taking this little Akka example to the cloud would be to deploy it on a PaaS. Take OpenShift for example (compare this post on how to do it). As a fat-jar application, it can be easily packaged into an immutable container. I'm not going to compare Akka with anything else in the Java space but wanted to give you a starting point for your own first steps and encourage you always stay curious and educate yourself about some of the technologies out there.

Further Readings and Information:

The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

akka ,java ,http ,maven ,docker

Published at DZone with permission of Markus Eisele, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}