I need an actor to send a message every minute. How do I best achieve this behaviour? I am afraid of using java.lang.Thread.sleep(long millis)
as a thread can be shared among many actors in Scala, as far as I understand.
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Error in Scala Compiler: java.lang.AssertionError:
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Testing request with CSRF Token in Play framework
- Run project with java options via sbt
Create an actor with
receiveWithin
to act as the timer.I ended up in creation of dedicated Runnable instance, which keeps sending a message to the target actor. Like
Or as @Daniel mentioned, here a running example:
You can use Akka FSM to model an actor that stays
forMax
millis in a waiting state and then sends a message, e.g. by switching to another state while usingonTransition
and staying there for 0 millis to switch back to waiting state. There is a good example at the akka page.Since scala.actors is now deprecated and being replaced with akka actors (and since there is no react or receiveWithin in akka actors), here is how to do it using akka actors (it's actually less of a 'hack' than using receiveWithin anyways IMHO).
The example below schedule a runnable to be invoked after 5 seconds:
Why use an actor when a thread is what you want?