Scheduling jobs

Schedule jobs in the future and monitor them using the dashboard.

Sometimes you may want to postpone a method invocation; for example, to send an email to newly registered users a day after their registration. To do this, just call the BackgroundJob.schedule method and pass the desired delay:

BackgroundJob.schedule<EmailService>(x -> x.sendNewlyRegisteredEmail(), 
  Instant.now().plusHours(24));

JobRunr’s BackgroundJobServer periodically checks all scheduled jobs and enqueues them when it is time to run them, allowing workers to execute them. By default, the check interval is equal to 15 seconds, but you can change it by passing the relevant argument to the BackgroundJobServer constructor.

The BackgroundJob.schedule methods has overloads and accepts:

All DateTime objects are converted to an Instant - in case of the LocalDateTime, the systemDefault zoneId is used to convert it.

These scheduling methods are also off-course available on the JobScheduler bean.

@Inject
private JobScheduler jobScheduler;

jobScheduler.schedule<EmailService>(x -> x.sendNewlyRegisteredEmail(), 
  Instant.now().plusHours(24));
Scheduling a background job in future using the JobScheduler bean