Overview

This plugin allows your application and the other plugins to register scheduled tasks to be run. A scheduled tasks, also called "cron job", is a piece of code that will be run one or multiple times, at a specific date or at a specific interval.

The plugin uses the Quartz scheduler library under the hood, and provides utilities to easily register your scheduled tasks, with full dependency injection support.

Usage

Writing a Scheduled Tasks class

The first thing to do is to create a class for your new scheduled task. A scheduled task's class must implement the SpincastScheduledTask interface, but we also recommend that it extends the default SpincastScheduledTaskBase abstract class, which provides some built-in functionalities.

public class MyScheduledTask extends SpincastScheduledTaskBase {

    @Override
    public Trigger getTrigger() {
        return TriggerBuilder.newTrigger()
                             .startNow()
                             .withSchedule(simpleSchedule().withIntervalInMinutes(60)
                                                           .repeatForever())
                             .build();
    }

    @Override
    public void executeSafe(JobExecutionContext context) {
        // The actions to be executed...
        System.out.println("I run!");
    }
}

Explanation :

  • 3-10 : The getTrigger() is the method by which you define when the scheduled task should run. Have a look at the Quartz documentation to see all the available options. There are a lot!
  • 12-16 : The executeSafe() is the method executed when the scheduled task runs. You implement the logic of your scheduled task there. Note that since we extend the provided SpincastScheduledTaskBase class, the execution of the scheduled task is safe: if a previous instance of the job is already running, any new instance will be canceled and won't run.

Registering the scheduled task

Once your scheduled tasks are ready, you need to register them in the Guice context. You do this using the Multibinder<SpincastScheduledTaskJob> multibinder, in your application's Guice module:

public class AppModule extends SpincastGuiceModuleBase {
    @Override
    protected void configure() {
       
        // Register the app's scheduled task
        Multibinder<SpincastScheduledTask> scheduledTasksMultibinder = Multibinder.newSetBinder(binder(), SpincastScheduledTask.class);
        scheduledTasksMultibinder.addBinding().to(MyScheduledTask.class).in(Scopes.SINGLETON);
        scheduledTasksMultibinder.addBinding().to(MyOtherScheduledTask.class).in(Scopes.SINGLETON);
        
        // Other bindings...
    }
}

You can also use the Multibinder<Set<SpincastScheduledTask>> to register a set of scheduled tasks, instead of registering them one by one. This alternative is useful when you have a Provider that decides of the scheduled tasks to bind:

public class AppModule extends SpincastGuiceModuleBase {
    @Override
    protected void configure() {
       
        // Register the app's scheduled tasks provider
        Multibinder<Set<SpincastScheduledTask>> acheduledTaskSetsMultibinder =
                Multibinder.newSetBinder(binder(), 
                                         Key.get(new TypeLiteral<Set<SpincastScheduledTask>>() {}));
        acheduledTaskSetsMultibinder.addBinding()
                              .toProvider(MyScheduledTasksProvider.class)
                              .in(Scopes.SINGLETON);
        
        // Other bindings...
    }
}

There is nothing more to do! When you start your application, the registered scheduled tasks should run respecting their Triggers.

Installation

1. Add this Maven artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-scheduledtasks</artifactId>
    <version>2.2.0</version>
</dependency>

2. Add an instance of the SpincastScheduledTasksPlugin plugin to your Spincast Bootstrapper:


Spincast.configure()
        .plugin(new SpincastScheduledTasksPlugin())
        // ...