Class AbstractBehavior<T>
- java.lang.Object
-
- org.apache.pekko.actor.typed.Behavior<T>
-
- org.apache.pekko.actor.typed.ExtensibleBehavior<T>
-
- org.apache.pekko.actor.typed.javadsl.AbstractBehavior<T>
-
public abstract class AbstractBehavior<T> extends ExtensibleBehavior<T>
An actorBehavior
can be implemented by extending this class and implement the abstract methodcreateReceive()
. Mutable state can be defined as instance variables of the class.This is an Object-oriented style of defining a
Behavior
. A more functional style alternative is provided by the factory methods inBehaviors
, for exampleBehaviors.receiveMessage
.Instances of this behavior should be created via
Behaviors.setup
and theActorContext
should be passed as a constructor parameter from the factory function. This is important because a new instance should be created when restart supervision is used.When switching
Behavior
to anotherAbstractBehavior
the originalActorContext
can be used as thecontext
parameter instead of wrapping in a newBehaviors.setup
, but it wouldn't be wrong to usecontext
fromBehaviors.setup
since that is the sameActorContext
instance.It must not be created with an
ActorContext
of another actor, such as the parent actor. Such mistake will be detected at runtime and throwIllegalStateException
when the first message is received.- See Also:
Behaviors.setup
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.pekko.actor.typed.Behavior
Behavior.BehaviorDecorators<Inner>, Behavior.BehaviorDecorators$
-
-
Constructor Summary
Constructors Constructor Description AbstractBehavior(ActorContext<T> context)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Receive<T>
createReceive()
Implement this to define how messages and signals are processed.protected ActorContext<T>
getContext()
protected ReceiveBuilder<T>
newReceiveBuilder()
Create a newReceiveBuilder
to define the message dispatch of theBehavior
.Behavior<T>
receive(TypedActorContext<T> ctx, T msg)
Process an incoming message and return the next behavior.Behavior<T>
receiveSignal(TypedActorContext<T> ctx, Signal msg)
Process an incomingSignal
and return the next behavior.-
Methods inherited from class org.apache.pekko.actor.typed.Behavior
BehaviorDecorators, canonicalize, existsInStack, interpretMessage, interpretSignal, isAlive, isDeferred, isUnhandled, narrow, start, validateAsInitial
-
-
-
-
Constructor Detail
-
AbstractBehavior
public AbstractBehavior(ActorContext<T> context)
-
-
Method Detail
-
createReceive
protected abstract Receive<T> createReceive()
Implement this to define how messages and signals are processed. Use theAbstractBehavior.newReceiveBuilder
to define the message dispatch.
-
getContext
protected ActorContext<T> getContext()
-
newReceiveBuilder
protected ReceiveBuilder<T> newReceiveBuilder()
Create a newReceiveBuilder
to define the message dispatch of theBehavior
. Typically used fromAbstractBehavior.createReceive
.
-
receive
public final Behavior<T> receive(TypedActorContext<T> ctx, T msg) throws java.lang.Exception
Description copied from class:ExtensibleBehavior
Process an incoming message and return the next behavior.The returned behavior can in addition to normal behaviors be one of the canned special objects:
* returning
stopped
will terminate this Behavior * returningsame
designates to reuse the current Behavior * returningunhandled
keeps the same Behavior and signals that the message was not yet handledCode calling this method should use
Behavior$
canonicalize
to replace the special objects with real Behaviors.- Specified by:
receive
in classExtensibleBehavior<T>
- Throws:
java.lang.Exception
-
receiveSignal
public final Behavior<T> receiveSignal(TypedActorContext<T> ctx, Signal msg) throws java.lang.Exception
Description copied from class:ExtensibleBehavior
Process an incomingSignal
and return the next behavior. This means that all lifecycle hooks, ReceiveTimeout, Terminated and Failed messages can initiate a behavior change.The returned behavior can in addition to normal behaviors be one of the canned special objects:
* returning
stopped
will terminate this Behavior * returningsame
designates to reuse the current Behavior * returningunhandled
keeps the same Behavior and signals that the message was not yet handledCode calling this method should use
Behavior$
canonicalize
to replace the special objects with real Behaviors.- Specified by:
receiveSignal
in classExtensibleBehavior<T>
- Throws:
java.lang.Exception
-
-