Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 08:39
Show Gist options
  • Save dacr/7417b3add200dbd988a9660b2587877a to your computer and use it in GitHub Desktop.
Save dacr/7417b3add200dbd988a9660b2587877a to your computer and use it in GitHub Desktop.
Playing with pekko-typed / published by https://github.com/dacr/code-examples-manager #4ce49910-1d77-4463-87cc-170dff2df987/f6b485068a1202ae52d2032df63730de311fba7
// summary : Playing with pekko-typed
// keywords : scala, actors, pekko, pekko-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 : 4ce49910-1d77-4463-87cc-170dff2df987
// created-on : 2023-06-25T17:53:00+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using repository "https://repository.apache.org/content/groups/snapshots"
//> using dep "org.apache.pekko::pekko-actor-typed:1.0.0-RC3+7-029806f8-SNAPSHOT"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using dep "org.slf4j:slf4j-simple:2.0.7"
// ---------------------
import org.scalatest._, flatspec._, matchers._
import org.apache.pekko.actor.typed.scaladsl.Behaviors
import org.apache.pekko.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