Package org.jobrunr.jobs.context
Class JobContext
java.lang.Object
org.jobrunr.jobs.context.JobContext
- Direct Known Subclasses:
RunnerJobContext
The JobContext class gives access to the Job id, the Job name, the state, ... .
Using the getMetadata()
, it also allows to store some data between different job retries so jobs can be made re-entrant.
This comes in handy when your job exists out of multiple steps, and you want to keep track of which step already succeeded. Then,
in case of a failure, you can skip the steps that already completed successfully.
As soon as the job is completed successfully the metadata is cleared (for storage purpose reasons).
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
protected
JobContext
(Job job) Keep constructor package protected to remove confusion on how to instantiate the JobContext. -
Method Summary
Modifier and TypeMethodDescriptiongetJobId()
Gives access to Job Metadata via an UnmodifiableMap.logger()
progressBar
(int totalAmount) progressBar
(long totalAmount) void
saveMetadata
(String key, Object metadata) Allows saving metadata for a certain Job.void
saveMetadataIfAbsent
(String key, Object metadata) Allows saving metadata for a certain Job.
-
Field Details
-
Null
-
-
Constructor Details
-
JobContext
protected JobContext() -
JobContext
Keep constructor package protected to remove confusion on how to instantiate the JobContext. Tip - To use the JobContext in your Job, pass JobContext.Null- Parameters:
job
- the job for this JobContext
-
-
Method Details
-
getJobId
-
getJobName
-
getJobState
-
getCreatedAt
-
getUpdatedAt
-
getSignature
-
logger
-
progressBar
-
progressBar
-
getMetadata
Gives access to Job Metadata via an UnmodifiableMap. To save Metadata, use thesaveMetadata(String, Object)
method- Returns:
- all user defined metadata about a Job. This metadata is only accessible up to the point a job succeeds.
-
saveMetadata
Allows saving metadata for a certain Job. The value must either be a simple type (String, UUID, Integers, ...) or implement the Metadata interface for serialization to Json. Note that it is important that the objects you save are thread-safe (e.g. a CopyOnWriteArrayList, ... ).If the key already exists, the metadata is updated.
- Parameters:
key
- the key to store the metadatametadata
- the metadata itself
-
saveMetadataIfAbsent
Allows saving metadata for a certain Job. The value must either be a simple type (String, UUID, Integers, ...) or implement the Metadata interface for serialization to Json. Note that it is important that the objects you save are thread-safe (e.g. a CopyOnWriteArrayList, ... ).If the key already exists, the metadata is NOT updated.
- Parameters:
key
- the key to store the metadatametadata
- the metadata itself
-