Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

public class WorkflowMutableInstance implements WorkflowInstance {

Expand All @@ -47,6 +48,8 @@ public class WorkflowMutableInstance implements WorkflowInstance {
protected AtomicReference<CompletableFuture<WorkflowModel>> futureRef = new AtomicReference<>();
protected Instant completedAt;

protected final Map<String, Object> additionalObjects = new ConcurrentHashMap<String, Object>();

private Lock statusLock = new ReentrantLock();
private Map<CompletableFuture<TaskContext>, TaskContext> suspended;

Expand Down Expand Up @@ -84,14 +87,18 @@ protected final CompletableFuture<WorkflowModel> startExecution(Runnable runnabl
.inputFilter()
.map(f -> f.apply(workflowContext, null, input))
.orElse(input))
.whenComplete(this::whenFailed)
.whenComplete(this::whenCompleted)
.thenApply(this::whenSuccess);
futureRef.set(future);
return future;
}

private void whenFailed(WorkflowModel result, Throwable ex) {
private void whenCompleted(WorkflowModel result, Throwable ex) {
completedAt = Instant.now();
additionalObjects.values().stream()
.filter(AutoCloseable.class::isInstance)
.map(AutoCloseable.class::cast)
.forEach(WorkflowUtils::safeClose);
if (ex != null) {
handleException(ex instanceof CompletionException ? ex = ex.getCause() : ex);
}
Expand Down Expand Up @@ -278,5 +285,9 @@ public boolean cancel() {
}
}

public <T> T additionalObject(String key, Supplier<T> supplier) {
return (T) additionalObjects.computeIfAbsent(key, k -> supplier.get());
}

public void restoreContext(WorkflowContext workflow, TaskContext context) {}
}