user232343
user232343

Reputation: 2118

difference between dispatcher and execution context in akka

I have following actors:

val actor1 = context.actorOf(SorterActor.build
    .withDispatcher("akka.actor.custom-dispatcher"), name = "actor_child1")
val actor2 = context.actorOf(SorterActor.build
    .withDispatcher("akka.actor.custom-dispatcher"), name = "actor_child2")

and execution context

implicit val ec: ExecutionContext = context.system.dispatchers.lookup("akka.actor.custom-dispatcher")

Thing what I don't understand is what is difference between saying which dispatcher to use with withDispatcher fn on actor, and specifying execution context which point to the same dispatcher. What will happen if execution context is pointing to different dispatcher than one defined with withDispatcher function?

Thank you.

Upvotes: 2

Views: 1896

Answers (1)

Sergiy Prydatchenko
Sergiy Prydatchenko

Reputation: 988

When you specify a dispatcher while creating actors then this dispatcher will be used by those actors themselves (for messages processing).

When you define an implicit val with execution context it determines which execution context will be used by Future-s in scope of its definition (and of course you can use it explicitly for execute Runnable-s).

If the implicit execution context is pointing to different dispatcher than one defined with withDispatcher function then your Future-s will be run on that different dispatcher (not on actor's dispatcher). Often it can be useful for more granular bulkheading.

Upvotes: 2

Related Questions