Passing arguments
Pass arguments to background jobs - just like any other normal method invocation.
JobRunr supports the passing of arguments to background jobs, both for jobs being created with Java 8 lambda’s and for jobs being created with the JobRequest
interface.
Since these arguments are serialized, consider their values carefully as they can blow up your StorageProvider
. Most of the time it is more efficient to store concrete values in an application database and pass only their identifiers to your background jobs.
Remember that background jobs may be processed days or weeks after they were enqueued. If you use data that is subject to change in your arguments, it may become stale – database records may be deleted, the text of an article may be changed, etc. Plan for these changes and design your background jobs accordingly.
Java 8 lambda’s
You can pass object to the background job lambda just like any other normal lambda method invocation. These arguments can be any of type java.lang
or even custom objects.
Take the following into account if you are using custom objects:
- it must be possible to serialize and deserialize them via either Jackson, Gson or JSON-B
- foresee a default no-argument constructor (it can be private) - this helps Jackson, Gson or JSON-B to deserialize it
JobRequest and JobRequestHandler
Also when using the JobRequest
approach, you can easily pass arguments. These arguments can again be any of type java.lang
or even custom objects.
Take the following into account if you are using custom objects:
- it must be possible to serialize and deserialize them via either Jackson, Gson or JSON-B
- foresee a default no-argument constructor (it can be private) - this helps Jackson, Gson or JSON-B to deserialize it