Scheduling tasks

Wisdom lets you define periodic jobs. To define a job, you need to:

  1. implement the scheduled interface, and expose it as a service

  2. add the @Every on a method that will be executed periodically

About the first step, if you are in a controller, you just have to implement the Scheduled interface:

include::{sourcedir}/controllers/ScheduledController.java[tags=scheduled]

If you are not in a controller, you need to use the @Provides annotation to expose the service (or just use the @Service annotation).

include::{sourcedir}/tasks/Printer.java[tags=scheduled]

For each method you want to run periodically, add the @Every annotation specifying the period. The period format is very simple:

"1s" : every second
"1m" : every minute
"1h" : every hour
"1d" : every day
"2h30m" : every 2 hours and 30 minutes

The task is not run immediately but only after the specified period.

Integrating with Akka

Akka uses the Actor Model to raise the abstraction level and provide a better platform to build correct concurrent and scalable applications. Actors also provide the abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications.

The application actor system

Akka can work with several containers called ActorSystems. An actor system manages the resources it is configured to use in order to run the actors it contains.

Wisdom defines a special actor system to be used by the applications. You can access the default actor system using the org.wisdom.akka.AkkaSystemService service:

include::{sourcedir}/controllers/Akka.java[tags=akka]

Scheduling asynchronous tasks

Once you have retrieved the actor system, you can schedule messages sending to actors and executing tasks (functions or Runnable instances). You will get a Cancellable back that you can call cancel on to cancel the execution of the scheduled operation.

For example, to send a message to the testActor every 30 minutes:

include::{sourcedir}/controllers/Akka.java[tags=schedule]

Alternatively, to run a block of code ten milliseconds from now:

include::{sourcedir}/controllers/Akka.java[tags=runOnce]