{% extends "../../layout.html" %} {% block sectionClasses %}plugins hasBreadCrumb plugins-spincast-crons{% endblock %} {% block meta_title %}Plugins - Spincast Crons{% endblock %} {% block meta_description %}Spincast Crons plugin - Cron jobs / Scheduled tasks management{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

This plugin allows your application and the other plugins to register cron jobs to be run. A cron job, also called "scheduled task", 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 cron jobs, with full dependency injection support.

Usage

Writing a Cron Job class

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

{% verbatim %}

public class MyCronJob extends SpincastCronJobBase {

    @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!");
    }
}
{% endverbatim %}

Explanation :

  • 3-10 : The getTrigger() is the method by which you define when the cron job 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 cron job runs. You implement the logic of your cron job there. Note that since we extend the provided SpincastCronJobBase class, the execution of the cron is safe: if a previous instance of the job is already running, any new instance will be canceled and won't run.

Registering the Crons Jobs

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

{% verbatim %}

public class AppModule extends SpincastGuiceModuleBase {
    @Override
    protected void configure() {
       
        // Register the app's cron jobs
        Multibinder<SpincastCronJob> cronsMultibinder = Multibinder.newSetBinder(binder(), SpincastCronJob.class);
        cronsMultibinder.addBinding().to(MyCronJob.class).in(Scopes.SINGLETON);
        cronsMultibinder.addBinding().to(MyOtherCronJob.class).in(Scopes.SINGLETON);
        
        // Other bindings...
    }
}
{% endverbatim %}

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

{% verbatim %}

public class AppModule extends SpincastGuiceModuleBase {
    @Override
    protected void configure() {
       
        // Register the app's cron jobs provider
        Multibinder<Set<SpincastCronJob>> cronsSetsMultibinder =
                Multibinder.newSetBinder(binder(), 
                                         Key.get(new TypeLiteral<Set<SpincastCronJob>>() {}));
        cronsSetsMultibinder.addBinding()
                            .toProvider(MyCronJobsProvider.class)
                            .in(Scopes.SINGLETON);
        
        // Other bindings...
    }
}
{% endverbatim %}

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

Installation

1. Add this Maven artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-crons</artifactId>
    <version>{{spincast.spincastCurrrentVersion}}</version>
</dependency>

2. Add an instance of the SpincastCronsPlugin plugin to your Spincast Bootstrapper: {% verbatim %}


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

{% endverbatim %}

{% endblock %}