As you already know from the 5 minute intro, you only need to pass a lambda with the corresponding method and its arguments to enqueue a background job:
The methods above do not call the target method immediately but instead run the following steps:
Either the JobRequest is used or the lambda is analyzed to extract the method information and all its arguments.
The method information and all its arguments are serialized to JSON.
A new background job is created based on the serialized information.
The background job is saved to the configured StorageProvider.
After these steps were performed, the BackgroundJob.enqueue or BackgroundJob.create method immediately returns to the caller. Another JobRunr component, called BackgroundJobServer, checks the persistent storage for enqueued background jobs and performs them in a reliable way.
Instead of the static methods on the BackgroundJob class, you can also use the JobScheduler bean. It has exactly the same methods as the BackgroundJob class. To use it, just let your dependency injection framework inject an instance of the JobScheduler bean and continue as before:
As before, you also do not need an instance of the myService available if the MyService class is know by your dependency injection framework.
Enqueueing a job using the JobScheduler and a Java 8 lambda
Enqueueing a job using the JobRequestScheduler and a JobRequest
Enqueueing background jobs in bulk
Sometimes you want to enqueue a lot of jobs - for example send an email to all users. JobRunr can process a Java 8 Stream of objects and for each item in that Stream, create a background job. The benefit of this is that it saves these jobs in bulk to the database - resulting in a big performance improvement.
Enqueueing many jobs using a Java 8 lambda
This allows for nice integration with the Spring Data framework which can return Java 8 Streams - this way, items can be processed incrementally and the entire database must not be put into memory.
Of course the above methods to enqueue jobs can also be done using the JobScheduler bean.
Enqueueing many jobs using the JobScheduler and a Java 8 lambda
Enqueueing many jobs using the JobRequestScheduler and a JobRequest
Enqueueing emails in bulk using the Stream API by means of a JobRequest.
Prevent duplicate jobs thanks to the JobIdentifier
Sometimes you want to limit how many times a job is created. JobRunr Pro helps you with the JobIdentifier which will only create the job if no job with that identifier exists.
Creating a job only once using a JobIdentifier
Replacing an existing job using a JobIdentifier
If you need to replace an existing job with an identifier, this can be done as follows:
You can learn more about replacing and updating jobs here.
Please also see the best practices for more information.