Class TaskContext

  • Direct Known Subclasses:
    RunnerTaskContext

    public class TaskContext
    extends Object
    The TaskContext class gives access to the Task id, the Task name, the state, ... .

    Using the getMetadata(), it also allows to store some data between different task retries so tasks can be made re-entrant. This comes in handy when your task 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 task is completed successfully the metadata is cleared (for storage purpose reasons).

    • Constructor Detail

      • TaskContext

        protected TaskContext()
      • TaskContext

        protected TaskContext​(Task task)
        Keep constructor package protected to remove confusion on how to instantiate the TaskContext. Tip - To use the TaskContext in your task, pass TaskContext.Null
        Parameters:
        task - the task for this TaskContext
    • Method Detail

      • getTaskId

        public UUID getTaskId()
      • getTaskName

        public String getTaskName()
      • getTaskState

        public StateName getTaskState()
      • getCreatedAt

        public Instant getCreatedAt()
      • getUpdatedAt

        public Instant getUpdatedAt()
      • getSignature

        public String getSignature()
      • getMetadata

        public Map<String,​Object> getMetadata()
        Gives access to Task Metadata via an UnmodifiableMap. To save Metadata, use the saveMetadata(String, Object) method
        Returns:
        all user defined metadata about a Task. This metadata is only accessible up to the point a task succeeds.
      • saveMetadata

        public void saveMetadata​(String key,
                                 Object metadata)
        Allows saving metadata for a certain Task. 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 metadata
        metadata - the metadata itself
      • saveMetadataIfAbsent

        public void saveMetadataIfAbsent​(String key,
                                         Object metadata)
        Allows saving metadata for a certain task. 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 metadata
        metadata - the metadata itself