Last active
February 3, 2026 20:18
-
-
Save dacr/684359283e0185b1045b8226eddc01ea to your computer and use it in GitHub Desktop.
Playing with akka-typed / published by https://github.com/dacr/code-examples-manager #5f834595-1e09-439f-b6de-49a85186799b/f2b0c9dd5f8b41f04e53c8ed61912ed66b1266aa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : Playing with akka-typed | |
| // keywords : scala, actors, akka, akka-typed, helloworld, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 5f834595-1e09-439f-b6de-49a85186799b | |
| // created-on : 2019-12-11T08:53:57Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "com.typesafe.akka::akka-actor-typed:2.6.21" | |
| //> using dep "org.scalatest::scalatest:3.2.16" | |
| //> using dep "org.slf4j:slf4j-simple:2.0.7" | |
| // --------------------- | |
| import org.scalatest._, flatspec._, matchers._ | |
| import akka.actor.typed.scaladsl.Behaviors | |
| import akka.actor.typed.{ActorRef, ActorSystem, Behavior} | |
| object HelloWorld { | |
| final case class Greet(whom: String, replyTo: ActorRef[HelloWorldMain.MainMessage]) | |
| def apply(): Behavior[Greet] = Behaviors.receive { (context, message) => | |
| context.log.info("Hello {}!", message.whom) | |
| message.replyTo ! HelloWorldMain.Greeted(message.whom, context.self) | |
| Behaviors.same | |
| } | |
| } | |
| object HelloWorldMain { | |
| sealed trait MainMessage | |
| final case class Start(name: String) extends MainMessage | |
| final case class Greeted(whom: String, from: ActorRef[HelloWorld.Greet]) extends MainMessage | |
| def apply(): Behavior[MainMessage] = | |
| Behaviors.setup { context => | |
| val greeter = context.spawn(HelloWorld(), "greeter") | |
| Behaviors.receiveMessage { | |
| case message: Start => | |
| println(message) | |
| greeter ! HelloWorld.Greet(message.name, context.self) | |
| Behaviors.same | |
| case message: Greeted => | |
| println(message) | |
| context.log.info("Received {}", message.whom) | |
| Behaviors.stopped | |
| } | |
| } | |
| } | |
| object AkkaTypedTest extends AsyncFlatSpec with must.Matchers { | |
| override def suiteName: String = "AkkaTypedTest" | |
| "AkkaTyped" must "succeed" in { | |
| val system: ActorSystem[HelloWorldMain.MainMessage] = ActorSystem(HelloWorldMain(), "hello") | |
| implicit val ec = system.executionContext | |
| //system ! HelloWorldMain.Start("Me") | |
| system ! HelloWorldMain.Start("Me") | |
| system.whenTerminated.map(_ => succeed) | |
| } | |
| } | |
| AkkaTypedTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment