Timers

Timers wait for a predefined amount of time, before triggering, once or repeatedly. They cou be used to specify time supervision, or to trigger certain logic after a certain period, or to repeat some action at regular intervals.

A Timer node is set up with a delay and a period. The delay specifies the amount of time (in milliseconds) to wait after node activation before triggering the timer the first time. The period defines the time between subsequent trigger activations. A period of 0 results in a one-shot timer.

The timer service is responsible for making sure that timers get triggered at the appropriate times. Timers can also be cancelled, meaning that the timer will no longer be triggered.

Timers can be used in two ways inside a process:

By default, the Drools engine is a passive component, meaning that it will start processing only if you tell it to. Typically, you first insert the necessary data and then tell the engine to start processing. In passive mode, a timer that has been triggered will be put on the action queue. This means that it will either be executed automatically if the engine is still running, or it will become delayed until the engine is told to start executing by the user (by calling fireAllRules()).

When using timers, it does usually make sense to let the Drools engine operate as an active component, so that it will execute actions whenever they become available, without the need to wait until the user tells it to resume execution. Thus, a timer would become effective as soon as it triggers. To make the engine fire all actions continuously, you must call the method fireUntilHalt(), whereupon the engine operates until halt() is called. Note that you should call fireUntilHalt() in a separate thread as it will only return if the engine has been halted, either by the user or some some logic calling halt() on the session. The following code snippet shows how to do this.

new Thread(new Runnable() {
  public void run() {
    ksession.fireUntilHalt();
  }
}).start();

// starting a new process instance
ksession.startProcess("...");
// any timer that triggers will now be executed automatically