Skip to content

Instantly share code, notes, and snippets.

@humandb
Last active August 29, 2015 14:16
Show Gist options
  • Save humandb/d536ae0c3849d4b27cb1 to your computer and use it in GitHub Desktop.
Save humandb/d536ae0c3849d4b27cb1 to your computer and use it in GitHub Desktop.
Akka `sender` discussion
package com.rightster
import akka.actor.{Props, Actor, ActorSystem}
/**
* The output of this code is
*
* Message 1 sent
* Message 2 sent
* Sender 1 sends you a message
* akka://my-system/user/sender1
* Sender 2 sends you a message
* akka://my-system/user/sender2
*
* which means that the sender will hold the value you expect and no need to save it at the beginning of processing the
* message
*
*/
object Test extends App {
val system = ActorSystem("my-system")
val sender1 = system.actorOf(Props[MySender], "sender1")
val sender2 = system.actorOf(Props[MySender], "sender2")
val actor = system.actorOf(Props[MyActor])
actor.tell("Sender 1 sends you a message", sender1)
println("Message 1 sent")
actor.tell("Sender 2 sends you a message", sender2)
println("Message 2 sent")
}
class MyActor extends Actor {
def receive = {
case x: String => {
Thread.sleep(1000)
println(x)
Thread.sleep(1000)
println(sender().path)
}
}
}
class MySender extends Actor {
def receive = {
case _ => println("do nothing")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment