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 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 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.
Of course the above methods to enqueue jobs can also be done using the JobScheduler bean.