Skip to content

Instantly share code, notes, and snippets.

@Moverr
Created July 8, 2022 23:58
Show Gist options
  • Save Moverr/b6e857624df28e842043a6a8bb0944f0 to your computer and use it in GitHub Desktop.
Save Moverr/b6e857624df28e842043a6a8bb0944f0 to your computer and use it in GitHub Desktop.
Simple Calculator Using Typed Actors Case Classes and Sealed Trait
package part1Recap
import akka.actor.typed.{ActorSystem, Behavior}
import akka.actor.typed.scaladsl.Behaviors
object ActorCalcApp {
sealed trait calculator
case class calc[T](a:T,b:T,action:String) extends calculator
object SimActor {
def apply(): Behavior[calculator] = Behaviors.receive{(context,message)=>{
message match {
case calc(a:Int, b:Int, "add") => {
context.log.info(s" ${a + b}")
}
case calc(a:Float, b:Float, "add") => {
context.log.info(s" ${a + b}")
}
case calc(a:Float, b:Float, "multi") => {
context.log.info(s" ${a * b}")
}
case calc(a:Int, b:Int, "multi") => {
context.log.info(s" ${a * b}")
}
case calc(a:Float, b:Float, "dev") => {
context.log.info(s" ${a / b}")
}
case calc(a:Int, b:Int, "dev") => {
context.log.info(s" ${a / b}")
}
}
Behaviors.same
}}
}
def demoActor(): Unit ={
val actorSystem =ActorSystem(SimActor(),"Calculator");
val ca = calc(1,2,"add");
val ca1 = calc((1.23).toFloat,(2.67).toFloat,"add");
actorSystem ! ca
Thread.sleep(1000)
actorSystem ! ca1
val ca12 = calc((1.23).toFloat,(2.67).toFloat,"dev");
Thread.sleep(1000)
actorSystem ! ca12
Thread.sleep(1000)
actorSystem.terminate()
}
def main(args: Array[String]): Unit = {
demoActor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment