Canceling a scheduled operation of the actor
By now, we know how to schedule the actor, but it is also necessary to cancel the scheduler if it has reached a certain condition. For this, we will have to maintain a state if the scheduler has run enough times to achieve the desired result.
Getting ready
Import the Hello-Akka project in the IDE; the other prerequisites are the same as earlier.Â
How to do it…
- Create a file, say,
CancelOperation.scala, in thecom.packt.chapter5Â package. - Add the following import to the top of the file:
       import akka.actor.{Cancellable, Props, ActorSystem, Actor}
       import scala.concurrent.duration._- Define an actor, as follows. It will not compile, as it is referencing the
Schedulerobject:
        class CancelOperation extends Actor {
       var i = 10
       def receive = {
       case "tick" => {
       println(s"Hi, Do you know i do the same task again and
again")
       i = i - 1
       if (i == 0) Scheduler...