I am pretty new in Scala Akka . Say I am spawning a Configuration child actor,
object Configuration {
def apply(): Behavior[ConfigurationMessage] = Behaviors.setup(context => new Configuration(context))
}
Now I need same context ActorContext[ConfigurationMessage] in my HTTP router to do some operation.
How can I create the same ActorContext there
The
ActorContextcannot be used outside of the actor it's associated with, including in an HTTP router. AnyActorContextwhich leaks out of an actor (e.g. by sending it as a message) will, by design, throw an exception and not do anything for most operations if it's used outside of its actor.The only operations on the
ActorContextwhich could possibly be used outside of the associated actor are:context.askand friends can be just as easily replaced with aFuture-returningaskon the targetRecipientRefwith the message send occurring in aforeachcallback on the future.context.executionContext: can just as easily usesystem.executionContext(which will typically be the same) or by looking up via dispatcherscontext.pipeToSelfis probably best done as a send in aforeachcallback on the futurecontext.scheduleOnceis better done using the system scheduler directlycontext.selfis kind of pointless, as you'd have to have theActorRefalready in order to leak theActorContextcontext.systemis likewise pointless, as you already have the system