Class GraphStageLogic
- Direct Known Subclasses:
GraphStageLogicWithLogging,KillSwitches.KillableGraphStageLogic,TimerGraphStageLogic
GraphStage. Roughly speaking, a subclass of GraphStageLogic is a
collection of the following parts:
* A set of InHandler and OutHandler instances and their assignments to the Inlets and Outlets
of the enclosing GraphStage
* Possible mutable state, accessible from the InHandler and OutHandler callbacks, but not from anywhere
else (as such access would not be thread-safe)
* The lifecycle hooks preStart() and postStop()
* Methods for performing stream processing actions, like pulling or pushing elements
The operator logic is completed once all its input and output ports have been closed. This can be changed by
setting setKeepGoing to true.
The postStop lifecycle hook on the logic itself is called once all ports are closed. This is the only tear down
callback that is guaranteed to happen, if the actor system or the materializer is terminated the handlers may never
see any callbacks to onUpstreamFailure, onUpstreamFinish or onDownstreamFinish. Therefore operator resource
cleanup should always be done in postStop.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInput handler that terminates the state upon receiving completion if the given condition holds at that time.static classOutput handler that terminates the state upon receiving completion if the given condition holds at that time.static classInput handler that terminates the operator upon receiving completion.static classOutput handler that terminates the operator upon cancellation.static classInput handler that does not terminate the operator upon receiving completion.static classOutput handler that does not terminate the operator upon cancellation.static final classMinimal actor to work with other actors and watch them in a synchronous waysstatic classstatic final classstatic classclassINTERNAL APIclassINTERNAL APIstatic classInput handler that does not terminate the operator upon receiving completion nor failure. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidabortEmitting(Outlet<?> out) Abort outstanding (suspended) emissions for the given outlet, if there are any.protected final voidabortReading(Inlet<?> in) Abort outstanding (suspended) reading for the given inlet, if there is any.protected voidINTERNAL APIprotected voidINTERNAL APIprotected final <T> voidRequests to stop receiving events from a given input port.protected final <T> voidRequests to stop receiving events from a given input port.final voidcancelStage(Throwable cause) Automatically invokes<T>cancel(org.apache.pekko.stream.Inlet<T>)or<T>complete(org.apache.pekko.stream.Outlet<T>)on all the input or output ports that have been called, then marks the stage as stopped.protected final <T> voidSignals that there will be no more elements emitted on the given port.final voidAutomatically invokes<T>cancel(org.apache.pekko.stream.Inlet<T>)or<T>complete(org.apache.pekko.stream.Outlet<T>)on all the input or output ports that have been called, then marks the operator as stopped.protected final InHandlerconditionalTerminateInput(scala.Function0<Object> predicate) Input handler that terminates the state upon receiving completion if the given condition holds at that time.protected final OutHandlerconditionalTerminateOutput(scala.Function0<Object> predicate) Output handler that terminates the state upon receiving completion if the given condition holds at that time.protected final <T> AsyncCallback<T>createAsyncCallback(Procedure<T> handler) Java API: Obtain a callback object that can be used asynchronously to re-enter the currentGraphStagewith an asynchronous notification.protected final InHandlerInput handler that terminates the operator upon receiving completion.protected final OutHandlerOutput handler that terminates the operator upon cancellation.protected final <T> voidEmit an element through the given outlet, suspending execution if necessary.protected final <T> voidprotected final <T> voidEmit an element through the given outlet and continue with the given thunk afterwards, suspending execution if necessary.protected final <T> voidemitMultiple(Outlet<T> out, Iterator<T> elems) Java APIprotected final <T> voidemitMultiple(Outlet<T> out, Iterator<T> elems, Effect andThen) Java APIprotected final <T> voidemitMultiple(Outlet<T> out, Spliterator<T> elems, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit a sequence of elements through the given outlet and continue with the given thunk afterwards, suspending execution if necessary.protected final <T> voidemitMultiple(Outlet<T> out, scala.collection.immutable.Iterable<T> elems) Emit a sequence of elements through the given outlet, suspending execution if necessary.protected final <T> voidemitMultiple(Outlet<T> out, scala.collection.immutable.Iterable<T> elems, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit a sequence of elements through the given outlet and continue with the given thunk afterwards, suspending execution if necessary.protected final <T> voidemitMultiple(Outlet<T> out, scala.collection.Iterator<T> elems) Emit a sequence of elements through the given outlet, suspending execution if necessary.protected final <T> voidemitMultiple(Outlet<T> out, scala.collection.Iterator<T> elems, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit a sequence of elements through the given outlet and continue with the given thunk afterwards, suspending execution if necessary.protected final <T> voidSignals failure through the given port.final voidAutomatically invokes<T>cancel(org.apache.pekko.stream.Inlet<T>)or<T>fail(org.apache.pekko.stream.Outlet<T>,java.lang.Throwable)on all the input or output ports that have been called, then marks the operator as stopped.final <T> AsyncCallback<T>getAsyncCallback(scala.Function1<T, scala.runtime.BoxedUnit> handler) Obtain a callback object that can be used asynchronously to re-enter the currentGraphStagewith an asynchronous notification.protected GraphStageLogic.StageActorgetEagerStageActor(Materializer eagerMaterializer, scala.Function1<scala.Tuple2<ActorRef, Object>, scala.runtime.BoxedUnit> receive) INTERNAL APIprotected final InHandlergetHandler(Inlet<?> in) Retrieves the current callback for the events on the givenInletprotected final OutHandlergetHandler(Outlet<?> out) Retrieves the current callback for the events on the givenOutletprotected final GraphStageLogic.StageActorgetStageActor(scala.Function1<scala.Tuple2<ActorRef, Object>, scala.runtime.BoxedUnit> receive) Initialize aGraphStageLogic.StageActorRefwhich can be used to interact with from the outside world "as-if" anpekko.actor.Actor.protected final <T> TOnce the callbackInHandler.onPushfor an input port has been invoked, the element that has been pushed can be retrieved via this method.protected final <T> booleanhasBeenPulled(Inlet<T> in) Indicates whether there is already a pending pull for the given input port.protected final InHandlerInput handler that does not terminate the operator upon receiving completion.protected final OutHandlerOutput handler that does not terminate the operator upon cancellation.intinCount()protected final <T> booleanisAvailable(Inlet<T> in) Indicates whether there is an element waiting at the given input port.final <T> booleanisAvailable(Outlet<T> out) Return true if the given output port is ready to be pushed.protected final <T> booleanIndicates whether the port has been closed.protected final <T> booleanIndicates whether the port has been closed.protected MaterializerThepekko.stream.Materializerthat has set this GraphStage in motion.intoutCount()protected final <Out,In extends Out>
voidInstall a handler on the given inlet that emits received elements on the given outlet before pulling for more data.protected final <Out,In extends Out>
booleanprotected final <Out,In extends Out>
booleanprotected final <Out,In extends Out>
booleanvoidpostStop()Invoked after processing of external events stopped because the operator is about to stop or fail.voidpreStart()Invoked before any external events are processed, at the startup of the operator.protected final <T> voidRequests an element on the given port.protected final <T> voidEmits an element through the given output port.protected final <T> voidJava API: Read an element from the given inlet and continue with the given function, suspending execution if necessary.protected final <T> voidread(Inlet<T> in, scala.Function1<T, scala.runtime.BoxedUnit> andThen, scala.Function0<scala.runtime.BoxedUnit> onClose) Read an element from the given inlet and continue with the given function, suspending execution if necessary.protected final <T> voidJava API: Read a number of elements from the given inlet and continue with the given function, suspending execution if necessary.protected final <T> voidreadN(Inlet<T> in, int n, scala.Function1<scala.collection.immutable.Seq<T>, scala.runtime.BoxedUnit> andThen, scala.Function1<scala.collection.immutable.Seq<T>, scala.runtime.BoxedUnit> onClose) Read a number of elements from the given inlet and continue with the given function, suspending execution if necessary.protected final voidsetHandler(Inlet<?> in, InHandler handler) Assigns callbacks for the events for anInletprotected final voidsetHandler(Outlet<?> out, OutHandler handler) Assigns callbacks for the events for anOutletprotected final voidsetHandlers(Inlet<?> in, Outlet<?> out, InHandler handler) Assign callbacks for linear operator for bothInletandOutletprotected final voidsetKeepGoing(boolean enabled) Controls whether this operator shall shut down when all its ports are closed, which is the default.protected StringOverride and return a name to be given to the StageActor of this operator.protected MaterializerAnpekko.stream.Materializerthat may run fusable parts of the graphs that it materializes within the same actor as the current GraphStage (if fusing is available).toString()protected final InHandlerInput handler that does not terminate the operator upon receiving completion nor failure.protected final <T> voidRequests an element on the given port unless the port is already closed.
-
Constructor Details
-
GraphStageLogic
-
-
Method Details
-
inCount
public int inCount() -
outCount
public int outCount() -
materializer
Thepekko.stream.Materializerthat has set this GraphStage in motion.Can not be used from a
GraphStageconstructor. Access to materializer is provided by thepekko.stream.scaladsl.Source.fromMaterializer,pekko.stream.scaladsl.Flow.fromMaterializerandpekko.stream.scaladsl.Sink.fromMaterializerand their corresponding Java API factories. -
subFusingMaterializer
Anpekko.stream.Materializerthat may run fusable parts of the graphs that it materializes within the same actor as the current GraphStage (if fusing is available). This materializer must not be shared outside of the GraphStage. -
eagerTerminateInput
Input handler that terminates the operator upon receiving completion. The operator fails upon receiving a failure. -
ignoreTerminateInput
Input handler that does not terminate the operator upon receiving completion. The operator fails upon receiving a failure. -
conditionalTerminateInput
Input handler that terminates the state upon receiving completion if the given condition holds at that time. The operator fails upon receiving a failure. -
totallyIgnorantInput
Input handler that does not terminate the operator upon receiving completion nor failure. -
eagerTerminateOutput
Output handler that terminates the operator upon cancellation. -
ignoreTerminateOutput
Output handler that does not terminate the operator upon cancellation. -
conditionalTerminateOutput
Output handler that terminates the state upon receiving completion if the given condition holds at that time. The operator fails upon receiving a failure. -
setHandler
Assigns callbacks for the events for anInlet -
setHandlers
Assign callbacks for linear operator for bothInletandOutlet -
getHandler
Retrieves the current callback for the events on the givenInlet -
setHandler
Assigns callbacks for the events for anOutlet -
getHandler
Retrieves the current callback for the events on the givenOutlet -
pull
Requests an element on the given port. Calling this method twice before an element arrived will fail. There can only be one outstanding request at any given time. The method<T>hasBeenPulled(org.apache.pekko.stream.Inlet<T>)can be used query whether pull is allowed to be called or not. This method will also fail if the port is already closed. -
tryPull
Requests an element on the given port unless the port is already closed. Calling this method twice before an element arrived will fail. There can only be one outstanding request at any given time. The method<T>hasBeenPulled(org.apache.pekko.stream.Inlet<T>)can be used query whether pull is allowed to be called or not. -
cancel
Requests to stop receiving events from a given input port. Cancelling clears any ungrabbed elements from the port.If cancellation is due to an error, use
cancel(in, cause)instead to propagate that cause upstream. This overload is a shortcut forcancel(in, SubscriptionWithCancelException.NoMoreElementsNeeded) -
cancel
Requests to stop receiving events from a given input port. Cancelling clears any ungrabbed elements from the port. -
grab
Once the callbackInHandler.onPushfor an input port has been invoked, the element that has been pushed can be retrieved via this method. After<T>grab(org.apache.pekko.stream.Inlet<T>)has been called the port is considered to be empty, and further calls to<T>grab(org.apache.pekko.stream.Inlet<T>)will fail until the port is pulled again and a new element is pushed as a response.The method
<T>isAvailable(org.apache.pekko.stream.Inlet<T>)can be used to query if the port has an element that can be grabbed or not. -
hasBeenPulled
Indicates whether there is already a pending pull for the given input port. If this method returns true then<T>isAvailable(org.apache.pekko.stream.Inlet<T>)must return false for that same port. -
isAvailable
Indicates whether there is an element waiting at the given input port.<T>grab(org.apache.pekko.stream.Inlet<T>)can be used to retrieve the element. After calling<T>grab(org.apache.pekko.stream.Inlet<T>)this method will return false.If this method returns true then
<T>hasBeenPulled(org.apache.pekko.stream.Inlet<T>)will return false for that same port. -
isClosed
Indicates whether the port has been closed. A closed port cannot be pulled. -
push
Emits an element through the given output port. Calling this method twice before a<T>pull(org.apache.pekko.stream.Inlet<T>)has been arrived will fail. There can be only one outstanding push request at any given time. The method<T>isAvailable(org.apache.pekko.stream.Inlet<T>)can be used to check if the port is ready to be pushed or not. -
setKeepGoing
protected final void setKeepGoing(boolean enabled) Controls whether this operator shall shut down when all its ports are closed, which is the default. In order to have it keep going past that point this method needs to be called with atrueargument before all ports are closed, and afterwards it will not be closed until this method is called with afalseargument or the operator is terminated viacompleteStage()orfailStage(). -
complete
Signals that there will be no more elements emitted on the given port. -
fail
Signals failure through the given port. -
completeStage
public final void completeStage()Automatically invokes<T>cancel(org.apache.pekko.stream.Inlet<T>)or<T>complete(org.apache.pekko.stream.Outlet<T>)on all the input or output ports that have been called, then marks the operator as stopped. -
cancelStage
Automatically invokes<T>cancel(org.apache.pekko.stream.Inlet<T>)or<T>complete(org.apache.pekko.stream.Outlet<T>)on all the input or output ports that have been called, then marks the stage as stopped. -
failStage
Automatically invokes<T>cancel(org.apache.pekko.stream.Inlet<T>)or<T>fail(org.apache.pekko.stream.Outlet<T>,java.lang.Throwable)on all the input or output ports that have been called, then marks the operator as stopped. -
isAvailable
Return true if the given output port is ready to be pushed. -
isClosed
Indicates whether the port has been closed. A closed port cannot be pushed. -
readN
protected final <T> void readN(Inlet<T> in, int n, scala.Function1<scala.collection.immutable.Seq<T>, scala.runtime.BoxedUnit> andThen, scala.Function1<scala.collection.immutable.Seq<T>, scala.runtime.BoxedUnit> onClose) Read a number of elements from the given inlet and continue with the given function, suspending execution if necessary. This action replaces theInHandlerfor the given inlet if suspension is needed and reinstalls the current handler upon receiving the lastonPush()signal.If upstream closes before N elements have been read, the
onClosefunction is invoked with the elements which were read. -
readN
protected final <T> void readN(Inlet<T> in, int n, Procedure<List<T>> andThen, Procedure<List<T>> onClose) Java API: Read a number of elements from the given inlet and continue with the given function, suspending execution if necessary. This action replaces theInHandlerfor the given inlet if suspension is needed and reinstalls the current handler upon receiving the lastonPush()signal (before invoking theandThenfunction). -
read
protected final <T> void read(Inlet<T> in, scala.Function1<T, scala.runtime.BoxedUnit> andThen, scala.Function0<scala.runtime.BoxedUnit> onClose) Read an element from the given inlet and continue with the given function, suspending execution if necessary. This action replaces theInHandlerfor the given inlet if suspension is needed and reinstalls the current handler upon receiving theonPush()signal (before invoking theandThenfunction). -
read
Java API: Read an element from the given inlet and continue with the given function, suspending execution if necessary. This action replaces theInHandlerfor the given inlet if suspension is needed and reinstalls the current handler upon receiving theonPush()signal (before invoking theandThenfunction). -
abortReading
Abort outstanding (suspended) reading for the given inlet, if there is any. This will reinstall the replaced handler that was in effect before thereadcall. -
emitMultiple
protected final <T> void emitMultiple(Outlet<T> out, scala.collection.immutable.Iterable<T> elems, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit a sequence of elements through the given outlet and continue with the given thunk afterwards, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal (before invoking theandThenfunction). -
emitMultiple
Emit a sequence of elements through the given outlet, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal. -
emitMultiple
Java APIEmit a sequence of elements through the given outlet, suspending execution if necessary. This action replaces the
AbstractOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal. -
emitMultiple
Java APIEmit a sequence of elements through the given outlet, suspending execution if necessary. This action replaces the
AbstractOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal. -
emitMultiple
protected final <T> void emitMultiple(Outlet<T> out, scala.collection.Iterator<T> elems, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit a sequence of elements through the given outlet and continue with the given thunk afterwards, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal (before invoking theandThenfunction). -
emitMultiple
protected final <T> void emitMultiple(Outlet<T> out, Spliterator<T> elems, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit a sequence of elements through the given outlet and continue with the given thunk afterwards, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal (before invoking theandThenfunction).- Since:
- 1.2.0
-
emitMultiple
Emit a sequence of elements through the given outlet, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal. -
emit
protected final <T> void emit(Outlet<T> out, T elem, scala.Function0<scala.runtime.BoxedUnit> andThen) Emit an element through the given outlet and continue with the given thunk afterwards, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal (before invoking theandThenfunction). -
emit
Emit an element through the given outlet, suspending execution if necessary. This action replaces theOutHandlerfor the given outlet if suspension is needed and reinstalls the current handler upon receiving anonPull()signal. -
emit
-
abortEmitting
Abort outstanding (suspended) emissions for the given outlet, if there are any. This will reinstall the replaced handler that was in effect before theemitcall. -
passAlong
protected final <Out,In extends Out> void passAlong(Inlet<In> from, Outlet<Out> to, boolean doFinish, boolean doFail, boolean doPull) Install a handler on the given inlet that emits received elements on the given outlet before pulling for more data.doFinishanddoFailcontrol whether completion or failure of the given inlet shall lead to operator termination or not.doPullinstructs to perform one initial pull on thefromport. -
passAlong$default$3
protected final <Out,In extends Out> boolean passAlong$default$3() -
passAlong$default$4
protected final <Out,In extends Out> boolean passAlong$default$4() -
passAlong$default$5
protected final <Out,In extends Out> boolean passAlong$default$5() -
getAsyncCallback
public final <T> AsyncCallback<T> getAsyncCallback(scala.Function1<T, scala.runtime.BoxedUnit> handler) Obtain a callback object that can be used asynchronously to re-enter the currentGraphStagewith an asynchronous notification. TheAsyncCallback.invokemethod of the returnedAsyncCallbackis safe to be called from other threads. It will in the background thread-safely delegate to the passed callback function. I.e.AsyncCallback.invokewill be called by other thread and the passed handler will be invoked eventually in a thread-safe way by the execution environment.In case stream is not yet materialized
AsyncCallbackwill buffer events until stream is available.AsyncCallback.invokeWithFeedbackhas an internal promise that will be failed if event cannot be processed due to stream completion.To be thread safe this method must only be called from either the constructor of the graph operator during materialization or one of the methods invoked by the graph operator machinery, such as
onPushandonPull.This object can be cached and reused within the same
GraphStageLogic. -
createAsyncCallback
Java API: Obtain a callback object that can be used asynchronously to re-enter the currentGraphStagewith an asynchronous notification. TheAsyncCallback.invokemethod of the returnedAsyncCallbackis safe to be called from other threads. It will in the background thread-safely delegate to the passed callback function. I.e.AsyncCallback.invokewill be called by other thread and the passed handler will be invoked eventually in a thread-safe way by the execution environment.AsyncCallback.invokeWithFeedbackhas an internal promise that will be failed if event cannot be processed due to stream completion.This object can be cached and reused within the same
GraphStageLogic. -
stageActor
-
getStageActor
protected final GraphStageLogic.StageActor getStageActor(scala.Function1<scala.Tuple2<ActorRef, Object>, scala.runtime.BoxedUnit> receive) Initialize aGraphStageLogic.StageActorRefwhich can be used to interact with from the outside world "as-if" anpekko.actor.Actor. The messages are looped through the<T>getAsyncCallback(scala.Function1<T,scala.runtime.BoxedUnit>)mechanism ofGraphStageso they are safe to modify internal state of this operator.This method must (the earliest) be called after the
GraphStageLogicconstructor has finished running, for example from thepreStart()callback the graph operator logic provides.Created
GraphStageLogic.StageActorRefto get messages and watch other actors in synchronous way.The
GraphStageLogic.StageActorRef's lifecycle is bound to the operator, in other words when the operator is finished, the Actor will be terminated as well. The entity backing theGraphStageLogic.StageActorRefis not a real Actor, but theGraphStageLogicitself, therefore it does not react topekko.actor.PoisonPill.To be thread safe this method must only be called from either the constructor of the graph operator during materialization or one of the methods invoked by the graph operator machinery, such as
onPushandonPull.- Parameters:
receive- callback that will be called upon receiving of a message by this special Actor- Returns:
- minimal actor with watch method
-
getEagerStageActor
protected GraphStageLogic.StageActor getEagerStageActor(Materializer eagerMaterializer, scala.Function1<scala.Tuple2<ActorRef, Object>, scala.runtime.BoxedUnit> receive) INTERNAL APITo be thread safe this method must only be called from either the constructor of the graph operator during materialization or one of the methods invoked by the graph operator machinery, such as
onPushandonPull. -
stageActorName
Override and return a name to be given to the StageActor of this operator.This method will be only invoked and used once, during the first
getStageActor(scala.Function1<scala.Tuple2<org.apache.pekko.actor.ActorRef,java.lang.Object>,scala.runtime.BoxedUnit>)invocation whichc reates the actor, since subsequentgetStageActorscalls function likebecome, rather than creating new actors.Returns an empty string by default, which means that the name will a unique generated String (e.g. "$$a").
-
beforePreStart
protected void beforePreStart()INTERNAL API -
afterPostStop
protected void afterPostStop()INTERNAL API -
preStart
Invoked before any external events are processed, at the startup of the operator.- Throws:
Exception
-
postStop
Invoked after processing of external events stopped because the operator is about to stop or fail.- Throws:
Exception
-
toString
-