liveBook · Manning

Timer

As explained in Akka in Action, Second Edition

A timer is a scheduling mechanism that sends a specified message after a configured delay, typically once or repeatedly.

A timer schedules a message for later delivery without making an actor wait. The actor continues processing its mailbox while the delay runs, so delayed work fits the same message-driven flow as ordinary commands. section 2.5

For a one-off action, create timers with `Behaviors.withTimers` and call `startSingleTimer` with the message and a `FiniteDuration` delay. The scheduled message must belong to the actor protocol; Scala duration syntax allows values such as `t.second`. section 2.5

Reactivate a wallet later

A wallet handles `Deactivate(seconds)` by scheduling its private `Activate` command, then switches to its deactivated behavior. `Activate` cannot be sent by outside actors, keeping automatic reactivation as internal protocol behavior. After three seconds, the application output shows the activation message arriving. section 2.5

Timers can also enforce a bounded validation window. When a bet opens, it starts a 10-second timer for `ValidationsTimedOut`, persists the opening event, requests market and funds validation, and later handles the timeout only while the bet remains open. section 12.1.3

Use a timer when delayed work should remain explicit in the actor protocol:

  • Schedule a private self-message for lifecycle actions such as wallet reactivation. section 2.5
  • Schedule a deadline message while asynchronous validations proceed through ordinary commands and events. section 12.1.3
  • Keep delayed-message handling state-specific, as `ValidationsTimedOut` is handled in `OpenState`. section 12.1.3
Listing 2.9 activated behavior from WalletTimer
import scala.concurrent.duration._
 
object WalletTimer {
   ...
 
  def activated(total: Int): Behavior[Command] =
    Behaviors.receive { (context, message) =>
      Behaviors.withTimers { timers =>                     #1
        message match {
          case Increase(amount) =>
            val current = total + amount
            context.log.info(s"increasing to $current")
            resume(current)
          case Deactivate(t) =>
            timers.startSingleTimer(Activate, t.second)    #2
            deactivated(total)
          case Activate =>
            Behaviors.same
       }
     }
   }
}

Inspect the minimal wallet behavior: timer setup sits beside normal message matching, and deactivation schedules `Activate` before returning the deactivated behavior. section 2.5

How Akka in Action, Second Edition covers it

How deep it goes
Demonstrates one-shot scheduling through `Behaviors.withTimers` and `startSingleTimer`, without detailing repeated timers.
Examples
Uses a wallet that reactivates after deactivation and a bet that times out pending market and funds validations.
What it assumes
Builds on actor messages and behaviors while introducing timer construction and duration arguments directly.
What you can do after
Apply it via Akka `TimerScheduler` to send a delayed protocol message without blocking an actor.
How it connects
Connects timers to actor protocols, behaviors, event sourcing, cluster sharding, and state machines.

Also explained in

Compare how 4 books explain timer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.