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>(Instant.now().plusHours(24),
x -> x.sendNewlyRegisteredEmail());
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:
- a ZonedDateTime
- an OffsetDateTime
- a LocalDateTime
- and an Instant
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 and the JobRequestScheduler
.
@Inject
private JobScheduler jobScheduler;
jobScheduler.schedule<EmailService>(Instant.now().plusHours(24),
x -> x.sendNewlyRegisteredEmail());
@Inject
private JobRequestScheduler jobRequestScheduler;
jobRequestScheduler.schedule(Instant.now().plusHours(24),
new SendNewlyRegisteredEmailJobRequest());