Interface ActorContext<T>
- All Superinterfaces:
ClassicActorContextProvider,TypedActorContext<T>
- All Known Subinterfaces:
ActorContextImpl<T>
Behavior and a context in
which this behavior is executed. As per the Actor Model an Actor can perform
the following actions when processing a message:
- send a finite number of messages to other Actors it knows - create a finite number of Actors - designate the behavior for the next message
In Pekko, the first capability is accessed by using the tell method
on an ActorRef, the second is provided by spawn(org.apache.pekko.actor.typed.Behavior<U>, java.lang.String)
and the third is implicit in the signature of Behavior in that the next
behavior is always returned from the message processing logic.
An ActorContext in addition provides access to the Actor’s own identity (“getSelf”),
the ActorSystem it is part of, methods for querying the list of child Actors it
created, access to Terminated and timed message scheduling.
Not for user extension.
-
Method Summary
Modifier and TypeMethodDescription<Req,Res> void ask(Class<Res> resClass, RecipientRef<Req> target, Duration responseTimeout, Function<ActorRef<Res>, Req> createRequest, Function2<Res, Throwable, T> applyToResponse) Perform a single request-response message interaction with another actor, and transform the messages back to the protocol of this actor.<Req,Res> void askWithStatus(Class<Res> resClass, RecipientRef<Req> target, Duration responseTimeout, Function<ActorRef<StatusReply<Res>>, Req> createRequest, Function2<Res, Throwable, T> applyToResponse) The same as<Req,Res>ask(java.lang.Class<Res>,org.apache.pekko.actor.typed.RecipientRef<Req>,java.time.Duration,org.apache.pekko.japi.function.Function<org.apache.pekko.actor.typed.ActorRef<Res>,Req>,org.apache.pekko.japi.function.Function2<Res,java.lang.Throwable,T>)but only for requests that result in a response of typepekko.pattern.StatusReply.asScala()Get thescaladslof thisActorContext.voidCancel the sending of receive timeout notifications.Delegate message and signal's execution by givenpekko.actor.typed.BehaviorusingBehavior.interpretMessageorBehavior.interpretSignalThe named child Actor if it is alive.The list of child Actors created by this Actor during its lifetime that are still alive, in no particular order.scala.concurrent.ExecutionContextExecutorThis Actor’s execution context.org.slf4j.LoggergetLog()An actor specific logger.getSelf()The identity of this Actor, bound to the lifecycle of this Actor instance.TheActorSystemto which this Actor belongs.<U> ActorRef<U>messageAdapter(Class<U> messageClass, Function<U, T> f) Create a message adapter that will convert or wrap messages such that other Actor’s protocols can be ingested by this Actor.<Value> voidpipeToSelf(CompletionStage<Value> future, Function2<Value, Throwable, T> applyToResult) Sends the result of the givenCompletionStageto this Actor (“self”), after adapted it with the given function.<U> CancellablescheduleOnce(Duration delay, ActorRef<U> target, U msg) Schedule the sending of the given message to the given target Actor after the given time period has elapsed.voidsetLoggerName(Class<?> clazz) Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has ghe given class name as logger name.voidsetLoggerName(String name) Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has ghe given name as logger name.voidsetReceiveTimeout(Duration timeout, T msg) Schedule the sending of a notification in case no other message is received during the given period of time.<U> ActorRef<U>Create a child Actor from the givenpekko.actor.typed.Behaviorand with the given name.<U> ActorRef<U>Create a child Actor from the givenpekko.actor.typed.Behaviorand with the given name.<U> ActorRef<U>spawnAnonymous(Behavior<U> behavior) Create a child Actor from the givenpekko.actor.typed.Behaviorunder a randomly chosen name.<U> ActorRef<U>spawnAnonymous(Behavior<U> behavior, Props props) Create a child Actor from the givenpekko.actor.typed.Behaviorunder a randomly chosen name.<U> voidForce the child Actor under the given name to terminate after it finishes processing its current message.<U> voidRevoke the registration established bywatch.<U> voidRegister forTerminatednotification once the Actor identified by the givenActorRefterminates.<U> voidRegister for termination notification with a custom message once the Actor identified by the givenActorRefterminates.Methods inherited from interface org.apache.pekko.actor.ClassicActorContextProvider
classicActorContextMethods inherited from interface org.apache.pekko.actor.typed.TypedActorContext
asJava
-
Method Details
-
asScala
ActorContext<T> asScala()Get thescaladslof thisActorContext.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks.- Specified by:
asScalain interfaceTypedActorContext<T>
-
ask
<Req,Res> void ask(Class<Res> resClass, RecipientRef<Req> target, Duration responseTimeout, Function<ActorRef<Res>, Req> createRequest, Function2<Res, Throwable, T> applyToResponse) Perform a single request-response message interaction with another actor, and transform the messages back to the protocol of this actor.The interaction has a timeout (to avoid a resource leak). If the timeout hits without any response it will be passed as an
TimeoutExceptionto theapplyToResponsefunction.For other messaging patterns with other actors, see
messageAdapter(java.lang.Class<U>, org.apache.pekko.japi.function.Function<U, T>).This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks.- Parameters:
createRequest- A function that creates a message for the other actor, containing the providedActorRef[Res]that the other actor can send a message back through.applyToResponse- Transforms the response from thetargetinto a message this actor understands. Will be invoked with either the response message or an AskTimeoutException failed or potentially another exception if the remote actor is classic and sent apekko.actor.Status.Failureas response. The returned message of typeTis then fed into this actor as a message. Should be a pure function but is executed inside the actor when the response arrives so can safely touch the actor internals. If this function throws an exception it is just as if the normal message receiving logic would throw.
-
askWithStatus
<Req,Res> void askWithStatus(Class<Res> resClass, RecipientRef<Req> target, Duration responseTimeout, Function<ActorRef<StatusReply<Res>>, Req> createRequest, Function2<Res, Throwable, T> applyToResponse) The same as<Req,Res>ask(java.lang.Class<Res>,org.apache.pekko.actor.typed.RecipientRef<Req>,java.time.Duration,org.apache.pekko.japi.function.Function<org.apache.pekko.actor.typed.ActorRef<Res>,Req>,org.apache.pekko.japi.function.Function2<Res,java.lang.Throwable,T>)but only for requests that result in a response of typepekko.pattern.StatusReply. If the response is apekko.pattern.StatusReply#successthe returned future is completed successfully with the wrapped response. If the status response is apekko.pattern.StatusReply#errorthe returned future will be failed with the exception in the error (normally apekko.pattern.StatusReply.ErrorMessage). -
cancelReceiveTimeout
void cancelReceiveTimeout()Cancel the sending of receive timeout notifications.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
delegate
Delegate message and signal's execution by givenpekko.actor.typed.BehaviorusingBehavior.interpretMessageorBehavior.interpretSignalnote: if given
pekko.actor.typed.BehaviorresultingBehaviors.samethat will cause context switching to the given behavior and if result isBehaviors.unhandledthat will trigger thepekko.actor.typed.scaladsl.ActorContext.onUnhandledthen switching to the given behavior. -
getChild
The named child Actor if it is alive.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
getChildren
The list of child Actors created by this Actor during its lifetime that are still alive, in no particular order.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
getExecutionContext
scala.concurrent.ExecutionContextExecutor getExecutionContext()This Actor’s execution context. It can be used to run asynchronous tasks likeFuturecombinators.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
getLog
org.slf4j.Logger getLog()An actor specific logger.The logger name will be an estimated source class for the actor which is calculated when the logger is first used (the logger is lazily created upon first use). If this yields the wrong class or another class is preferred this can be changed with
setLoggerName.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
getSelf
The identity of this Actor, bound to the lifecycle of this Actor instance. An Actor with the same name that lives before or after this instance will have a differentActorRef.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
getSystem
ActorSystem<Void> getSystem()TheActorSystemto which this Actor belongs.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
messageAdapter
Create a message adapter that will convert or wrap messages such that other Actor’s protocols can be ingested by this Actor.You can register several message adapters for different message classes. It's only possible to have one message adapter per message class to make sure that the number of adapters are not growing unbounded if registered repeatedly. That also means that a registered adapter will replace an existing adapter for the same message class.
A message adapter will be used if the message class matches the given class or is a subclass thereof. The registered adapters are tried in reverse order of their registration order, i.e. the last registered first.
A message adapter (and the returned
ActorRef) has the same lifecycle as this actor. It's recommended to register the adapters in a top levelBehaviors.setupor constructor ofAbstractBehaviorbut it's possible to register them later also if needed. Message adapters don't have to be stopped since they consume no resources other than an entry in an internalMapand the number of adapters are bounded since it's only possible to have one per message class.The function is running in this actor and can safely access state of it.
*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
pipeToSelf
Sends the result of the givenCompletionStageto this Actor (“self”), after adapted it with the given function.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
scheduleOnce
Schedule the sending of the given message to the given target Actor after the given time period has elapsed. The scheduled action can be cancelled by invokingpekko.actor.Cancellable#cancelon the returned handle.For scheduling messages to the actor itself, use
Behaviors.withTimersThis method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
setLoggerName
Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has ghe given name as logger name. Logger source MDC entry "pekkoSource" will be the actor path.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
setLoggerName
Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has ghe given class name as logger name. Logger source MDC entry "pekkoSource" will be the actor path.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
setReceiveTimeout
Schedule the sending of a notification in case no other message is received during the given period of time. The timeout starts anew with each received message. UsecancelReceiveTimeoutto switch off this mechanism.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
spawn
Create a child Actor from the givenpekko.actor.typed.Behaviorand with the given name.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
spawn
Create a child Actor from the givenpekko.actor.typed.Behaviorand with the given name.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
spawnAnonymous
Create a child Actor from the givenpekko.actor.typed.Behaviorunder a randomly chosen name. It is good practice to name Actors wherever practical.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
spawnAnonymous
Create a child Actor from the givenpekko.actor.typed.Behaviorunder a randomly chosen name. It is good practice to name Actors wherever practical.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
stop
Force the child Actor under the given name to terminate after it finishes processing its current message. Nothing happens if the ActorRef is a child that is already stopped.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks.- Throws:
IllegalArgumentException- if the given actor ref is not a direct child of this actorc
-
unwatch
Revoke the registration established bywatch. ATerminatednotification will not subsequently be received for the referenced Actor.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
watch
Register forTerminatednotification once the Actor identified by the givenActorRefterminates. This message is also sent when the watched actor is on a node that has been removed from the cluster when using Pekko Cluster.watchis idempotent if it is not mixed withwatchWith.It will fail with an
IllegalStateExceptionif the same subject was watched before usingwatchWith. To clear the termination message, unwatch first.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks. -
watchWith
Register for termination notification with a custom message once the Actor identified by the givenActorRefterminates. This message is also sent when the watched actor is on a node that has been removed from the cluster when using Pekko Cluster.watchWithis idempotent if it is called with the samemsgand not mixed withwatch.It will fail with an
IllegalStateExceptionif the same subject was watched before usingwatchorwatchWithwith another termination message. To change the termination message, unwatch first.*Warning*: This method is not thread-safe and must not be accessed from threads other than the ordinary actor message processing thread, such as
CompletionStagecallbacks.
-