Last active
May 25, 2024 08:39
-
-
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/e9337fd6c002b009107e2d0d48253ccffcc58347
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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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