libraryDependencies += "com.typesafe.akka" % "akka-actor_2.11" % "2.4.8"
Actor for string output:
class OutputActor extends Actor {
override def receive: Receive = {
case message => println(message)
}
}
Actor for string modifying:
class AppendActor(outputActor: ActorRef) extends Actor {
override def receive: Receive = {
case message: String =>
val changed = s"Hello, $message!"
outputActor ! changed
case unknown =>
println(s"unknown message: $unknown")
}
}
object HelloWorld extends App {
val system = ActorSystem("HelloWorld")
val outputActor = system.actorOf(Props[OutputActor], name = "output")
val appendActor = system.actorOf(Props(classOf[AppendActor], outputActor), name = "appender")
appendActor ! "Akka" // send test message
Thread.sleep(500) // wait for async evaluation
system.terminate() // terminate actors system
}
Program output:
Hello, Akka!