Spray with Akka Starter Kit

Over the last few months, Spray is fast becoming the de-facto for all the products that we are working on. Irrespective of whether it is a product which has a full featured UI or a gaming component which needs to provide services for the game to execute in a performant way, Spray has found its use.

The best part about Spray being that is very performant and endorses the asynchronous, non blocking style that we are used to with Akka. It satisfies the HTTP integration needs of any product. More information on this webinar.

The starter kit which is the main part of this post, is a template which allows you to accept incoming REST requests, route them to the corresponding Akka actor which provides a response and then carrying back this response to the actual user in a non-blocking, performant way. Note that the example is very simple but it attempts to give you a starting point on which you can build further.

The main characters of this example are
1. The HUB which is responsible for communication to and from all modules.
2. Modules which would interact with the HUB
3. Client call which would call a REST service on the HUB and await a response.

Let us see how the interactions happen. For simplicity, we would assume that the client is making a GET call on the browser with something like

http://localhost:8080/module/BANG

Now the call lands up on the HUB which has exposed a REST service. The HTTP server on Spray is started like this

Ofcouse you would be externalizing a few things like host and port. I am being lazy 😉

If you notice, we are binding to the service called hubservices. This what wraps over our routes.

and the hubRoutes are defined as

Now the fun part, we are accepting 2 kinds of requests as per the routes defined. One is a GET request of the kind /module/XXX when XXX could be anything. Another one is a POST request where we call /module and pass some data to be processed.

The interesting thing that the XXX gets passed to the module, in our case let us consider that to tbe the HRService as a Future call. You can read about Futures on the Knoldus blog here.

We are handling the Future as a succcess or failure as

Let us take a quick look at the HRService as well

So keeping it simple, the HumanResourcesService reacts when it gets the BANG message. At other time it spits out the “Bad Message” string.

The point to note here is that our REST Api is not blocked when we are asking the HumanResourcesService to work for us. REST service spins up a future and waits for the future to either succeed or fail.

For testing, we are using the spray.testkit.Specs2RouteTest which makes testing spring so much easier.

Hence, you would notice that for GET request, in /module/BANG we get back a Hello response. For anything else, we get back a “Bad Message” response. For the post call, which we have not implemented right now, we get back the “Currently I cannot process data with” response.

The entire starter kit is present on the Knoldus github account. You would like to add test cases for your real world Akka actor testing. More details here.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

1 thought on “Spray with Akka Starter Kit

Leave a Reply

%d bloggers like this: