2011-12-19 18:58:19 +13:00
|
|
|
|
.. _agents-java:
|
|
|
|
|
|
|
|
|
|
|
|
##############
|
|
|
|
|
|
Agents (Java)
|
|
|
|
|
|
##############
|
|
|
|
|
|
|
|
|
|
|
|
Agents in Akka are inspired by `agents in Clojure`_.
|
|
|
|
|
|
|
|
|
|
|
|
.. _agents in Clojure: http://clojure.org/agents
|
|
|
|
|
|
|
|
|
|
|
|
Agents provide asynchronous change of individual locations. Agents are bound to
|
|
|
|
|
|
a single storage location for their lifetime, and only allow mutation of that
|
|
|
|
|
|
location (to a new state) to occur as a result of an action. Update actions are
|
|
|
|
|
|
functions that are asynchronously applied to the Agent's state and whose return
|
|
|
|
|
|
value becomes the Agent's new state. The state of an Agent should be immutable.
|
|
|
|
|
|
|
|
|
|
|
|
While updates to Agents are asynchronous, the state of an Agent is always
|
2013-01-23 02:06:49 +01:00
|
|
|
|
immediately available for reading by any thread (using ``get``) without any messages.
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
|
|
|
|
|
Agents are reactive. The update actions of all Agents get interleaved amongst
|
2013-01-23 02:06:49 +01:00
|
|
|
|
threads in an ``ExecutionContext``. At any point in time, at most one ``send`` action for
|
2011-12-19 18:58:19 +13:00
|
|
|
|
each Agent is being executed. Actions dispatched to an agent from another thread
|
|
|
|
|
|
will occur in the order they were sent, potentially interleaved with actions
|
2013-01-23 02:06:49 +01:00
|
|
|
|
dispatched to the same agent from other threads.
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
|
|
|
|
|
If an Agent is used within an enclosing transaction, then it will participate in
|
2013-01-23 02:06:49 +01:00
|
|
|
|
that transaction. Agents are integrated with Scala STM - any dispatches made in
|
2011-12-19 18:58:19 +13:00
|
|
|
|
a transaction are held until that transaction commits, and are discarded if it
|
|
|
|
|
|
is retried or aborted.
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
Creating Agents
|
2011-12-19 18:58:19 +13:00
|
|
|
|
============================
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
Agents are created by invoking ``new Agent<ValueType>(value, executionContext)`` – passing in the Agent's initial
|
|
|
|
|
|
value and providing an ``ExecutionContext`` to be used for it:
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2012-05-24 22:23:36 +02:00
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java
|
2013-01-23 02:06:49 +01:00
|
|
|
|
:include: import-agent,create
|
2011-12-19 18:58:19 +13:00
|
|
|
|
:language: java
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
Reading an Agent's value
|
|
|
|
|
|
========================
|
|
|
|
|
|
|
|
|
|
|
|
Agents can be dereferenced (you can get an Agent's value) by invoking the Agent
|
|
|
|
|
|
with ``get()`` like this:
|
|
|
|
|
|
|
|
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java#read-get
|
2011-12-19 18:58:19 +13:00
|
|
|
|
:language: java
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
Reading an Agent's current value does not involve any message passing and
|
|
|
|
|
|
happens immediately. So while updates to an Agent are asynchronous, reading the
|
|
|
|
|
|
state of an Agent is synchronous.
|
|
|
|
|
|
|
|
|
|
|
|
You can also get a ``Future`` to the Agents value, that will be completed after the
|
|
|
|
|
|
currently queued updates have completed:
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java
|
|
|
|
|
|
:include: import-future,read-future
|
2011-12-19 18:58:19 +13:00
|
|
|
|
:language: java
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
See :ref:`futures-java` for more information on ``Futures``.
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
Updating Agents (send & alter)
|
|
|
|
|
|
==============================
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
You update an Agent by sending a function (``akka.dispatch.Mapper``) that transforms the current value or
|
2011-12-19 18:58:19 +13:00
|
|
|
|
by sending just a new value. The Agent will apply the new value or function
|
|
|
|
|
|
atomically and asynchronously. The update is done in a fire-forget manner and
|
|
|
|
|
|
you are only guaranteed that it will be applied. There is no guarantee of when
|
|
|
|
|
|
the update will be applied but dispatches to an Agent from a single thread will
|
|
|
|
|
|
occur in order. You apply a value or a function by invoking the ``send``
|
|
|
|
|
|
function.
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java
|
|
|
|
|
|
:include: import-function,send
|
2011-12-19 18:58:19 +13:00
|
|
|
|
:language: java
|
|
|
|
|
|
|
|
|
|
|
|
You can also dispatch a function to update the internal state but on its own
|
|
|
|
|
|
thread. This does not use the reactive thread pool and can be used for
|
|
|
|
|
|
long-running or blocking operations. You do this with the ``sendOff``
|
|
|
|
|
|
method. Dispatches using either ``sendOff`` or ``send`` will still be executed
|
|
|
|
|
|
in order.
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java
|
|
|
|
|
|
:include: import-function,send-off
|
2011-12-19 18:58:19 +13:00
|
|
|
|
:language: java
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
All ``send`` methods also have a corresponding ``alter`` method that returns a ``Future``.
|
|
|
|
|
|
See :ref:`futures-java` for more information on ``Futures``.
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java
|
|
|
|
|
|
:include: import-future,import-function,alter
|
2011-12-19 18:58:19 +13:00
|
|
|
|
:language: java
|
|
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
.. includecode:: code/docs/agent/AgentDocTest.java
|
|
|
|
|
|
:include: import-future,import-function,alter-off
|
|
|
|
|
|
:language: java
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2013-01-23 02:06:49 +01:00
|
|
|
|
Transactional Agents
|
|
|
|
|
|
====================
|
2011-12-19 18:58:19 +13:00
|
|
|
|
|
2013-01-27 23:01:42 +01:00
|
|
|
|
If an Agent is used within an enclosing ``Scala STM transaction``, then it will participate in
|
2013-01-23 02:06:49 +01:00
|
|
|
|
that transaction. If you send to an Agent within a transaction then the dispatch
|
|
|
|
|
|
to the Agent will be held until that transaction commits, and discarded if the
|
2013-01-27 23:01:42 +01:00
|
|
|
|
transaction is aborted.
|