Let’s create the most well known application for us, the HELLO WORLD APP

In order to have the required actor of an AKKA application, we will need:

  • A message
  • An actor

A message

This is quite simple, we only need one line of code:

case class Task(taskName: String)

An actor

Don’t be afraid, this is quite simple too. You just have to inherit from akka.actor.Actor and provide an implementation for the receive method. This method will be executed when the actor receives the message.

class Employee extends Actor {
override def receive: Receive = {
case Task(taskName) => println(s"I'm working on $taskName")
}
}

For finish, we only need to send the message to the Actor. Let’s say that someone has sent me a message to work on the Hello World app, the app should looks like:

val system = ActorSystem("HelloWorld-App")
val developer = system.actorOf(Props[Employee], "Rene")
// Send the task to the developer
developer ! Task("Write an AKKA Hello World app")

I love Scala because it help us to keep the things really simple, the whole application can reside in just one file:

import akka.actor.{Actor, ActorSystem, Props}

// A message with what to do
case class Task(taskName: String)

// Who will do
class Employee extends Actor {
override def receive: Receive = {
case Task(taskName) => println(s"I'm working on $taskName")
}
}


object HelloWorld {

def main(args: Array[String]): Unit = {
val system = ActorSystem("HelloWorld-App")
val developer = system.actorOf(Props[Employee], "Rene")
// Send the task to the developer
developer ! Task("Write an AKKA Hello World app")
}

}

The code is available on Github, if you want to run it just type:

$ sbt
> run-main io.github.enriquezrene.akka01.HelloWorld
#=> prints 'I'm working on Write an AKKA Hello World app' to STDOUT.