pekko/akka-docs/scala/dispatchers.rst

221 lines
7.7 KiB
ReStructuredText
Raw Normal View History

.. _dispatchers-scala:
2011-04-09 19:55:46 -06:00
Dispatchers (Scala)
===================
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
-----------------------------------
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01: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:
2011-04-09 19:55:46 -06:00
.. includecode:: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#defining-dispatcher
2012-02-24 14:28:17 +01:00
.. 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"``
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
.. 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
.. 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
Types of dispatchers
--------------------
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
2012-02-24 14:28:17 +01:00
- Use cases: Default dispatcher, Bulkheading
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-24 14:28:17 +01:00
- Mailboxes: Any, creates one per Actor
2012-02-24 14:28:17 +01:00
- Use cases: Bulkheading
2012-02-24 14:28:17 +01:00
- Driven by: Any ``akka.dispatch.ThreadPoolExecutorConfigurator``
by default a "thread-pool-executor"
2012-02-24 14:28:17 +01:00
* BalancingDispatcher
- 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
- 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-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
- Note that you can **not** use a ``BalancingDispatcher`` as a **Router Dispatcher**. (You can however use it for the **Routees**)
2012-02-24 14:28:17 +01:00
* CallingThreadDispatcher
- This dispatcher runs invocations on the current thread only. This dispatcher does not create any new threads,
but it can be used from different threads concurrently for the same actor. See :ref:`TestCallingThreadDispatcherRef`
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
.. 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
.. includecode:: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#defining-pinned-dispatcher
2011-04-09 19:55:46 -06:00
Note that ``thread-pool-executor`` configuration as per the above ``my-thread-pool-dispatcher`` example is
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-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-04-09 19:55:46 -06: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:
2012-02-24 14:28:17 +01:00
* UnboundedMailbox
2012-02-24 14:28:17 +01:00
- Backed by a ``java.util.concurrent.ConcurrentLinkedQueue``
2012-02-24 14:28:17 +01:00
- Blocking: No
2012-02-24 14:28:17 +01:00
- Bounded: No
2012-02-24 14:28:17 +01:00
* BoundedMailbox
2012-02-24 14:28:17 +01:00
- Backed by a ``java.util.concurrent.LinkedBlockingQueue``
2012-02-24 14:28:17 +01:00
- Blocking: Yes
2012-02-24 14:28:17 +01:00
- Bounded: Yes
2012-02-24 14:28:17 +01:00
* UnboundedPriorityMailbox
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Backed by a ``java.util.concurrent.PriorityBlockingQueue``
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: No
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
* BoundedPriorityMailbox
2011-04-09 19:55:46 -06: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
.. includecode:: ../scala/code/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
.. includecode:: ../scala/code/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
.. includecode:: ../scala/code/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
.. includecode:: ../scala/code/docs/dispatcher/DispatcherDocSpec.scala#mailbox-implementation-example
2011-04-26 21:52:45 +02:00
And then you just specify the FQCN of your MailboxType as the value of the "mailbox-type" in the dispatcher configuration.
.. note::
2011-04-26 21:52:45 +02:00
Make sure to include a constructor which takes
``akka.actor.ActorSystem.Settings`` and ``com.typesafe.config.Config``
arguments, as this constructor is invoked reflectively to construct your
mailbox type. The config passed in as second argument is that section from
the configuration which describes the dispatcher using this mailbox type; the
mailbox type will be instantiated once for each dispatcher using it.
2011-04-26 21:52:45 +02:00
Special Semantics of ``system.actorOf``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In order to make ``system.actorOf`` both synchronous and non-blocking while
keeping the return type :class:`ActorRef` (and the semantics that the returned
ref is fully functional), special handling takes place for this case. Behind
the scenes, a hollow kind of actor reference is constructed, which is sent to
the systems guardian actor who actually creates the actor and its context and
puts those inside the reference. Until that has happened, messages sent to the
:class:`ActorRef` will be queued locally, and only upon swapping the real
filling in will they be transferred into the real mailbox. Thus,
.. code-block:: scala
system.actorOf(...) ! "bang"
assert(bangIsInMyCustomMailbx)
will probably fail; you will have to allow for some time to pass and retry the
check à la :meth:`TestKit.awaitCond`.