2011-05-17 16:34:07 +02:00
.. _dispatchers-scala:
2013-04-19 13:21:15 +02:00
Dispatchers
2011-04-09 19:55:46 -06:00
===================
2012-02-24 14:28:17 +01:00
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 :ref: `futures-scala` .
2011-04-09 19:55:46 -06:00
Default dispatcher
------------------
2012-02-24 14:28:17 +01:00
Every `` ActorSystem `` will have a default dispatcher that will be used in case nothing else is configured for an `` Actor `` .
The default dispatcher can be configured, and is by default a `` Dispatcher `` with a "fork-join-executor", which gives excellent performance in most cases.
2012-10-15 21:34:31 +02:00
.. _dispatcher-lookup-scala:
Looking up a Dispatcher
-----------------------
Dispatchers implement the :class: `ExecutionContext` interface and can thus be used to run :class: `Future` invocations etc.
.. includecode :: code/docs/dispatcher/DispatcherDocSpec.scala#lookup
2012-02-24 14:28:17 +01:00
Setting the dispatcher for an Actor
-----------------------------------
2011-04-09 19:55:46 -06:00
2013-04-05 16:12:45 +02:00
So in case you want to give your `` Actor `` a different dispatcher than the default, you need to do two things, of which the first
is to configure the dispatcher:
2011-04-09 19:55:46 -06:00
2012-05-24 22:23:36 +02:00
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#my-dispatcher-config
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
And here's another example that uses the "thread-pool-executor":
2011-04-09 19:55:46 -06:00
2012-05-24 22:23:36 +02:00
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#my-thread-pool-dispatcher-config
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
For more options, see the default-dispatcher section of the :ref: `configuration` .
2011-04-09 19:55:46 -06:00
2013-04-05 16:12:45 +02:00
Then you create the actor as usual and define the dispatcher in the deployment configuration.
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#defining-dispatcher-in-config
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#dispatcher-deployment-config
An alternative to the deployment configuration is to define the dispatcher in code.
If you define the `` dispatcher `` in the deployment configuration then this value will be used instead
of programmatically provided parameter.
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#defining-dispatcher-in-code
.. note ::
The dispatcher you specify in `` withDispatcher `` and the `` dispatcher `` property in the deployment
configuration is in fact a path into your configuration.
So in this example it's a top-level section, but you could for instance put it as a sub-section,
where you'd use periods to denote sub-sections, like this: `` "foo.bar.my-dispatcher" ``
2011-04-09 19:55:46 -06:00
Types of dispatchers
--------------------
2011-12-12 21:20:32 +01:00
There are 4 different types of message dispatchers:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
* Dispatcher
2011-04-09 19:55:46 -06:00
2012-03-28 17:54:40 +12:00
- This is an event-based dispatcher that binds a set of Actors to a thread pool. It is the default dispatcher
used if one is not specified.
2012-02-24 14:28:17 +01:00
- Sharability: Unlimited
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Mailboxes: Any, creates one per Actor
2011-12-12 21:20:32 +01:00
2012-02-24 14:28:17 +01:00
- Use cases: Default dispatcher, Bulkheading
2011-12-12 21:20:32 +01:00
2012-02-24 14:28:17 +01:00
- Driven by: `` java.util.concurrent.ExecutorService ``
specify using "executor" using "fork-join-executor",
"thread-pool-executor" or the FQCN of
an `` akka.dispatcher.ExecutorServiceConfigurator ``
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
* PinnedDispatcher
2012-03-29 16:42:05 +12:00
- This dispatcher dedicates a unique thread for each actor using it; i.e. each actor will have its own thread pool with only one thread in the pool.
2012-03-28 17:54:40 +12:00
2012-02-24 14:28:17 +01:00
- Sharability: None
2012-02-08 15:54:35 +01:00
2012-02-24 14:28:17 +01:00
- Mailboxes: Any, creates one per Actor
2012-02-08 15:54:35 +01:00
2012-02-24 14:28:17 +01:00
- Use cases: Bulkheading
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
- Driven by: Any `` akka.dispatch.ThreadPoolExecutorConfigurator ``
by default a "thread-pool-executor"
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
* BalancingDispatcher
2012-02-07 09:50:03 +01:00
2012-05-18 16:16:41 +02:00
- This is an executor based event driven dispatcher that will try to redistribute work from busy actors to idle actors.
2012-03-28 17:54:40 +12:00
2012-05-28 10:33:59 +02:00
- All the actors share a single Mailbox that they get their messages from.
2012-03-28 17:54:40 +12:00
- It is assumed that all actors using the same instance of this dispatcher can process all messages that have been sent to one of the actors; i.e. the actors belong to a pool of actors, and to the client there is no guarantee about which actor instance actually processes a given message.
2012-02-24 14:28:17 +01:00
- Sharability: Actors of the same type only
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
- Mailboxes: Any, creates one for all Actors
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Use cases: Work-sharing
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
- Driven by: `` java.util.concurrent.ExecutorService ``
specify using "executor" using "fork-join-executor",
"thread-pool-executor" or the FQCN of
an `` akka.dispatcher.ExecutorServiceConfigurator ``
2011-04-20 21:12:46 +02:00
2012-05-25 17:42:12 +02:00
- Note that you can **not** use a `` BalancingDispatcher `` as a **Router Dispatcher** . (You can however use it for the **Routees** )
2012-05-18 16:16:41 +02:00
2012-02-24 14:28:17 +01:00
* CallingThreadDispatcher
2012-02-07 09:50:03 +01:00
2012-05-18 16:16:41 +02:00
- This dispatcher runs invocations on the current thread only. This dispatcher does not create any new threads,
2012-06-29 14:42:11 +02:00
but it can be used from different threads concurrently for the same actor. See :ref: `Scala-CallingThreadDispatcher`
2012-03-29 16:35:30 +12:00
for details and restrictions.
2012-03-28 17:54:40 +12:00
2012-02-24 14:28:17 +01:00
- Sharability: Unlimited
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
- Mailboxes: Any, creates one per Actor per Thread (on demand)
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
- Use cases: Testing
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Driven by: The calling thread (duh)
2011-04-09 19:55:46 -06:00
2012-03-28 17:54:40 +12:00
2012-02-24 14:28:17 +01:00
More dispatcher configuration examples
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
Configuring a `` PinnedDispatcher `` :
2011-07-27 11:18:57 +03:00
2012-05-24 22:23:36 +02:00
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#my-pinned-dispatcher-config
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
And then using it:
2011-04-09 19:55:46 -06:00
2012-05-24 22:23:36 +02:00
.. includecode :: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#defining-pinned-dispatcher
2011-04-09 19:55:46 -06:00
2012-05-25 17:42:12 +02:00
Note that `` thread-pool-executor `` configuration as per the above `` my-thread-pool-dispatcher `` example is
2012-05-18 16:16:41 +02:00
NOT applicable. This is because every actor will have its own thread pool when using `` PinnedDispatcher `` ,
2012-03-28 17:54:40 +12:00
and that pool will have only one thread.
2012-12-21 12:22:39 +01:00
Note that it's not guaranteed that the *same* thread is used over time, since the core pool timeout
is used for `` PinnedDispatcher `` to keep resource usage down in case of idle actors. To use the same
thread all the time you need to add `` thread-pool-executor.allow-core-timeout=off `` to the
configuration of the `` PinnedDispatcher `` .