Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between timers.startSingleTimer and scheduler.scheduleOnce in Akka Actor?

I am designing an actor that should schedule sending a message to itself.

I notice that there are at least two ways to do it.

I would like to understand the difference to choose the right one.

The first is one method of akka.actor.Timers:

def startSingleTimer(key: Any, msg: Any, timeout: FiniteDuration): Unit

The second is the pretty common way with scheduler of actor context system:

final def scheduleOnce(
    delay:    FiniteDuration,
    receiver: ActorRef,
    message:  Any)(implicit executor: ExecutionContext,
                            sender: ActorRef = Actor.noSender): Cancellable

Question:

  • Which is the main difference between them in case of scheduling a message to itself?
  • Is it a good idea to pass actor context to scheduleOnce method?
like image 596
mkUltra Avatar asked Jan 30 '19 09:01

mkUltra


People also ask

Is Akka used for scheduling in spark?

Spark uses Akka basically for scheduling. All the workers request for a task to master after registering. The master just assigns the task. Here Spark uses Akka for messaging between the workers and masters.

What is actor system in Akka?

What is an Actor in Akka? An actor is essentially nothing more than an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognize the type of message it has received and take action accordingly.

What is Akka typed?

Akka “Typed Actors”, now replaced by Akka Typed, were an implementation of the Active Objects pattern. Essentially turning method invocations into asynchronous dispatch instead of synchronous that has been the default way since Smalltalk came out.


1 Answers

akka.actor.Timers.startSingleTimer

  • Can be used only within an actor
  • Allows only scheduling a message to self, ie not possible to schedule a message to some other actors
  • If actor dies, the active timers are cancelled automatically
  • Keeps track of active timers by user provided key

context.system.scheduler.scheduleOnce

  • Can be used to schedule messages outside and inside actors
  • Requires explicit ActorRef for receiver and optional sender
  • There is no automatic clean-up process. All should be handled explicitly by calling returned akka.actor.Cancellable

So, if you just need to schedule messages to itself, pick akka.actor.Timers

Is it a good idea to pass actor context to scheduleOnce method?

Not sure in what way you want to do it, but in general actor context must be used only within receive method and not passed outside of an actor neither used in callback methods of Futures.

like image 73
Ivan Stanislavciuc Avatar answered Sep 20 '22 12:09

Ivan Stanislavciuc