Last active
February 3, 2026 20:21
-
-
Save dacr/84ab3310cfd6d388398e6ce0d73ab63a to your computer and use it in GitHub Desktop.
Simple pekko hello world example / published by https://github.com/dacr/code-examples-manager #d2822abf-e20d-4cc6-8ab6-00a50b1888d8/5e4800e2675121934ee8ccd73f6dd6fcc2996a30
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 : Simple pekko hello world example | |
| // keywords : scala, actors, pekko, helloworld, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : d2822abf-e20d-4cc6-8ab6-00a50b1888d8 | |
| // created-on : 2023-06-24T18:56:35+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:1.0.0-RC3+7-029806f8-SNAPSHOT" | |
| // --------------------- | |
| import scala.concurrent.{ExecutionContextExecutor, Await} | |
| import scala.concurrent.duration.Duration | |
| import org.apache.pekko.actor.{Actor, ActorRef, ActorSystem, PoisonPill, Props, Terminated} | |
| // import akka.pattern.ask // Required to get responses using ? (but take care, of course...) | |
| // --------------------------------------------------------------------------- | |
| object A { | |
| case class Start() | |
| case class AMsg(txt:String) | |
| def props(b: ActorRef) = Props(new A(b)) | |
| } | |
| class A(b: ActorRef) extends Actor { | |
| override def receive = { | |
| case A.Start() => | |
| b ! B.BMsg("hello world") | |
| //sender ! "OK" | |
| case A.AMsg(txt) => | |
| println(s"B says $txt") | |
| context.system.terminate() | |
| case x => | |
| println(s"Unsupported message received $x") | |
| } | |
| } | |
| // --------------------------------------------------------------------------- | |
| object B { | |
| case class BMsg(txt:String) | |
| def props() = Props(new B()) | |
| } | |
| class B() extends Actor { | |
| override def receive = { | |
| case B.BMsg(txt) => | |
| sender() ! A.AMsg(s"message size is ${txt.size} for $txt") | |
| } | |
| } | |
| // --------------------------------------------------------------------------- | |
| object Toto { | |
| val system = ActorSystem("HalSystem") | |
| //implicit val executionContext: ExecutionContextExecutor = system.dispatcher | |
| val b = system.actorOf(B.props()) | |
| val a = system.actorOf(A.props(b)) | |
| a ! A.Start() | |
| //val rcf = (a ? A.Start()) (Timeout(10, TimeUnit.SECONDS)) | |
| //rcf.foreach(rc => println(rc)) | |
| } | |
| Toto.system // Just to boot as object inits (statics) are lazy | |
| Await.result(Toto.system.whenTerminated, Duration.Inf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment