#!/usr/bin/env amm import $ivy.`com.typesafe.akka::akka-actor-typed:2.6.8` import $ivy.`ch.qos.logback:logback-classic:1.2.3` import akka.actor.typed.delivery.ConsumerController.Command import akka.actor.Actor import akka.actor.typed.ActorRef import akka.actor.typed.ActorSystem import akka.actor.typed.Behavior import akka.actor.typed.scaladsl.Behaviors import akka.util.Timeout import scala.concurrent.duration._ import akka.actor.typed.scaladsl.AskPattern._ import scala.concurrent.ExecutionContext.Implicits.global object CounterActor { sealed trait Command final case object IncCount extends Command final case class CounterResult(count: Int) final case class GetResult(sender: ActorRef[RootActor.Command]) extends Command var counter = 0 def apply(): Behavior[Command] = Behaviors.receive { (context, command) => command match { case IncCount => counter += 1 case GetResult(sender) => context.log.info(s"Value of counter is $counter"); sender ! RootActor.Reply(counter) } Behaviors.same } } object RootActor { sealed trait Command case class Reply[T](reply: T) extends Command case object Begin extends Command case object GetResult extends Command implicit val timeout: Timeout = 3.seconds def apply(): Behavior[Command] = Behaviors.setup { context => implicit val system = context.system val counterActor = context.spawn(CounterActor(), "counterActor") Behaviors.receiveMessage { message => message match { case Reply(reply) => context.log.info(s"Received message $message") case Begin => context.log.info("--- Beginning ---") counterActor ! CounterActor.IncCount counterActor ! CounterActor.IncCount counterActor ! CounterActor.IncCount counterActor ! CounterActor.IncCount counterActor ! CounterActor.IncCount case GetResult => counterActor ! CounterActor.GetResult( context.self ) } Behaviors.same } } } @main def main() = { implicit val rootActor = ActorSystem(RootActor(), "TestActors") implicit val timeout: Timeout = 3.seconds rootActor ! RootActor.Begin rootActor ! RootActor.GetResult Thread.sleep(1000) }