Interface ActorContext<T>
-
- All Superinterfaces:
ClassicActorContextProvider,TypedActorContext<T>
- All Known Subinterfaces:
ActorContextImpl<T>
public interface ActorContext<T> extends TypedActorContext<T>, ClassicActorContextProvider
An Actor is given by the combination of aBehaviorand 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 Akka the first capability is accessed by using the
!ortellmethod on anActorRef, the second is provided byspawn(akka.actor.typed.Behavior<U>, java.lang.String, akka.actor.typed.Props)and the third is implicit in the signature ofBehaviorin that the next behavior is always returned from the message processing logic.An
ActorContextin addition provides access to the Actor’s own identity (“self”), theActorSystemit is part of, methods for querying the list of child Actors it created, access toTerminatedand timed message scheduling.Not for user extension.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ActorContext<T>asJava()Get thejavadslof thisActorContext.<Req,Res>
voidask(RecipientRef<Req> target, scala.Function1<ActorRef<Res>,Req> createRequest, scala.Function1<scala.util.Try<Res>,T> mapResponse, Timeout responseTimeout, scala.reflect.ClassTag<Res> classTag)Perform a single request-response message interaction with another actor, and transform the messages back to the protocol of this actor.<Req,Res>
voidaskWithStatus(RecipientRef<Req> target, scala.Function1<ActorRef<StatusReply<Res>>,Req> createRequest, scala.Function1<scala.util.Try<Res>,T> mapResponse, Timeout responseTimeout, scala.reflect.ClassTag<Res> classTag)The same as<Req,Res>ask(akka.actor.typed.RecipientRef<Req>,scala.Function1<akka.actor.typed.ActorRef<Res>,Req>,scala.Function1<scala.util.Try<Res>,T>,akka.util.Timeout,scala.reflect.ClassTag<Res>)but only for requests that result in a response of typeStatusReply.voidcancelAllTimers()INTERNAL APIvoidcancelReceiveTimeout()Cancel the sending of receive timeout notifications.voidcheckCurrentActorThread()INTERNAL APIscala.Option<ActorRef<scala.runtime.Nothing$>>child(java.lang.String name)The named child Actor if it is alive.scala.collection.Iterable<ActorRef<scala.runtime.Nothing$>>children()The list of child Actors created by this Actor during its lifetime that are still alive, in no particular order.voidclearCurrentActorThread()INTERNAL APIvoidclearMdc()INTERNAL APIBehavior<T>currentBehavior()INTERNAL APIBehavior<T>delegate(Behavior<T> delegator, T msg)Delegate message and signal's execution by givenBehaviorusingBehavior.interpretMessageorBehavior.interpretSignalscala.concurrent.ExecutionContextExecutorexecutionContext()This Actor’s execution context.booleanhasTimer()INTERNAL APIorg.slf4j.Loggerlog()An actor specific logger.<U> ActorRef<U>messageAdapter(scala.Function1<U,T> f, scala.reflect.ClassTag<U> evidence$1)Create a message adapter that will convert or wrap messages such that other Actor’s protocols can be ingested by this Actor.voidonUnhandled(T msg)INTERNAL API<Value> voidpipeToSelf(scala.concurrent.Future<Value> future, scala.Function1<scala.util.Try<Value>,T> mapResult)Sends the result of the givenFutureto this Actor (“self”), after adapted it with the given function.<U> CancellablescheduleOnce(scala.concurrent.duration.FiniteDuration 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.ActorRef<T>self()The identity of this Actor, bound to the lifecycle of this Actor instance.voidsetCurrentActorThread()INTERNAL APIvoidsetLoggerName(java.lang.Class<?> clazz)Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has the given class name as logger name.voidsetLoggerName(java.lang.String name)Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has the given name as logger name.voidsetReceiveTimeout(scala.concurrent.duration.FiniteDuration 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>spawn(Behavior<U> behavior, java.lang.String name, Props props)Create a child Actor from the givenBehaviorand with the given name.<U> Propsspawn$default$3()Create a child Actor from the givenBehaviorand with the given name.<U> ActorRef<U>spawnAnonymous(Behavior<U> behavior, Props props)Create a child Actor from the givenBehaviorunder a randomly chosen name.<U> PropsspawnAnonymous$default$2()<U> ActorRef<U>spawnMessageAdapter(scala.Function1<U,T> f)INTERNAL API: SeespawnMessageAdapterwith name parameter<U> ActorRef<U>spawnMessageAdapter(scala.Function1<U,T> f, java.lang.String name)INTERNAL API: It is currently internal because it's too easy to create resource leaks by spawning adapters without stopping them.<U> voidstop(ActorRef<U> child)Force the child Actor under the given name to terminate after it finishes processing its current message.ActorSystem<scala.runtime.Nothing$>system()TheActorSystemto which this Actor belongs.<U> voidunwatch(ActorRef<U> other)Revoke the registration established bywatch.<U> voidwatch(ActorRef<U> other)Register forTerminatednotification once the Actor identified by the givenActorRefterminates.<U> voidwatchWith(ActorRef<U> other, T msg)Register for termination notification with a custom message once the Actor identified by the givenActorRefterminates.-
Methods inherited from interface akka.actor.ClassicActorContextProvider
classicActorContext
-
Methods inherited from interface akka.actor.typed.TypedActorContext
asScala
-
-
-
-
Method Detail
-
asJava
ActorContext<T> asJava()
Get thejavadslof thisActorContext.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
Futurecallbacks.- Specified by:
asJavain interfaceTypedActorContext<T>
-
ask
<Req,Res> void ask(RecipientRef<Req> target, scala.Function1<ActorRef<Res>,Req> createRequest, scala.Function1<scala.util.Try<Res>,T> mapResponse, Timeout responseTimeout, scala.reflect.ClassTag<Res> classTag)
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 a
Failure(TimeoutException)to themapResponsefunction (this is the only "normal" way aFailureis passed to the function).For other messaging patterns with other actors, see
messageAdapter(scala.Function1<U, T>, scala.reflect.ClassTag<U>).This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
Futurecallbacks.- 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.mapResponse- Transforms the response from thetargetinto a message this actor understands. 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(RecipientRef<Req> target, scala.Function1<ActorRef<StatusReply<Res>>,Req> createRequest, scala.Function1<scala.util.Try<Res>,T> mapResponse, Timeout responseTimeout, scala.reflect.ClassTag<Res> classTag)
The same as<Req,Res>ask(akka.actor.typed.RecipientRef<Req>,scala.Function1<akka.actor.typed.ActorRef<Res>,Req>,scala.Function1<scala.util.Try<Res>,T>,akka.util.Timeout,scala.reflect.ClassTag<Res>)but only for requests that result in a response of typeStatusReply. If the response is aakka.pattern.StatusReply.Successthe returned future is completed successfully with the wrapped response. If the status response is aakka.pattern.StatusReply.Errorthe returned future will be failed with the exception in the error (normally aStatusReply.ErrorMessage).
-
cancelAllTimers
void cancelAllTimers()
INTERNAL API
-
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
Futurecallbacks.
-
checkCurrentActorThread
void checkCurrentActorThread()
INTERNAL API
-
child
scala.Option<ActorRef<scala.runtime.Nothing$>> child(java.lang.String name)
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
Futurecallbacks.
-
children
scala.collection.Iterable<ActorRef<scala.runtime.Nothing$>> children()
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
Futurecallbacks.
-
clearCurrentActorThread
void clearCurrentActorThread()
INTERNAL API
-
clearMdc
void clearMdc()
INTERNAL API
-
delegate
Behavior<T> delegate(Behavior<T> delegator, T msg)
Delegate message and signal's execution by givenBehaviorusingBehavior.interpretMessageorBehavior.interpretSignalnote: if given
BehaviorresultingBehaviors.samethat will cause context switching to the given behavior and if result isBehaviors.unhandledthat will trigger theakka.actor.typed.scaladsl.ActorContext.onUnhandledthen switching to the given behavior.
-
executionContext
scala.concurrent.ExecutionContextExecutor executionContext()
This Actor’s execution context. It can be used to run asynchronous tasks likeFutureoperators.This field is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
Futurecallbacks.
-
hasTimer
boolean hasTimer()
INTERNAL API
-
log
org.slf4j.Logger log()
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
Futurecallbacks.
-
messageAdapter
<U> ActorRef<U> messageAdapter(scala.Function1<U,T> f, scala.reflect.ClassTag<U> evidence$1)
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
Futurecallbacks.
-
onUnhandled
void onUnhandled(T msg)
INTERNAL API
-
pipeToSelf
<Value> void pipeToSelf(scala.concurrent.Future<Value> future, scala.Function1<scala.util.Try<Value>,T> mapResult)Sends the result of the givenFutureto 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
Futurecallbacks.
-
scheduleOnce
<U> Cancellable scheduleOnce(scala.concurrent.duration.FiniteDuration 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. The scheduled action can be cancelled by invokingCancellable.cancel()on the returned handle.This method is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
Futurecallbacks.
-
self
ActorRef<T> self()
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 field is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
Futurecallbacks.
-
setCurrentActorThread
void setCurrentActorThread()
INTERNAL API
-
setLoggerName
void setLoggerName(java.lang.String name)
Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has the given name as logger name. Logger source MDC entry "akkaSource" 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
void setLoggerName(java.lang.Class<?> clazz)
Replace the current logger (or initialize a new logger if the logger was not touched before) with one that has the given class name as logger name. Logger source MDC entry "akkaSource" 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
Futurecallbacks.
-
setReceiveTimeout
void setReceiveTimeout(scala.concurrent.duration.FiniteDuration timeout, T msg)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
Futurecallbacks.
-
spawn
<U> ActorRef<U> spawn(Behavior<U> behavior, java.lang.String name, Props props)
Create a child Actor from the givenBehaviorand 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
Futurecallbacks.
-
spawn$default$3
<U> Props spawn$default$3()
Create a child Actor from the givenBehaviorand 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
Futurecallbacks.
-
spawnAnonymous
<U> ActorRef<U> spawnAnonymous(Behavior<U> behavior, Props props)
Create a child Actor from the givenBehaviorunder 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
Futurecallbacks.
-
spawnAnonymous$default$2
<U> Props spawnAnonymous$default$2()
-
spawnMessageAdapter
<U> ActorRef<U> spawnMessageAdapter(scala.Function1<U,T> f, java.lang.String name)
INTERNAL API: It is currently internal because it's too easy to create resource leaks by spawning adapters without stopping them.messageAdapteris the public API.Create a "lightweight" child actor that will convert or wrap messages such that other Actor’s protocols can be ingested by this Actor. You are strongly advised to cache these ActorRefs or to stop them when no longer needed.
The name of the child actor will be composed of a unique identifier starting with a dollar sign to which the given
nameargument is appended, with an inserted hyphen between these two parts. Therefore the givennameargument does not need to be unique within the scope of the parent actor.The function is applied inside the "parent" actor and can safely access state of the "parent".
-
spawnMessageAdapter
<U> ActorRef<U> spawnMessageAdapter(scala.Function1<U,T> f)
INTERNAL API: SeespawnMessageAdapterwith name parameter
-
stop
<U> void stop(ActorRef<U> child)
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
Futurecallbacks.- Throws:
java.lang.IllegalArgumentException- if the given actor ref is not a direct child of this actor
-
system
ActorSystem<scala.runtime.Nothing$> system()
TheActorSystemto which this Actor belongs.This field is thread-safe and can be called from other threads than the ordinary actor message processing thread, such as
Futurecallbacks.
-
unwatch
<U> void unwatch(ActorRef<U> other)
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
Futurecallbacks.
-
watch
<U> void watch(ActorRef<U> other)
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 Akka 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
Futurecallbacks.
-
watchWith
<U> void watchWith(ActorRef<U> other, T msg)
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 using Akka 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
Futurecallbacks.
-
-