Class Behaviors
- java.lang.Object
-
- akka.actor.typed.javadsl.Behaviors
-
public class Behaviors extends java.lang.Object
Factories forBehavior
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Behaviors.Supervise<T>
-
Constructor Summary
Constructors Constructor Description Behaviors()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Behavior<T>
empty()
A behavior that treats every incoming message as unhandled.static <T> Behavior<T>
ignore()
A behavior that ignores every incoming message and returns “same”.static <O,I>
Behavior<O>intercept(java.util.function.Supplier<BehaviorInterceptor<O,I>> behaviorInterceptor, Behavior<I> behavior)
Intercept messages and signals for abehavior
by first passing them to aBehaviorInterceptor
static <T> Behavior<T>
logMessages(Behavior<T> behavior)
Behavior decorator that logs all messages to theBehavior
using the providedLogOptions
default configuration before invoking the wrapped behavior.static <T> Behavior<T>
logMessages(LogOptions logOptions, Behavior<T> behavior)
Behavior decorator that logs all messages to theBehavior
using the providedLogOptions
configuration before invoking the wrapped behavior.static <T> Behavior<T>
monitor(java.lang.Class<T> interceptMessageClass, ActorRef<T> monitor, Behavior<T> behavior)
Behavior decorator that copies all received message to the designated monitorActorRef
before invoking the wrapped behavior.static <T> Behavior<T>
receive(Function2<ActorContext<T>,T,Behavior<T>> onMessage)
Construct an actor behavior that can react to incoming messages but not to lifecycle signals.static <T> Behavior<T>
receive(Function2<ActorContext<T>,T,Behavior<T>> onMessage, Function2<ActorContext<T>,Signal,Behavior<T>> onSignal)
Construct an actor behavior that can react to both incoming messages and lifecycle signals.static <T> BehaviorBuilder<T>
receive(java.lang.Class<T> type)
Constructs an actor behavior builder that can build a behavior that can react to both incoming messages and lifecycle signals.static <T> Behavior<T>
receiveMessage(Function<T,Behavior<T>> onMessage)
Simplified version of<T>receive(akka.japi.function.Function2<akka.actor.typed.javadsl.ActorContext<T>,T,akka.actor.typed.Behavior<T>>)
with only a single argument - the message to be handled.static <T> Behavior<T>
receiveSignal(Function2<ActorContext<T>,Signal,Behavior<T>> handler)
Construct an actor behavior that can react to lifecycle signals only.static <T> Behavior<T>
same()
Return this behavior from message processing in order to advise the system to reuse the previous behavior.static <T> Behavior<T>
setup(Function<ActorContext<T>,Behavior<T>> factory)
setup
is a factory for a behavior.static <T> Behavior<T>
stopped()
Return this behavior from message processing to signal that this actor shall terminate voluntarily.static <T> Behavior<T>
stopped(Effect postStop)
Return this behavior from message processing to signal that this actor shall terminate voluntarily.static <T> Behaviors.Supervise<T>
supervise(Behavior<T> wrapped)
Wrap the given behavior such that it is restarted (i.e.static <Outer,Inner>
Behavior<Outer>transformMessages(java.lang.Class<Outer> interceptMessageClass, Behavior<Inner> behavior, java.util.function.Function<PFBuilder<Outer,Inner>,PFBuilder<Outer,Inner>> selector)
Transform the incoming messages by placing a funnel in front of the wrappedBehavior
: the supplied PartialFunction decides which message to pull in (those that it is defined at) and may transform the incoming message to place them into the wrapped Behavior’s type hierarchy.static <T> Behavior<T>
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.static <T> Behavior<T>
withMdc(java.lang.Class<T> interceptMessageClass, Function<T,java.util.Map<java.lang.String,java.lang.String>> mdcForMessage, Behavior<T> behavior)
Per message MDC (Mapped Diagnostic Context) logging.static <T> Behavior<T>
withMdc(java.lang.Class<T> interceptMessageClass, java.util.Map<java.lang.String,java.lang.String> staticMdc, Behavior<T> behavior)
Static MDC (Mapped Diagnostic Context)static <T> Behavior<T>
withMdc(java.lang.Class<T> interceptMessageClass, java.util.Map<java.lang.String,java.lang.String> staticMdc, Function<T,java.util.Map<java.lang.String,java.lang.String>> mdcForMessage, Behavior<T> behavior)
Combination of static and per message MDC (Mapped Diagnostic Context).static <T> Behavior<T>
withStash(int capacity, java.util.function.Function<StashBuffer<T>,Behavior<T>> factory)
Support for stashing messages to unstash at a later time.static <T> Behavior<T>
withTimers(Function<TimerScheduler<T>,Behavior<T>> factory)
Support for scheduledself
messages in an actor.
-
-
-
Method Detail
-
setup
public static <T> Behavior<T> setup(Function<ActorContext<T>,Behavior<T>> factory)
setup
is a factory for a behavior. Creation of the behavior instance is deferred until the actor is started, as opposed toreceive(akka.japi.function.Function2<akka.actor.typed.javadsl.ActorContext<T>, T, akka.actor.typed.Behavior<T>>)
that creates the behavior instance immediately before the actor is running. Thefactory
function pass theActorContext
as parameter and that can for example be used for spawning child actors.setup
is 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
public static <T> Behavior<T> withStash(int capacity, java.util.function.Function<StashBuffer<T>,Behavior<T>> factory)
Support for stashing messages to unstash at a later time.
-
same
public static <T> Behavior<T> 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
public static <T> Behavior<T> 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
public static <T> Behavior<T> 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
PostStop
signal that results from stopping this actor will be passed to the current behavior. All other messages and signals will effectively be ignored.
-
stopped
public static <T> Behavior<T> stopped(Effect postStop)
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
PostStop
signal that results from stopping this actor will first be passed to the current behavior and then the providedpostStop
callback will be invoked. All other messages and signals will effectively be ignored.An example of when the callback can be useful compared to the
PostStop
signal if you want to send a reply to the message that initiated a graceful stop.
-
empty
public static <T> Behavior<T> empty()
A behavior that treats every incoming message as unhandled.
-
ignore
public static <T> Behavior<T> ignore()
A behavior that ignores every incoming message and returns “same”.
-
receive
public static <T> Behavior<T> receive(Function2<ActorContext<T>,T,Behavior<T>> onMessage)
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 anActorSystem
) it will be executed within anActorContext
that allows access to the system, spawning and watching other actors, etc.Compared to using
AbstractBehavior
this 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
public static <T> Behavior<T> receiveMessage(Function<T,Behavior<T>> onMessage)
Simplified version of<T>receive(akka.japi.function.Function2<akka.actor.typed.javadsl.ActorContext<T>,T,akka.actor.typed.Behavior<T>>)
with 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(akka.japi.function.Function<akka.actor.typed.javadsl.ActorContext<T>,akka.actor.typed.Behavior<T>>)
or similar.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
ActorSystem
) it will be executed within anActorContext
that allows access to the system, spawning and watching other actors, etc.Compared to using
AbstractBehavior
this 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.
-
receive
public static <T> Behavior<T> receive(Function2<ActorContext<T>,T,Behavior<T>> onMessage, Function2<ActorContext<T>,Signal,Behavior<T>> onSignal)
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 anActorSystem
) it will be executed within anActorContext
that allows access to the system, spawning and watching other actors, etc.Compared to using
AbstractBehavior
this 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.
-
receive
public static <T> BehaviorBuilder<T> receive(java.lang.Class<T> type)
Constructs an actor behavior builder that can build a behavior that can react to both incoming messages and lifecycle signals.Compared to using
AbstractBehavior
this 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.- Parameters:
type
- the supertype of all messages accepted by this behavior- Returns:
- the behavior builder
-
receiveSignal
public static <T> Behavior<T> receiveSignal(Function2<ActorContext<T>,Signal,Behavior<T>> handler)
Construct an actor behavior that can react to lifecycle signals only.
-
intercept
public static <O,I> Behavior<O> intercept(java.util.function.Supplier<BehaviorInterceptor<O,I>> behaviorInterceptor, Behavior<I> behavior)
Intercept messages and signals for abehavior
by first passing them to aBehaviorInterceptor
When 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
BehaviorInterceptor.isSame(akka.actor.typed.BehaviorInterceptor<java.lang.Object, java.lang.Object>)
method) 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 static <T> Behavior<T> monitor(java.lang.Class<T> interceptMessageClass, ActorRef<T> monitor, Behavior<T> behavior)
Behavior decorator that copies all received message to the designated monitorActorRef
before invoking the wrapped behavior. The wrapped behavior can evolve (i.e. return different behavior) without needing to be wrapped in amonitor
call again.- Parameters:
interceptMessageClass
- Ensures 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.monitor
- The messages will also be sent to thisActorRef
behavior
- The inner behavior that is decorated
-
logMessages
public static <T> Behavior<T> logMessages(Behavior<T> behavior)
Behavior decorator that logs all messages to theBehavior
using the providedLogOptions
default configuration before invoking the wrapped behavior. To include an MDC context then first wraplogMessages
withwithMDC
.
-
logMessages
public static <T> Behavior<T> logMessages(LogOptions logOptions, Behavior<T> behavior)
Behavior decorator that logs all messages to theBehavior
using the providedLogOptions
configuration before invoking the wrapped behavior. To include an MDC context then first wraplogMessages
withwithMDC
.
-
supervise
public static <T> Behaviors.Supervise<T> supervise(Behavior<T> wrapped)
Wrap the given behavior such that it is restarted (i.e. reset to its initial state) whenever it throws an exception of the given class or a subclass thereof. Exceptions that are not subtypes ofThr
will 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.
The
SupervisorStrategy
is only invoked for "non fatal" (seeNonFatal
) exceptions.Example:
final Behavior[DbCommand] dbConnector = ... final Behavior[DbCommand] dbRestarts = Behaviors.supervise(dbConnector) .onFailure(SupervisorStrategy.restart) // handle all NonFatal exceptions final Behavior[DbCommand] dbSpecificResumes = Behaviors.supervise(dbConnector) .onFailure[IndexOutOfBoundsException](SupervisorStrategy.resume) // resume for IndexOutOfBoundsException exceptions
-
transformMessages
public static <Outer,Inner> Behavior<Outer> transformMessages(java.lang.Class<Outer> interceptMessageClass, Behavior<Inner> behavior, java.util.function.Function<PFBuilder<Outer,Inner>,PFBuilder<Outer,Inner>> selector)
Transform the incoming messages by placing a funnel in front of the wrappedBehavior
: the supplied PartialFunction decides which message to pull in (those that it is defined at) and may transform the incoming message to place them into the wrapped Behavior’s type hierarchy. Signals are not transformed.Example:
Behavior<String> s = Behaviors.receive((ctx, msg) -> { return Behaviors.same(); }); Behavior<Number> n = Behaviors.transformMessages(Number.class, s, pf -> pf .match(BigInteger.class, i -> "BigInteger(" + i + ")") .match(BigDecimal.class, d -> "BigDecimal(" + d + ")") // drop all other kinds of Number );
- Parameters:
interceptMessageClass
- Ensures 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.behavior
- the behavior that will receive the selected messagesselector
- a partial function builder for describing the selection and transformation- Returns:
- a behavior of the
Outer
type
-
withTimers
public static <T> Behavior<T> withTimers(Function<TimerScheduler<T>,Behavior<T>> factory)
Support for scheduledself
messages 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:
TimerScheduler
-
withMdc
public static <T> Behavior<T> withMdc(java.lang.Class<T> interceptMessageClass, Function<T,java.util.Map<java.lang.String,java.lang.String>> mdcForMessage, Behavior<T> behavior)
Per message MDC (Mapped Diagnostic Context) logging.- Parameters:
interceptMessageClass
- Ensures 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.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 static <T> Behavior<T> withMdc(java.lang.Class<T> interceptMessageClass, java.util.Map<java.lang.String,java.lang.String> staticMdc, Behavior<T> behavior)
Static MDC (Mapped Diagnostic Context)- Parameters:
interceptMessageClass
- Ensures 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.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 static <T> Behavior<T> withMdc(java.lang.Class<T> interceptMessageClass, java.util.Map<java.lang.String,java.lang.String> staticMdc, Function<T,java.util.Map<java.lang.String,java.lang.String>> mdcForMessage, Behavior<T> behavior)
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
staticMdc
ormdcForMessage
may be empty.- Parameters:
interceptMessageClass
- Ensures 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.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
-
-