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>(, ChronoUnit.HOURS), 
  x -> x.sendNewlyRegisteredEmail());

You can also use the JobBuilder to achieve the same result:

    .withName("Send welcome email to newly registered users")
    .<EmailService>withDetails(x -> x.sendNewlyRegisteredEmail()));
Scheduling emails is also possible using the JobBuilder pattern

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 poll interval is equal to 15 seconds, but you can change it by passing the relevant argument to the BackgroundJobServer constructor or by changing the properties for your preferred framework.

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.

All of the above methods are of course also available on the JobScheduler bean and the JobRequestScheduler.

private JobScheduler jobScheduler;

jobScheduler.schedule<EmailService>(, ChronoUnit.HOURS), 
  x -> x.sendNewlyRegisteredEmail());
Scheduling a background job in the future using the JobScheduler bean
private JobRequestScheduler jobRequestScheduler;

jobRequestScheduler.schedule(, ChronoUnit.HOURS), 
  new SendNewlyRegisteredEmailJobRequest());
Scheduling a background job in the future using the JobRequestScheduler bean
private JobRequestScheduler jobRequestScheduler;

  .withJobRequest(new SendNewlyRegisteredEmailJobRequest()));
Scheduling a background job in the future using the JobBuilder pattern

Note that scheduled jobs may not be executed on the exact moment you specified: whenever JobRunr fetches all the jobs that are scheduled and need to be executed, it fetches all jobs that need to happen in the next poll interval and enqueues them immediately. This may result in a difference of a couple of seconds. If you need real-time scheduling, then have a look at JobRunr Pro.