The deadly combination of Akka with Scala

Reading Time: 3 minutes

Hi, today in this blog we are going to work with Akka using the Scala Programming Language. In today’s blog, we are going to learn the basics of the Akka using Scala.

What are we going to learn today?

  • Learn the Basics of Akka
  • Create Akka System and Akka Actors
  • Make use of Akka actors
  • Create an application using Akka and Scala
AkkaScala

About Akka

What is Akka and Why we use it?

Akka is a toolkit and uses the Actor model system in which there are different actors that can message each other and initiate action. Akka helps provide concurrency and avoid the locking mechanism.

In more simple words Akka saves you from the trouble of dealing with low-level concurrency primitives yourself when writing multi-threaded high-performance apps. Instead, you can use Actors, a higher-level construct, to write your concurrent code.

Akka is very useful to write server-side scalable applications. Using Akka, it is very easy to send messages to various nodes of your application.


Create an Akka System

Now before creating an Akka actor we need to add some of the dependencies in our Scala Application.

Add the following dependency in your project:

val AkkaVersion = "2.6.18"
libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % AkkaVersion

Now create a package in the scala folder and make an object type with the name given by you.

object Counter extends App{
  val actorSystem = ActorSystem("actorSystem")

}

Inside that just use the above code. This is the way to create an actor system for your application.

Now to build an application to use our actor’s functions we are going to create a class a class as MessageReceiver and we need to extend this class with the Actor(Actor base trait that should be extended by or mixed to create an Actor with the semantics of the ‘Actor Model’)

When we extend this Actor with our class then we also need to override Actor trait members to use it.


extends Actor

class MessageReceiver extends Actor{
  override def receive: Receive = {
   

  }
}

In this case, this trait provides a method receive which is a Receive type. This Receive works as a Message Receiver which receives the messages from the Actors, Soon we are going to see the example of this.


Let’s implement this method by providing some cases based on data types.

class MessageReceiver extends Actor{
  override def receive: Receive = {
    case message: String => {
      println(s"I received the message in string [$message]")
    }
    case integer: Int => {
      println(s"I received the message in integer [$integer]")
    }

  }
}

Okay so we are done with the 2 steps:

  1. To create an Actor System
  2. To create a class that extend Actor and its receive method

Now the last step we are going to use to initiate the Actors. This is the way to do this.

First, we need to create an Actor by using the ActorSystem like this.

val actorCaller = actorSystem.actorOf(Props[MessageReceiver], "actorCaller")

The props in the actorOf contain the class of which we want to make an actor. We can create actors like this as much as we want using the ActorSystem.


Last step- Invoke Actors

Now the last task is to use this Actor.

actorCaller! "Hi actor"
actorCaller! 43

This is the way we make use of any actor. Here “!” provide the sender reference. We can also check at the receiving end which actor has sent the message by using the $self in the print function.


The application we made

package counter

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


object Counter extends App {
  val actorSystem = ActorSystem("actorSystem")

  class MessageReceiver extends Actor{
    override def receive: Receive = {
      case message: String => {
        println(s"[$self] I received the message in string [$message]")
      }
      case integer: Int => {
        println(s"I received the message in integer [$integer]")
      }

    }
  }
  val actorCaller = actorSystem.actorOf(Props[MessageReceiver], "actorCaller")
  actorCaller! "Hi actor"
  actorCaller! 43


}

The output you are going to get is:

[Actor[akka://actorSystem/user/actorCaller#-588186035]] I received the message in string [Hi actor]
I received the message in integer [43]


message passing mpsc

Written by 

Nitin is a Software Consultant, with experience of more than 1.4 years. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.

1 thought on “The deadly combination of Akka with Scala4 min read

Comments are closed.