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:
You can also use the JobBuilder
to achieve the same result:
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:
- 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.
All of the above methods are of course also available on the JobScheduler
bean and the JobRequestScheduler
.
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.