public abstract class CoroutineStep<I,O> extends org.obrel.core.RelatedObject implements org.obrel.core.FluentRelatable<CoroutineStep<I,O>>
execute(Object,
Continuation)
which must perform the actual (blocking) code execution. The
default implementations of runBlocking(Object, Continuation)
and
runAsync(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 CodeExecution
step.
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 a Suspension
object can be created by invoking Continuation.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.
Modifier | Constructor and Description |
---|---|
protected |
CoroutineStep()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected abstract O |
execute(I rInput,
Continuation<?> rContinuation)
This method must be implemented by subclasses to provide the actual
functionality of this step.
|
protected O |
fail(java.lang.Throwable eError,
Continuation<?> rContinuation)
Signals an execution failure to the given continuation.
|
void |
runAsync(java.util.concurrent.CompletableFuture<I> fPreviousExecution,
CoroutineStep<O,?> rNextStep,
Continuation<?> rContinuation)
Runs this execution step asynchronously as a continuation of a previous
code execution in a
CompletableFuture and proceeds to the next
step afterwards. |
O |
runBlocking(I rInput,
Continuation<?> rContinuation)
Runs this execution immediately, blocking the current thread until the
execution finishes.
|
protected void |
terminateCoroutine(Continuation<?> rContinuation)
Allow subclasses to terminate the coroutine they currently run in.
|
java.lang.String |
toString() |
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public void runAsync(java.util.concurrent.CompletableFuture<I> fPreviousExecution, CoroutineStep<O,?> rNextStep, Continuation<?> rContinuation)
CompletableFuture
and 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 Suspension
by invoking
Continuation.suspend(CoroutineStep, CoroutineStep)
on the next
step. If the condition that caused the suspension resolves the coroutine
execution can be resumed by calling Suspension.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)
.
fPreviousExecution
- The future of the previous code executionrNextStep
- The next step to execute or NULL for nonerContinuation
- The continuation of the executionpublic O runBlocking(I rInput, Continuation<?> rContinuation)
rInput
- The input valuerContinuation
- The continuation of the executionpublic java.lang.String toString()
toString
in class org.obrel.core.RelatedObject
protected abstract O execute(I rInput, Continuation<?> rContinuation)
rInput
- The input valuerContinuation
- The continuation of the executionprotected O fail(java.lang.Throwable eError, Continuation<?> rContinuation)
CompletableFuture.exceptionally(java.util.function.Function)
.eError
- The exception that occurredrContinuation
- The continuationprotected void terminateCoroutine(Continuation<?> rContinuation)
rContinuation
- The continuation of the current execution