Schedule a task

Schedule tasks

Configuring the scheduler

Blossom uses Quartz Scheduler by default.

A file name `quartz.properties` can be added to your project to configure it further.

See all available configurations here

Add a scheduled task

Creating a job

To create a new job, do the following :

  1. Create a class implementing the `org.quartz.Job` interface.
  2. Add annotations if necessary (see the example)
  3. Autowire existing beans if they are needed by the job

An example is provided below.

Creating a job detail factory

The job definition and execution being created, you know need to reference it with Quartz in order to schedule it.

It requires the creation of a `JobDetailFactoryBean` in your application context.

This factory doesn't schedule anything, it's a definition of your job.

You can configure it by setting :

  • a group name to regroup jobs that are related (e.g : Indexation).
  • a job name
  • a job description
  • a durability (should the job definition be kept even if it's not scheduled ?)

An example is provided below.

Creating triggers

Trigger are what really schedule the jobs at given times.

They can be named and described.

Multiple triggers of different types can be configured for each job detail.

The SimpleTriggerFactoryBean allows you to define a repeat interval in milliseconds, and a repeat count (possibly indefinitely).

This trigger can also be used as a "fire once" trigger with a repeat count of zero.

(Note: the "fire once" trigger will be stored and not triggered again when the application restarts. See below for on-application-start trigger)

The CronTriggerFactoryBean allows you to define cron expression to schedule the job.

Start a job dynamically

Use the blossom provided ScheduledJobService bean to immediately start a job.

Start a job on application start

Use a CommandLineRunner to execute some arbitrary code right after the ApplicationContext has been initialized.