Class Behaviors$
pekko.actor.typed.Behavior.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Behaviors$Static reference to the singleton instance of this Scala object. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Behavior<T>empty()A behavior that treats every incoming message as unhandled.<T> Behavior<T>ignore()A behavior that ignores every incoming message and returns “same”.<O,I> Behavior<O> intercept(scala.Function0<BehaviorInterceptor<O, I>> behaviorInterceptor, Behavior<I> behavior) Intercept messages and signals for abehaviorby first passing them to apekko.actor.typed.BehaviorInterceptor<T> Behavior<T>logMessages(Behavior<T> behavior) Behavior decorator that logs all messages to thepekko.actor.typed.Behaviorusing the providedpekko.actor.typed.LogOptionsdefault configuration before invoking the wrapped behavior.<T> Behavior<T>logMessages(LogOptions logOptions, Behavior<T> behavior) Behavior decorator that logs all messages to thepekko.actor.typed.Behaviorusing the providedpekko.actor.typed.LogOptionsconfiguration before invoking the wrapped behavior.<T> Behavior<T>Behavior decorator that copies all received message to the designated monitorpekko.actor.typed.ActorRefbefore invoking the wrapped behavior.<T> Behaviors.Receive<T>receive(scala.Function2<ActorContext<T>, T, Behavior<T>> onMessage) Construct an actor behavior that can react to both incoming messages and lifecycle signals.<T> Behaviors.Receive<T>receiveMessage(scala.Function1<T, Behavior<T>> onMessage) Simplified version ofReceivewith only a single argument - the message to be handled.<T> Behaviors.Receive<T>receiveMessagePartial(scala.PartialFunction<T, Behavior<T>> onMessage) Construct an actorBehaviorfrom a partial message handler which treats undefined messages as unhandled.<T> Behaviors.Receive<T>receiveMessageWithSame(scala.Function1<T, scala.runtime.BoxedUnit> onMessage) Simplified version of<T>receiveMessage(scala.Function1<T,org.apache.pekko.actor.typed.Behavior<T>>)with only a single argument - the message to be handled, but it doesn't produce a return value of next behavior.<T> Behaviors.Receive<T>receivePartial(scala.PartialFunction<scala.Tuple2<ActorContext<T>, T>, Behavior<T>> onMessage) Construct an actorBehaviorfrom a partial message handler which treats undefined messages as unhandled.<T> Behavior<T>receiveSignal(scala.PartialFunction<scala.Tuple2<ActorContext<T>, Signal>, Behavior<T>> handler) Construct an actorBehaviorthat can react to lifecycle signals only.<T> Behavior<T>same()Return this behavior from message processing in order to advise the system to reuse the previous behavior.<T> Behavior<T>setup(scala.Function1<ActorContext<T>, Behavior<T>> factory) setupis a factory for a behavior.<T> Behavior<T>stopped()Return this behavior from message processing to signal that this actor shall terminate voluntarily.<T> Behavior<T>stopped(scala.Function0<scala.runtime.BoxedUnit> postStop) Return this behavior from message processing to signal that this actor shall terminate voluntarily.<T> Behaviors.Supervise<T>Wrap the given behavior with the givenSupervisorStrategyfor the given exception.<T> Behavior<T>Return this behavior from message processing in order to advise the system to reuse the previous behavior, including the hint that the message has not been handled.<T> Behavior<T>withMdc(scala.collection.immutable.Map<String, String> staticMdc, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$3) Static MDC (Mapped Diagnostic Context)<T> Behavior<T>withMdc(scala.collection.immutable.Map<String, String> staticMdc, scala.Function1<T, scala.collection.immutable.Map<String, String>> mdcForMessage, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$4) Combination of static and per message MDC (Mapped Diagnostic Context).<T> Behavior<T>withMdc(scala.Function1<T, scala.collection.immutable.Map<String, String>> mdcForMessage, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$2) Per message MDC (Mapped Diagnostic Context) logging.<T> Behavior<T>withStash(int capacity, scala.Function1<StashBuffer<T>, Behavior<T>> factory) Support for stashing messages to unstash at a later time.<T> Behavior<T>withTimers(scala.Function1<TimerScheduler<T>, Behavior<T>> factory) Support for scheduledselfmessages in an actor.
-
Field Details
-
MODULE$
Static reference to the singleton instance of this Scala object.
-
-
Constructor Details
-
Behaviors$
public Behaviors$()
-
-
Method Details
-
setup
setupis a factory for a behavior. Creation of the behavior instance is deferred until the actor is started, as opposed toBehaviors.receivethat creates the behavior instance immediately before the actor is running. Thefactoryfunction pass theActorContextas parameter and that can for example be used for spawning child actors.setupis typically used as the outer most behavior when spawning an actor, but it can also be returned as the next behavior when processing a message or signal. In that case it will be started immediately after it is returned, i.e. next message will be processed by the started behavior. -
withStash
Support for stashing messages to unstash at a later time. -
same
Return this behavior from message processing in order to advise the system to reuse the previous behavior. This is provided in order to avoid the allocation overhead of recreating the current behavior where that is not necessary. -
unhandled
Return this behavior from message processing in order to advise the system to reuse the previous behavior, including the hint that the message has not been handled. This hint may be used by composite behaviors that delegate (partial) handling to other behaviors. -
stopped
Return this behavior from message processing to signal that this actor shall terminate voluntarily. If this actor has created child actors then these will be stopped as part of the shutdown procedure.The
PostStopsignal that results from stopping this actor will be passed to the current behavior. All other messages and signals will effectively be ignored. -
stopped
Return this behavior from message processing to signal that this actor shall terminate voluntarily. If this actor has created child actors then these will be stopped as part of the shutdown procedure.The
PostStopsignal that results from stopping this actor will first be passed to the current behavior and then the providedpostStopcallback will be invoked. All other messages and signals will effectively be ignored.An example of when the callback can be useful compared to the
PostStopsignal if you want to send a reply to the message that initiated a graceful stop. -
empty
A behavior that treats every incoming message as unhandled. -
ignore
A behavior that ignores every incoming message and returns “same”. -
receive
Construct an actor behavior that can react to both incoming messages and lifecycle signals. After spawning this actor from another actor (or as the guardian of anpekko.actor.typed.ActorSystem) it will be executed within anActorContextthat allows access to the system, spawning and watching other actors, etc.Compared to using
AbstractBehaviorthis factory is a more functional style of defining theBehavior. Processing the next message results in a new behavior that can potentially be different from this one. State is maintained by returning a new behavior that holds the new immutable state. -
receiveMessage
Simplified version ofReceivewith only a single argument - the message to be handled. Useful for when the context is already accessible by other means, like being wrapped in an<T>setup(scala.Function1<org.apache.pekko.actor.typed.scaladsl.ActorContext<T>,org.apache.pekko.actor.typed.Behavior<T>>)or similar.Construct an actor behavior that can react to both incoming messages and lifecycle signals. After spawning this actor from another actor (or as the guardian of an
pekko.actor.typed.ActorSystem) it will be executed within anActorContextthat allows access to the system, spawning and watching other actors, etc.Compared to using
AbstractBehaviorthis factory is a more functional style of defining theBehavior. Processing the next message results in a new behavior that can potentially be different from this one. State is maintained by returning a new behavior that holds the new immutable state. -
receiveMessageWithSame
public <T> Behaviors.Receive<T> receiveMessageWithSame(scala.Function1<T, scala.runtime.BoxedUnit> onMessage) Simplified version of<T>receiveMessage(scala.Function1<T,org.apache.pekko.actor.typed.Behavior<T>>)with only a single argument - the message to be handled, but it doesn't produce a return value of next behavior. Useful for when the behavior doesn't want to change in runtime.Construct an actor behavior that can react to incoming messages but not to lifecycle signals. After spawning this actor from another actor (or as the guardian of an
pekko.actor.typed.ActorSystem) it will be executed within anActorContextthat allows access to the system, spawning and watching other actors, etc.Compared to using
AbstractBehaviorthis factory is a more functional style of defining theBehavior. Processing the next message will not result in different behavior than this one- Since:
- 1.1.0
-
receivePartial
public <T> Behaviors.Receive<T> receivePartial(scala.PartialFunction<scala.Tuple2<ActorContext<T>, T>, Behavior<T>> onMessage) Construct an actorBehaviorfrom a partial message handler which treats undefined messages as unhandled. -
receiveMessagePartial
public <T> Behaviors.Receive<T> receiveMessagePartial(scala.PartialFunction<T, Behavior<T>> onMessage) Construct an actorBehaviorfrom a partial message handler which treats undefined messages as unhandled. -
receiveSignal
public <T> Behavior<T> receiveSignal(scala.PartialFunction<scala.Tuple2<ActorContext<T>, Signal>, Behavior<T>> handler) Construct an actorBehaviorthat can react to lifecycle signals only. -
intercept
public <O,I> Behavior<O> intercept(scala.Function0<BehaviorInterceptor<O, I>> behaviorInterceptor, Behavior<I> behavior) Intercept messages and signals for abehaviorby first passing them to apekko.actor.typed.BehaviorInterceptorWhen a behavior returns a new behavior as a result of processing a signal or message and that behavior already contains the same interceptor (defined by the
isSamemethod on theBehaviorInterceptor) only the innermost interceptor is kept. This is to protect against stack overflow when recursively defining behaviors.The interceptor is created with a factory function in case it has state and should not be shared. If the interceptor has no state the same instance can be returned from the factory to avoid unnecessary object creation.
-
monitor
public <T> Behavior<T> monitor(ActorRef<T> monitor, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$1) Behavior decorator that copies all received message to the designated monitorpekko.actor.typed.ActorRefbefore invoking the wrapped behavior. The wrapped behavior can evolve (i.e. return different behavior) without needing to be wrapped in amonitorcall again.The
ClassTagforTensures that the messages of this class or a subclass thereof will be sent to themonitor. Other message types (e.g. a private protocol) will bypass the interceptor and be continue to the inner behavior.- Parameters:
monitor- The messages will also be sent to thisActorRefbehavior- The inner behavior that is decorated
-
logMessages
Behavior decorator that logs all messages to thepekko.actor.typed.Behaviorusing the providedpekko.actor.typed.LogOptionsdefault configuration before invoking the wrapped behavior. To include an MDC context then first wraplogMessageswithwithMDC. -
logMessages
Behavior decorator that logs all messages to thepekko.actor.typed.Behaviorusing the providedpekko.actor.typed.LogOptionsconfiguration before invoking the wrapped behavior. To include an MDC context then first wraplogMessageswithwithMDC. -
supervise
Wrap the given behavior with the givenSupervisorStrategyfor the given exception. Exceptions that are not subtypes ofThrwill not be caught and thus lead to the termination of the actor.It is possible to specify different supervisor strategies, such as restart, resume, backoff.
Note that only
NonFatalthrowables will trigger the supervision strategy.Example:
val dbConnector: Behavior[DbCommand] = ... val dbRestarts = Behaviors.supervise(dbConnector) .onFailure(SupervisorStrategy.restart) // handle all NonFatal exceptions val dbSpecificResumes = Behaviors.supervise(dbConnector) .onFailure[IndexOutOfBoundsException](SupervisorStrategy.resume) // resume for IndexOutOfBoundsException exceptions -
withTimers
Support for scheduledselfmessages in an actor. It takes care of the lifecycle of the timers such as cancelling them when the actor is restarted or stopped.- See Also:
-
withMdc
public <T> Behavior<T> withMdc(scala.Function1<T, scala.collection.immutable.Map<String, String>> mdcForMessage, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$2) Per message MDC (Mapped Diagnostic Context) logging.The
ClassTagforTensures that only messages of this class or a subclass thereof will be intercepted. Other message types (e.g. a private protocol) will bypass the interceptor and be continue to the inner behavior untouched.- Parameters:
mdcForMessage- Is invoked before each message is handled, allowing to setup MDC, MDC is cleared after each message processing by the inner behavior is done.behavior- The actual behavior handling the messages, the MDC is used for the log entries logged throughActorContext.log
-
withMdc
public <T> Behavior<T> withMdc(scala.collection.immutable.Map<String, String> staticMdc, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$3) Static MDC (Mapped Diagnostic Context)The
ClassTagforTensures that only messages of this class or a subclass thereof will be intercepted. Other message types (e.g. a private protocol) will bypass the interceptor and be continue to the inner behavior untouched.- Parameters:
staticMdc- This MDC is setup in the logging context for every messagebehavior- The actual behavior handling the messages, the MDC is used for the log entries logged throughActorContext.log
-
withMdc
public <T> Behavior<T> withMdc(scala.collection.immutable.Map<String, String> staticMdc, scala.Function1<T, scala.collection.immutable.Map<String, String>> mdcForMessage, Behavior<T> behavior, scala.reflect.ClassTag<T> evidence$4) Combination of static and per message MDC (Mapped Diagnostic Context).Each message will get the static MDC plus the MDC returned for the message. If the same key are in both the static and the per message MDC the per message one overwrites the static one in the resulting log entries.
The
staticMdcormdcForMessagemay be empty.The
ClassTagforTensures that only messages of this class or a subclass thereof will be intercepted. Other message types (e.g. a private protocol) will bypass the interceptor and be continue to the inner behavior untouched.- Parameters:
staticMdc- A static MDC applied for each messagemdcForMessage- Is invoked before each message is handled, allowing to setup MDC, MDC is cleared after each message processing by the inner behavior is done.behavior- The actual behavior handling the messages, the MDC is used for the log entries logged throughActorContext.log
-