Skip to content

Instantly share code, notes, and snippets.

@tinoadams
Created May 27, 2012 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinoadams/2800556 to your computer and use it in GitHub Desktop.
Save tinoadams/2800556 to your computer and use it in GitHub Desktop.
Shows the behaviour of setReceiveTimeout
import java.util.Date
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.ReceiveTimeout
import akka.util.duration._
class MyActor extends Actor {
context.setReceiveTimeout(5 seconds)
def receive = {
case x: String => println(new Date + " / Recieved: " + x)
case ReceiveTimeout => println(new Date + " / No message received since 5 seconds")
}
}
object Main extends App {
val sys = ActorSystem("test")
val a = sys.actorOf(Props[MyActor])
println(new Date + " / Started")
Thread.sleep(15000) // expect two timeout messages, one after 5 seconds, next after 10
// send ten msg's (1 every second) just when we should receive the third timeout
1 to 10 foreach { x =>
a ! "Message: " + x
Thread.sleep(1000)
}
// the next timeout message should be received 5 seconds after the loop finishes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment