Skip to content

Instantly share code, notes, and snippets.

@zhangxu
Last active August 29, 2015 14:21
Show Gist options
  • Save zhangxu/e3c0822d25e6db92b8c4 to your computer and use it in GitHub Desktop.
Save zhangxu/e3c0822d25e6db92b8c4 to your computer and use it in GitHub Desktop.
Forward message
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.ExecutionContext._
import scala.concurrent.duration._
import Implicits.global
implicit val timeout = Timeout(30.seconds)
val system = ActorSystem("system")
case class Msg(msg: String)
val responder = system.actorOf(
Props {
new Actor with ActorLogging {
override def receive = {
case Msg(msg) =>
log.warning("Message received by responder: {} from {}. And send the reply message back",
msg, sender())
sender() ! Msg("world")
}
}
},
"responder"
)
val broker = system.actorOf(
Props {
new Actor with ActorLogging {
override def receive = {
case Msg(msg) =>
log.warning("Message received by broker: {} from {}", msg, sender())
responder.forward(Msg(msg))
}
}
},
"broker"
)
val producer = system.actorOf(
Props {
new Actor with ActorLogging {
override def receive = {
case msg: String =>
log.warning("Sending msg {} from {} by telling", msg, self)
broker ! Msg(msg)
case Msg(msg) =>
log.warning("Get reply msg {} from {}", msg, sender())
}
}
},
"producer"
)
producer ! "hello"
(broker ? Msg("hello")).map(println _)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment