Ben Dilts
Ben Dilts

Reputation: 10745

Scala/Akka: Run code after an actor doesn't receive a message for a given timeout

I have an actor that receives messages in bunches--that is, it typically receives several messages in rapid succession, followed by long periods of inactivity. I would like to execute code when the actor hasn't received a message for, say, 250ms.

Just to clarify what I mean, in Javascript, I might write a bit of code like this:

function gotMessage() {
  if(this.idleTimeout) {
    clearTimeout(this.idleTimeout);
  }

  this.idleTimeout = setTimeout(function() {
    //My code here
  }, 250);
}

How might I achieve that effect in Scala, or in Akka if there's a mechanism for it already in that library?

Upvotes: 0

Views: 1368

Answers (1)

Dylan
Dylan

Reputation: 13922

That functionality is described here (Akka actors documentation page, "Receive timeout" section).

The setReceiveTimeout method has documentation here

Upvotes: 5

Related Questions