Class TaskContext
- java.lang.Object
-
- cn.boboweike.carrot.tasks.context.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).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTaskContext.Metadata
-
Field Summary
Fields Modifier and Type Field Description static TaskContextNull
-
Constructor Summary
Constructors Modifier Constructor Description protectedTaskContext()protectedTaskContext(Task task)Keep constructor package protected to remove confusion on how to instantiate the TaskContext.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description InstantgetCreatedAt()Map<String,Object>getMetadata()Gives access to Task Metadata via an UnmodifiableMap.StringgetSignature()UUIDgetTaskId()StringgetTaskName()StateNamegetTaskState()InstantgetUpdatedAt()TaskDashboardLoggerlogger()TaskDashboardProgressBarprogressBar(int totalAmount)TaskDashboardProgressBarprogressBar(long totalAmount)voidsaveMetadata(String key, Object metadata)Allows saving metadata for a certain Task.voidsaveMetadataIfAbsent(String key, Object metadata)Allows saving metadata for a certain task.
-
-
-
Field Detail
-
Null
public static final TaskContext Null
-
-
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()
-
logger
public TaskDashboardLogger logger()
-
progressBar
public TaskDashboardProgressBar progressBar(int totalAmount)
-
progressBar
public TaskDashboardProgressBar progressBar(long totalAmount)
-
getMetadata
public Map<String,Object> getMetadata()
Gives access to Task Metadata via an UnmodifiableMap. To save Metadata, use thesaveMetadata(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 metadatametadata- 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 metadatametadata- the metadata itself
-
-