Dispatchers

Dispatchers are the machinery that makes actors work. They are responsible for assigning CPU to the actors, managing actor's mailboxes, and passing over messages from the mailbox to an actor. There are four commonly used types of dispatchers: 

  • Default dispatcher: This dispatcher creates one mailbox per actor and may be shared by any number of actors. It uses java.util.concurrent.ExecutorService for this process. It is designed to be used in combination with actors having a nonblocking code. The dispatcher selects an idle thread and assigns it to an actor of its choice. The actor then processes a certain number of messages before releasing the thread.
  • Balanced dispatcher: This dispatcher creates a single mailbox that can be shared by multiple actors of the same kind. Messages from the mailbox are distributed among actors sharing the dispatcher.
  • Pinned dispatcher: This dispatcher uses a thread pool with a single thread. This thread is assigned to a single actor. Thus, each actor has its own thread and mailbox, and can perform blocking or long-running activities without starving other actors.
  • CallingThread dispatcher: This dispatcher assigns one thread per actor. This is mainly used for testing.

 

In our case, the Mixer has a blocking call in its implementation. Because of this, we are better off with the pinned dispatcher. First, we'll add a dispatcher configuration to application.conf:

mixers-dispatcher {
executor = "thread-pool-executor"
type = PinnedDispatcher
}
The name of the dispatcher is defined at the root level of the configuration and isn't nested into the akka namespace.
Akka uses Typesafe Config as a configuration library. It is a very powerful and useful configuration facility that's absolutely worth checking out. You can find it at https://github.com/lightbend/config.

And then we can use the configured dispatcher at the moment we create an actor:

def props: Props = Props[Mixer].withDispatcher("mixer-dispatcher")

This way, each of the mixers will have its own thread, and blocking will not affect its siblings and other actors in the system. 

After waiting, the mixer returns the produced dough to the sender() and sends itself a PoisonPill so that it can terminate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.38.176