Fire-and-forget method invocation has never been simpler thanks to JobRunr.
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 enqueue method does not call the target method immediately, it runs the following steps instead:
Analyse the lambda to extraxt the method information and all its arguments.
Serialize the method information and all its arguments.
Create a new background job based on the serialized information.
Save background job to the configured StorageProvider.
After these steps were performed, the BackgroundJob.enqueue 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 method call BackgroundJob.enqueue 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 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.
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.
Off-course the above two enqueueing methods can also be done using the JobScheduler bean.