2011-05-17 16:34:07 +02:00
.. _dispatchers-java:
2011-04-09 19:55:46 -06:00
Dispatchers (Java)
2011-12-13 16:18:51 +01:00
===================
2011-04-09 19:55:46 -06:00
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-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" ``
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:
2012-02-17 13:06:50 +01:00
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
--------------------
2011-12-13 16:18:51 +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-04-09 19:55:46 -06:00
2012-02-24 14:28:17 +01:00
- Use cases: Default dispatcher, Bulkheading
2011-12-13 16:18:51 +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-02-08 15:54:35 +01: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-07 09:50:03 +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-02-24 14:28:17 +01:00
- Sharability: Actors of the same type only
2011-12-13 16:18:51 +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-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-07 09:50:03 +01:00
2012-02-24 14:28:17 +01:00
* CallingThreadDispatcher
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 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-06-13 16:47:18 +02: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:
2011-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
* UnboundedMailbox
2011-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
- Backed by a `` java.util.concurrent.ConcurrentLinkedQueue ``
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
- Blocking: No
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
* BoundedMailbox
2011-12-21 20:34:46 +01:00
2012-02-24 14:28:17 +01:00
- Backed by a `` java.util.concurrent.LinkedBlockingQueue ``
2011-12-21 20:34:46 +01: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-04-12 15:40:09 +02:00
2012-02-24 14:28:17 +01:00
* UnboundedPriorityMailbox
2011-12-13 14:53:18 +01: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
2012-02-26 21:38:56 +01:00
.. includecode :: ../java/code/akka/docs/dispatcher/DispatcherDocTestBase.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-26 21:38:56 +01:00
.. includecode :: ../java/code/akka/docs/dispatcher/DispatcherDocTestBase.java#prio-dispatcher
.. note ::
2011-04-09 19:55:46 -06:00
2012-02-26 21:38:56 +01: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