Package dev.getelements.elements.rt
Interface TaskContext
- All Known Implementing Classes:
SimpleTaskContext
public interface TaskContext
Manages the global state of currently operating tasks. This allows a callers to listen for tasks as they are running
and receive callbacks that the task has completed. While each
Resource
is responsible for managing tasks,
this is the hub through which all tasks are managed.-
Method Summary
Modifier and TypeMethodDescriptionboolean
finishWithError
(TaskId taskId, Throwable error) boolean
finishWithResult
(TaskId taskId, Object result) Finishes the task with associatedTaskId
with the provided result.void
default void
start()
Starts thisTaskContext
.default void
stop()
Stops thisTaskContext
.
-
Method Details
-
start
default void start()Starts thisTaskContext
. -
stop
default void stop()Stops thisTaskContext
. -
register
Registers the suppliedTaskId
, resultConsumer
, andConsumer<Throwable>
for the associated task. TheTaskContext
will store the associated result consumers internally and at some point later invoke either the result or failure consumer. However, it is advisable to account for scenarios where neither consumer may be called. This may happen in the event the remote service has been restarted and in-memory consumers were lost. The calling code should have a strategy to account for timeouts where appropriate.- Parameters:
taskId
- the task IDtConsumer
-throwableTConsumer
-- Throws:
DuplicateTaskException
- if the suppliedTaskId
was already registered.
-
finishWithResult
Finishes the task with associatedTaskId
with the provided result. Consumers previously registered with theregister(TaskId, Consumer, Consumer)
call will be processed. As not all tasks are registered with a set of listeners, this may simply return false indicating that no listeners were notified.- Parameters:
taskId
-- Returns:
- true if the task was finished, false if otherwise
-
finishWithError
Finishes the task with associatedTaskId
with the providedThrowable
error. As not all tasks are registered with a set of listeners, this may simply return false indicating that no listeners were notified.- Parameters:
taskId
- theTaskId
- Returns:
- true if the task was finished, false if otherwise
-