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
CodePudding user response:
The ActorContext
cannot be used outside of the actor it's associated with, including in an HTTP router. Any ActorContext
which 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 ActorContext
which could possibly be used outside of the associated actor are:
context.ask
and friends can be just as easily replaced with aFuture
-returningask
on the targetRecipientRef
with the message send occurring in aforeach
callback on the future.context.executionContext
: can just as easily usesystem.executionContext
(which will typically be the same) or by looking up via dispatcherscontext.pipeToSelf
is probably best done as a send in aforeach
callback on the futurecontext.scheduleOnce
is better done using the system scheduler directlycontext.self
is kind of pointless, as you'd have to have theActorRef
already in order to leak theActorContext
context.system
is likewise pointless, as you already have the system