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.scaladsl.AbstractBehavior<T>
Direct Known Subclasses:
InitialGroupRouterImpl, PoolRouterImpl

public abstract class AbstractBehavior<T> extends ExtensibleBehavior<T>
An actor Behavior can be implemented by extending this class and implement the abstract method onMessage(T) and optionally override onSignal(). 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 in Behaviors, for example Behaviors.receiveMessage.

Instances of this behavior should be created via Behaviors.setup and the ActorContext 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 AbstractBehavior the original ActorContext can be used as the context parameter instead of wrapping in a new Behaviors.setup, but it wouldn't be wrong to use context from Behaviors.setup since that is the same ActorContext 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 throw IllegalStateException when the first message is received.

See Also:
  • Behaviors.setup
  • Constructor Details

    • AbstractBehavior

      public AbstractBehavior(ActorContext<T> context)
  • Method Details

    • context

      protected ActorContext<T> context()
    • onMessage

      public abstract Behavior<T> onMessage(T msg) throws Exception
      Implement this method to 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
      • returning this or same designates to reuse the current Behavior
      • returning unhandled keeps the same Behavior and signals that the message was not yet handled
      Throws:
      Exception
    • onSignal

      public scala.PartialFunction<Signal,Behavior<T>> onSignal() throws Exception
      Override this method to process an incoming pekko.actor.typed.Signal 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 * returning this or same designates to reuse the current Behavior * returning unhandled keeps the same Behavior and signals that the message was not yet handled

      By default, partial function is empty and does not handle any signals.

      Throws:
      Exception
    • receive

      public final Behavior<T> receive(TypedActorContext<T> ctx, T msg) throws 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 * returning same designates to reuse the current Behavior * returning unhandled keeps the same Behavior and signals that the message was not yet handled

      Code calling this method should use Behavior$ canonicalize to replace the special objects with real Behaviors.

      Specified by:
      receive in class ExtensibleBehavior<T>
      Throws:
      Exception
    • receiveSignal

      public final Behavior<T> receiveSignal(TypedActorContext<T> ctx, Signal msg) throws Exception
      Description copied from class: ExtensibleBehavior
      Process an incoming Signal 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 * returning same designates to reuse the current Behavior * returning unhandled keeps the same Behavior and signals that the message was not yet handled

      Code calling this method should use Behavior$ canonicalize to replace the special objects with real Behaviors.

      Specified by:
      receiveSignal in class ExtensibleBehavior<T>
      Throws:
      Exception