pekko/akka-docs/java/dispatchers.rst

163 lines
4.7 KiB
ReStructuredText
Raw Normal View History

.. _dispatchers-java:
2011-04-09 19:55:46 -06:00
Dispatchers (Java)
===================
2011-04-09 19:55:46 -06:00
2011-04-26 21:52:45 +02:00
.. sidebar:: Contents
.. contents:: :local:
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-java`.
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:
2011-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
.. includecode:: ../java/code/akka/docs/dispatcher/DispatcherDocTestBase.java#defining-dispatcher
2011-04-09 19:55:46 -06:00
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"``
And then you just need to configure that dispatcher in your configuration:
2012-02-24 14:28:17 +01:00
.. includecode:: ../scala/code/akka/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-02-24 14:28:17 +01:00
.. includecode:: ../scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala#my-thread-pool-dispatcher-config
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-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-04-09 19:55:46 -06:00
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-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
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-09 19:55:46 -06: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-24 14:28:17 +01:00
* CallingThreadDispatcher
- Sharability: Unlimited
2011-04-09 19:55:46 -06: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-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-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-06-13 16:47:18 +02:00
2012-02-24 14:28:17 +01:00
.. includecode:: ../scala/code/akka/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-02-24 14:28:17 +01:00
.. includecode:: ../java/code/akka/docs/dispatcher/DispatcherDocTestBase.java#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-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
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
2012-02-24 14:28:17 +01:00
.. includecode:: ../scala/code/akka/docs/dispatcher/DispatcherTestBase.java#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-26 21:52:45 +02:00
2012-02-24 14:28:17 +01:00
.. includecode:: ../java/code/akka/docs/dispatcher/DispatcherDocTestBase.java#prio-dispatcher