Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 25, 2023 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/684359283e0185b1045b8226eddc01ea to your computer and use it in GitHub Desktop.
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/8fbec65a7314ab093d8fccb3e834d1bc698b1c3c
// 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.3.0"
//> 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