Akka (3) – Dispatchers

  • Concept

    Every ActorSystem will have a default dispatcher that will be in case nothing else is configured for an actor. The default dispatcher can be configured, and is by default a Dispatcher with a specified default executor.
    An Akka MessageDispatcher is what makes Akka Actors “tick”, it is the engine of the machine so to speak. All MessageDispatcher implementations are also an ExecutionContext, which means that they can be used to execute arbitrary code, for instance Futures.

  • Types of dispatchers

Except default dispatchers, Akka contains lots of other kinds of dispatcher. Here we list some types of dispatchers:
(1) Dispatcher:
It is an event-based dispatcher that binds a set of Actors to a thread pool.
(2) PinnedDispatcher
Each actor will have its own thread pool with only one thread in the pool.
(3) BalancingDispatcher
This is an executor based event driven dispatcher that will try to redistribute work from busy actors to idle actors.
(4) CallingThreadDispatcher
This dispatcher runs invocations on the current thread only. (for testing normally)

  • Relationship between Dispatcher and ExecutionContext

If an ActorSystem is created with an ExecutionContext passed in, this ExecutionContext will be used as the default executor for all dispatchers in this ActorSystem. If no ExecutionContext is given, it will fallback to the executor specified in akka.actor.default-dispatcher.default-executor.fallback. By default, this is a “fork-join-executor” which gives excellent performance in most cases. Dispatchers implement the ExecutionContext interface and can thus be used to run Future invocations etc. 

  • Setting the dispatcher for an Actor by fork-join-pool

my-dispatcher{
  type=Dispatcher
  executor="fork-join-executor"
  fork-join-executor {
    parallelism-min=2
    parallelism-factor=2.0
    parallelism-max=10
  }
  throughput=100
}

Note: throughput defines the maximum number of messages to be processed per actor before the thread jumps to the next actor. Set to 1 for as fair as possible.

  • Setting the dispatcher for an Actor by thread pool

my-thread-pool-dispatcher{
  type=Dispatcher
  executor="thread-pool-executor"
  thread-pool-executor{
    core-pool-size-min=2
    core-pool-size-factor=2.0
    core-pool-size-max=10
  }
  throughput=100
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s