vicaba vicaba - 1 year ago 133
Scala Question

How to schedule random messages in Akka? (Message reference is always the same)

I'm trying to schedule random messages with Akka scheduler.schedule, something like:

system.scheduler.schedule(1 second, 5 seconds, actorRef,

The problem is that the "Int" reference gets caught and the message the scheduler sends is always the same number. In other words, the random number is only generated once.

I can't come up with a workaround for that. Any help?

Answer Source

Use the overloaded schedule method which takes a function to execute periodically. In the function body send the message (random number).

context.system.scheduler.schedule(1 second, 5 second) { actorRef ! Random.nextInt(10) }

This way nextInt will be called every time message is sent to the actor.So new random number will be generated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download