Class AbstractOnMessageBehavior<T>
- java.lang.Object
-
- akka.actor.typed.Behavior<T>
-
- akka.actor.typed.ExtensibleBehavior<T>
-
- akka.actor.typed.javadsl.AbstractOnMessageBehavior<T>
-
public abstract class AbstractOnMessageBehavior<T> extends ExtensibleBehavior<T>
An actorBehavior
can be implemented by extending this class and implementing the abstract methodonMessage(T)
. 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
.An alternative object-oriented style is found in
AbstractBehavior
, which uses builders to define theBehavior
. In contrast to extendingAbstractBehavior
, extending this class should have reduced overhead, though depending on the complexity of the protocol handled by this actor and on the Java version in use, theonMessage
andonSignal
methods may be overly complex.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 another behavior which requires a context, the original
ActorContext
can be used or aBehaviors.setup
can be used: either will end up using the sameActorContext
instance.It must not be created with an
ActorContext
of another actor (e.g. the parent actor). Doing so will be detected at runtime and throw anIllegalStateException
when the first message is received.- See Also:
Behaviors.setup
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class akka.actor.typed.Behavior
Behavior.BehaviorDecorators<Inner>, Behavior.BehaviorDecorators$
-
-
Constructor Summary
Constructors Constructor Description AbstractOnMessageBehavior(ActorContext<T> context)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected ActorContext<T>
getContext()
abstract Behavior<T>
onMessage(T message)
Implement this to define how messages are processed.Behavior<T>
onSignal(Signal signal)
Override this to handle a signal.Behavior<T>
receive(TypedActorContext<T> ctx, T msg)
Process an incoming message and return the next behavior.Behavior<T>
receiveSignal(TypedActorContext<T> ctx, Signal signal)
Process an incomingSignal
and return the next behavior.-
Methods inherited from class akka.actor.typed.Behavior
BehaviorDecorators, canonicalize, existsInStack, interpretMessage, interpretSignal, isAlive, isDeferred, isUnhandled, narrow, start, validateAsInitial
-
-
-
-
Constructor Detail
-
AbstractOnMessageBehavior
public AbstractOnMessageBehavior(ActorContext<T> context)
-
-
Method Detail
-
getContext
protected final ActorContext<T> getContext()
-
onMessage
public abstract Behavior<T> onMessage(T message) throws java.lang.Exception
Implement this to define how messages are processed. To indicate no change in behavior beyond changes due to updating instance variables of this class, one may return eitherthis
orBehaviors.same
.- Throws:
java.lang.Exception
-
onSignal
public Behavior<T> onSignal(Signal signal) throws java.lang.Exception
Override this to handle a signal. The default implementation handles onlyMessageAdaptionFailure
and otherwise ignores the signal.- Throws:
java.lang.Exception
-
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 signal) 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
-
-