Class AbstractActor

java.lang.Object
org.apache.pekko.actor.AbstractActor
All Implemented Interfaces:
Actor
Direct Known Subclasses:
AbstractActorWithStash, AbstractActorWithTimers, AbstractActorWithUnboundedStash, AbstractActorWithUnrestrictedStash, AbstractLoggingActor, AbstractPersistentActor, AbstractPersistentActorWithTimers, UntypedAbstractActor

public abstract class AbstractActor extends Object implements Actor
Java API: compatible with lambda expressions

Actor base class that should be extended to create Java actors that use lambdas.

Example:
 public class MyActorForJavaDoc extends AbstractActor{
    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(Double.class, d -> {
                    sender().tell(d.isNaN() ? 0 : d, self());
                })
                .match(Integer.class, i -> {
                    sender().tell(i * 10, self());
                })
                .match(String.class, s -> s.startsWith("foo"), s -> {
                    sender().tell(s.toUpperCase(), self());
                })
                .build();
    }
 }
 
  • Constructor Details

    • AbstractActor

      public AbstractActor()
  • Method Details

    • emptyBehavior

      public static final AbstractActor.Receive emptyBehavior()
      emptyBehavior is a Receive-expression that matches no messages at all, ever.
    • context

      public ActorContext context()
      Description copied from interface: Actor
      Scala API: Stores the context for this actor, including self, and sender. It is implicit to support operations such as forward.

      WARNING: Only valid within the Actor itself, so do not close over it and publish it to other threads!

      pekko.actor.ActorContext is the Scala API. getContext returns a pekko.actor.AbstractActor.ActorContext, which is the Java API of the actor context.

      Specified by:
      context in interface Actor
    • self

      public final ActorRef self()
      Description copied from interface: Actor
      The 'self' field holds the ActorRef for this actor.

      Can be used to send messages to itself:
       self ! message
       
      Specified by:
      self in interface Actor
    • org$apache$pekko$actor$Actor$_setter_$context_$eq

      protected void org$apache$pekko$actor$Actor$_setter_$context_$eq(ActorContext x$1)
      Description copied from interface: Actor
      Scala API: Stores the context for this actor, including self, and sender. It is implicit to support operations such as forward.

      WARNING: Only valid within the Actor itself, so do not close over it and publish it to other threads!

      pekko.actor.ActorContext is the Scala API. getContext returns a pekko.actor.AbstractActor.ActorContext, which is the Java API of the actor context.

      Specified by:
      org$apache$pekko$actor$Actor$_setter_$context_$eq in interface Actor
    • org$apache$pekko$actor$Actor$_setter_$self_$eq

      protected final void org$apache$pekko$actor$Actor$_setter_$self_$eq(ActorRef x$1)
      Description copied from interface: Actor
      The 'self' field holds the ActorRef for this actor.

      Can be used to send messages to itself:
       self ! message
       
      Specified by:
      org$apache$pekko$actor$Actor$_setter_$self_$eq in interface Actor
    • getContext

      public AbstractActor.ActorContext getContext()
      Returns this AbstractActor's ActorContext The ActorContext is not thread safe so do not expose it outside of the AbstractActor.
    • getSelf

      public ActorRef getSelf()
      Returns the ActorRef for this actor.

      Same as self().

    • getSender

      public ActorRef getSender()
      The reference sender Actor of the currently processed message. This is always a legal destination to send to, even if there is no logical recipient for the reply, in which case it will be sent to the dead letter mailbox.

      Same as sender().

      WARNING: Only valid within the Actor itself, so do not close over it and publish it to other threads!

    • supervisorStrategy

      public SupervisorStrategy supervisorStrategy()
      User overridable definition the strategy to use for supervising child actors.
      Specified by:
      supervisorStrategy in interface Actor
    • preStart

      public void preStart() throws Exception
      User overridable callback.

      Is called when an Actor is started. Actor are automatically started asynchronously when created. Empty default implementation.
      Specified by:
      preStart in interface Actor
      Throws:
      Exception
    • postStop

      public void postStop() throws Exception
      User overridable callback.

      Is called asynchronously after getContext().stop() is invoked. Empty default implementation.
      Specified by:
      postStop in interface Actor
      Throws:
      Exception
    • preRestart

      public void preRestart(Throwable reason, scala.Option<Object> message) throws Exception
      Deprecated.
      Override preRestart with message parameter with Optional type instead. Since Akka 2.5.0.
      Description copied from interface: Actor
      Scala API: User overridable callback: '''By default it disposes of all children and then calls postStop().'''
      Specified by:
      preRestart in interface Actor
      Parameters:
      reason - the Throwable that caused the restart to happen
      message - optionally the current message the actor processed when failing, if applicable

      Is called on a crashed Actor right BEFORE it is restarted to allow clean up of resources before Actor is terminated.
      Throws:
      Exception
    • preRestart

      public void preRestart(Throwable reason, Optional<Object> message) throws Exception
      User overridable callback: '''By default it disposes of all children and then calls postStop().'''

      Is called on a crashed Actor right BEFORE it is restarted to allow clean up of resources before Actor is terminated.
      Throws:
      Exception
    • postRestart

      public void postRestart(Throwable reason) throws Exception
      User overridable callback: By default it calls preStart().

      Is called right AFTER restart on the newly created Actor to allow reinitialization after an Actor crash.
      Specified by:
      postRestart in interface Actor
      Parameters:
      reason - the Throwable that caused the restart to happen

      Is called right AFTER restart on the newly created Actor to allow reinitialization after an Actor crash.
      Throws:
      Exception
    • createReceive

      public abstract AbstractActor.Receive createReceive()
      An actor has to define its initial receive behavior by implementing the createReceive method.
    • receive

      public scala.PartialFunction<Object,scala.runtime.BoxedUnit> receive()
      Description copied from interface: Actor
      Scala API: This defines the initial actor behavior, it must return a partial function with the actor logic.
      Specified by:
      receive in interface Actor
    • receiveBuilder

      public final ReceiveBuilder receiveBuilder()
      Convenience factory of the ReceiveBuilder. Creates a new empty ReceiveBuilder.