Class CoroutineStep<I,O>
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<I,O>
-
- All Implemented Interfaces:
de.esoco.lib.property.Fluent<CoroutineStep<I,O>>,org.obrel.core.FluentRelatable<CoroutineStep<I,O>>,org.obrel.core.Relatable
- Direct Known Subclasses:
AsynchronousChannelStep,CallSubroutine,ChannelStep,CodeExecution,Collect,Condition,Delay,Iteration,Loop,Select
public abstract class CoroutineStep<I,O> extends org.obrel.core.RelatedObject implements org.obrel.core.FluentRelatable<CoroutineStep<I,O>>
This is the base class for all steps of coroutines. For simple steps it is sufficient to implement the single abstract methodexecute(Object, Continuation)which must perform the actual (blocking) code execution. The default implementations ofrunBlocking(Object, Continuation)andrunAsync(CompletableFuture, CoroutineStep, Continuation)then invoke this method as needed.In most cases it is not necessary to extend this class because the 'step' sub-package already contains implementations of several common steps. For example, a simple code execution can be achieved by wrapping a closure in an instance of the
CodeExecutionstep.Creating a new step subclass is only needed to implement advanced coroutine suspensions that are not already provided by existing steps. In such a case it is typically also necessary to override the method
runAsync(CompletableFuture, CoroutineStep, Continuation)to check for the suspension condition. If a suspension is necessary aSuspensionobject can be created by invokingContinuation.suspend(CoroutineStep, CoroutineStep)for the current step. The suspension object can then be used by code that waits for some external condition to resume the coroutine when appropriate.It is recommended that a step implementation provides one or more static factory methods alongside the constructor(s). These factory methods can then be used as static imports for the fluent builder API of coroutines.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedCoroutineStep()Creates a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Oexecute(I input, Continuation<?> continuation)This method must be implemented by subclasses to provide the actual functionality of this step.voidrunAsync(java.util.concurrent.CompletableFuture<I> previousExecution, CoroutineStep<O,?> nextStep, Continuation<?> continuation)Runs this execution step asynchronously as a continuation of a previous code execution in aCompletableFutureand proceeds to the next step afterwards.OrunBlocking(I input, Continuation<?> continuation)Runs this execution immediately, blocking the current thread until the execution finishes.protected voidterminateCoroutine(Continuation<?> continuation)Allow subclasses to terminate the coroutine they currently run in.java.lang.StringtoString()-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
-
-
-
-
Method Detail
-
runAsync
public void runAsync(java.util.concurrent.CompletableFuture<I> previousExecution, CoroutineStep<O,?> nextStep, Continuation<?> continuation)
Runs this execution step asynchronously as a continuation of a previous code execution in aCompletableFutureand proceeds to the next step afterwards.Subclasses that need to suspend the invocation of the next step until some condition is met (e.g. sending or receiving data has finished) need to override this method and create a
Suspensionby invokingContinuation.suspend(CoroutineStep, CoroutineStep)on the next step. If the condition that caused the suspension resolves the coroutine execution can be resumed by callingSuspension.resume(Object).Subclasses that override this method also need to handle errors by terminating any further execution (i.e. not resuming a suspension if such exists) and forwarding the causing exception to
Continuation.fail(Throwable).- Parameters:
previousExecution- The future of the previous code executionnextStep- The next step to execute or NULL for nonecontinuation- The continuation of the execution
-
runBlocking
public O runBlocking(I input, Continuation<?> continuation)
Runs this execution immediately, blocking the current thread until the execution finishes.- Parameters:
input- The input valuecontinuation- The continuation of the execution- Returns:
- The execution result
-
toString
public java.lang.String toString()
- Overrides:
toStringin classorg.obrel.core.RelatedObject
-
execute
protected abstract O execute(I input, Continuation<?> continuation)
This method must be implemented by subclasses to provide the actual functionality of this step.- Parameters:
input- The input valuecontinuation- The continuation of the execution- Returns:
- The result of the execution
-
terminateCoroutine
protected void terminateCoroutine(Continuation<?> continuation)
Allow subclasses to terminate the coroutine they currently run in.- Parameters:
continuation- The continuation of the current execution
-
-