2011-05-17 16:34:07 +02:00
.. _dispatchers-scala:
2011-04-09 19:55:46 -06:00
Dispatchers (Scala)
===================
2011-04-26 21:52:45 +02:00
.. sidebar :: Contents
.. contents :: :local:
2011-12-14 15:12:40 +01: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.
Setting the dispatcher for an Actor
-----------------------------------
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:
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#defining-dispatcher
.. note ::
The "dispatcherId" you specify in withDispatcher 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
2012-02-24 14:28:17 +01:00
And then you just need to configure that dispatcher in your configuration:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#my-dispatcher-config
2012-02-17 13:06:50 +01: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-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/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
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-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
- Sharability: None
- Mailboxes: Any, creates one per Actor
- Use cases: Bulkheading
- Driven by: Any `` akka.dispatch.ThreadPoolExecutorConfigurator ``
by default a "thread-pool-executor"
* BalancingDispatcher
- Sharability: Actors of the same type only
2012-02-08 15:54:35 +01:00
2012-02-24 14:28:17 +01:00
- Mailboxes: Any, creates one for all Actors
2012-02-08 15:54:35 +01:00
2012-02-24 14:28:17 +01:00
- Use cases: Work-sharing
2012-02-07 09:50:03 +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 ``
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
* CallingThreadDispatcher
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
- Sharability: Unlimited
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
- Mailboxes: Any, creates one per Actor per Thread (on demand)
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Use cases: Testing
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
- Driven by: The calling thread (duh)
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
More dispatcher configuration examples
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2012-02-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
Configuring a `` PinnedDispatcher `` :
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#my-pinned-dispatcher-config
2011-04-20 21:12:46 +02:00
2012-02-24 14:28:17 +01:00
And then using it:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#defining-pinned-dispatcher
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
Mailboxes
---------
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
An Akka `` Mailbox `` holds the messages that are destined for an `` Actor `` .
Normally each `` Actor `` has its own mailbox, but with example a `` BalancingDispatcher `` all actors with the same `` BalancingDispatcher `` will share a single instance.
2011-07-27 11:18:57 +03:00
2012-02-24 14:28:17 +01:00
Builtin implementations
^^^^^^^^^^^^^^^^^^^^^^^
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
Akka comes shipped with a number of default mailbox implementations:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
* UnboundedMailbox
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Backed by a `` java.util.concurrent.ConcurrentLinkedQueue ``
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Blocking: No
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Bounded: No
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
* BoundedMailbox
2011-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
- Backed by a `` java.util.concurrent.LinkedBlockingQueue ``
2011-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
- Blocking: Yes
2011-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
- Bounded: Yes
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
* UnboundedPriorityMailbox
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
- Backed by a `` java.util.concurrent.PriorityBlockingQueue ``
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
- Blocking: Yes
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
- Bounded: No
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
* BoundedPriorityMailbox
2011-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
- Backed by a `` java.util.PriorityBlockingQueue `` wrapped in an `` akka.util.BoundedBlockingQueue ``
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Blocking: Yes
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Bounded: Yes
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
* Durable mailboxes, see :ref: `durable-mailboxes` .
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
Mailbox configuration examples
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
How to create a PriorityMailbox:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#prio-mailbox
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
And then add it to the configuration:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#prio-dispatcher-config
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
And then an example on how you would use it:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#prio-dispatcher
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
Creating your own Mailbox type
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
An example is worth a thousand quacks:
2011-04-26 21:52:45 +02:00
2012-02-24 14:28:17 +01:00
.. includecode :: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#mailbox-implementation-example
2011-04-26 21:52:45 +02:00
2012-02-24 14:28:17 +01:00
And then you just specify the FQCN of your MailboxType as the value of the "mailbox-type" in the dispatcher configuration.