Introduction to Akka Typed with Scala for beginners.

Learn to Build distributed and fault tolerant systems with me and Dead-pool.

Image for post
Image for post

What is Akka and why you should learn it with Scala?

Simplified- Akka is Great, Scala is Awesome!!!!

How it started(Akka Classic) / How its going(Akka Typed)

What the hell is an Actor?

Why use Scala?

1- Building the Project

Image for post
Image for post
val akkaVersion = "2.6.10"

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor-typed" % akkaVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"ch.qos.logback" % "logback-classic" % "1.2.3"
)
Image for post
Image for post
add the code snippet and hit load sbt changes.

Create main .scala File

traits and case classes

Required Imports

import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorRef, ActorSystem, Behavior}
import scala.concurrent.ExecutionContextExecutor

Actor API/Protocol

sealed trait MeanPool
case class comments(what:String) extends MeanPool

Define the Behavior in the main app-

object DeadPoolActor extends App{
def apply(): Behavior[MeanPool] = {
Behaviors.receive {
case (context, comments(comment)) =>
context.log.info(comment)
Behaviors.same
}
}

Create ActorSystem

implicit val system: ActorSystem[MeanPool] = ActorSystem(DeadPoolActor(), "DeadPoolActor1")
ActorSystem(guardianBehavior: Behavior[T], name: String)

Send Messages to Actor System

val findFrancis: ActorRef[MeanPool] = system

findFrancis
! comments("Maximum Effort")

Output

Image for post
Image for post
Output

Complete Code Snippet

import akka.actor.typed.{ActorRef, ActorSystem, Behavior}
import akka.actor.typed.scaladsl.Behaviors

sealed trait MeanPool
case class comments(what:String) extends MeanPool

object DeadPoolActor extends App{
def apply(): Behavior[MeanPool] = {
Behaviors.receive {
case (context, comments(comment)) =>
context.log.info(comment)
Behaviors.same
}
}
implicit val system: ActorSystem[MeanPool] = ActorSystem(DeadPoolActor(), "DeadPoolActor1")

val findFrancis: ActorRef[MeanPool] = system

findFrancis
! comments("Maximum Effort")
}

Written by

Scala Developer with interestes in Distributed computing and fast data.Content creator,Blogger.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store