Kotlin + Akka Part 1: Hello Kotlin!

Blockchain.com Engineering
Blockchain.com Engineering
2 min readApr 4, 2018

One of the many projects currently underway at Blockchain involves building a real-time platform that runs 24/7, capable of processing thousands of messages a second running, and with an API scaling to millions of requests. There are certainly many ways to approach this problem, but the underlying framework chosen for the task was Akka.

This is the first of four blogs about building an Akka system with Kotlin. To make the most of the following information, it would be best for the reader to have some basic experience using Kotlin, but no prior use of Akka is required. These blog posts will not exhaustively explore all concepts of Akka, but rather provide enough details to get the reader quickly up to speed on a surface level with Akka.

Akka is an implementation of the Actor Model which allows us to build scalable message driven systems and comes with a huge set of tools and extensions. Rather than repeat what you can read directly from the source, please visit Akka.

All the source code you find from this blog is available here.

Let’s create our first ‘Hello Kotlin’ actor:

fun main(args: Array<String>) {class HelloKotlinActor : AbstractLoggingActor() {override fun createReceive() = ReceiveBuilder().match(String::class.java) { log().info(it) }.build()}val actorSystem = ActorSystem.create("part1")val actorRef = actorSystem.actorOf(Props.create(HelloKotlinActor::class.java))actorSystem.log().info("Sending Hello Kotlin")actorRef.tell("Hello Kotlin", ActorRef.noSender())}

… and break down what exactly is going on here:

  1. HelloKotlinActor class is configured to log any messages received of the type String using the ReceiveBuilder
  2. ActorSystem.create(“kotlin-akka”) creates the Akka System
  3. The Props.create(…) can be seen as a type of metadata describing how to instantiate our class HelloKotlinActor.
  4. actorSystem.actorOf(…) creates an Actor ‘container’ for this metadata which is configured with mailbox; the ‘container’ uses the Props to create an instance of HelloKotlinActor which will then consume any messages put in its mailbox.
  5. actorRef.tell(“Hello Kotlin”, ActorRef.noSender()) sends a String message to the actor ‘container’ which is added the to mailbox.

Running the above code will startup the actor system and print out the following:

[INFO] [03/04/2018 11:12:03.843] [main] [akka.actor.ActorSystemImpl(part1)] Sending Hello Kotlin[INFO] [03/04/2018 11:12:03.850] [part1-akka.actor.default-dispatcher-2] [akka://part1/user/$a] Hello Kotlin

There are a couple of things to note:

  1. The log messages are coming from separate threads. The ‘dispatcher’ thread delivers the message receive on the mailbox to the HelloKotlinActor created. The ‘dispatcher’ is a core concept to Akka and will be discussed in the coming weeks.
  2. The actor path is the unique address of the actor within the Akka system. The ‘$a’ is an internal name Akka assigned to our Actor, since we did not provide a name explicitly. For example, we could have actorSystem.actorOf(Props.create(HelloKotlinActor::class.java),”HelloKotlin”)

Although a trivial example, this demonstrates the basics of creating an actor putting a message in its mailbox. In our next post, we will briefly discuss actor hierarchies.

--

--

Blockchain.com Engineering
Blockchain.com Engineering

The Blockchain.com engineering team is building an open, accessible, and fair financial future, one piece of software at a time. Learn more at blockchain.com