parent
67a9e62254
commit
0953e7aee3
41 changed files with 156 additions and 368 deletions
|
|
@ -38,11 +38,11 @@ send them on after the burst ended or a flush request is received.
|
|||
|
||||
First, consider all of the below to use these import statements:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java#simple-imports
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java#simple-imports
|
||||
|
||||
The contract of our “Buncher” actor is that it accepts or produces the following messages:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Events.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/Events.java
|
||||
:include: simple-events
|
||||
:exclude: boilerplate
|
||||
|
||||
|
|
@ -53,7 +53,7 @@ The contract of our “Buncher” actor is that it accepts or produces the follo
|
|||
The actor can be in two states: no message queued (aka ``Idle``) or some
|
||||
message queued (aka ``Active``). The states and the state data is defined like this:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java
|
||||
:include: simple-state
|
||||
:exclude: boilerplate
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ reference to send the batches to and the actual queue of messages.
|
|||
|
||||
Now let’s take a look at the skeleton for our FSM actor:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java
|
||||
:include: simple-fsm
|
||||
:exclude: transition-elided,unhandled-elided
|
||||
|
||||
|
|
@ -93,7 +93,7 @@ shall work identically in both states, we make use of the fact that any event
|
|||
which is not handled by the ``when()`` block is passed to the
|
||||
``whenUnhandled()`` block:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java#unhandled-elided
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java#unhandled-elided
|
||||
|
||||
The first case handled here is adding ``Queue()`` requests to the internal
|
||||
queue and going to the ``Active`` state (this does the obvious thing of staying
|
||||
|
|
@ -107,7 +107,7 @@ target, for which we use the ``onTransition`` mechanism: you can declare
|
|||
multiple such blocks and all of them will be tried for matching behavior in
|
||||
case a state transition occurs (i.e. only when the state actually changes).
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java#transition-elided
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java#transition-elided
|
||||
|
||||
The transition callback is a partial function which takes as input a pair of
|
||||
states—the current and the next state. During the state change, the old state
|
||||
|
|
@ -117,7 +117,7 @@ available as ``nextStateData``.
|
|||
To verify that this buncher actually works, it is quite easy to write a test
|
||||
using the :ref:`akka-testkit`, here using JUnit as an example:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/BuncherTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/BuncherTest.java
|
||||
:include: test-code
|
||||
|
||||
Reference
|
||||
|
|
@ -129,7 +129,7 @@ The AbstractFSM Class
|
|||
The :class:`AbstractFSM` abstract class is the base class used to implement an FSM. It implements
|
||||
Actor since an Actor is created to drive the FSM.
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java
|
||||
:include: simple-fsm
|
||||
:exclude: fsm-body
|
||||
|
||||
|
|
@ -181,7 +181,7 @@ The :meth:`stateFunction` argument is a :class:`PartialFunction[Event, State]`,
|
|||
which is conveniently given using the state function builder syntax as
|
||||
demonstrated below:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/Buncher.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/Buncher.java
|
||||
:include: when-syntax
|
||||
|
||||
.. warning::
|
||||
|
|
@ -193,7 +193,7 @@ It is recommended practice to declare the states as an enum and then verify that
|
|||
``when`` clause for each of the states. If you want to leave the handling of a state
|
||||
“unhandled” (more below), it still needs to be declared like this:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java#NullFunction
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java#NullFunction
|
||||
|
||||
Defining the Initial State
|
||||
--------------------------
|
||||
|
|
@ -213,7 +213,7 @@ If a state doesn't handle a received event a warning is logged. If you want to
|
|||
do something else in this case you can specify that with
|
||||
:func:`whenUnhandled(stateFunction)`:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: unhandled-syntax
|
||||
|
||||
Within this handler the state of the FSM may be queried using the
|
||||
|
|
@ -257,7 +257,7 @@ of the modifiers described in the following:
|
|||
|
||||
All modifiers can be chained to achieve a nice and concise description:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: modifier-syntax
|
||||
|
||||
The parentheses are not actually needed in all cases, but they visually
|
||||
|
|
@ -294,14 +294,14 @@ The handler is a partial function which takes a pair of states as input; no
|
|||
resulting state is needed as it is not possible to modify the transition in
|
||||
progress.
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: transition-syntax
|
||||
|
||||
It is also possible to pass a function object accepting two states to
|
||||
:func:`onTransition`, in case your transition handling logic is implemented as
|
||||
a method:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: alt-transition-syntax
|
||||
|
||||
The handlers registered with this method are stacked, so you can intersperse
|
||||
|
|
@ -377,14 +377,14 @@ state data which is available during termination handling.
|
|||
the same way as a state transition (but note that the ``return`` statement
|
||||
may not be used within a :meth:`when` block).
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: stop-syntax
|
||||
|
||||
You can use :func:`onTermination(handler)` to specify custom code that is
|
||||
executed when the FSM is stopped. The handler is a partial function which takes
|
||||
a :class:`StopEvent(reason, stateName, stateData)` as argument:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: termination-syntax
|
||||
|
||||
As for the :func:`whenUnhandled` case, this handler is not stacked, so each
|
||||
|
|
@ -418,7 +418,7 @@ Event Tracing
|
|||
The setting ``akka.actor.debug.fsm`` in :ref:`configuration` enables logging of an
|
||||
event trace by :class:`LoggingFSM` instances:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: logging-fsm
|
||||
:exclude: body-elided
|
||||
|
||||
|
|
@ -439,7 +439,7 @@ The :class:`AbstractLoggingFSM` class adds one more feature to the FSM: a rollin
|
|||
log which may be used during debugging (for tracing how the FSM entered a
|
||||
certain failure state) or for other creative uses:
|
||||
|
||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/fsm/FSMDocTest.java
|
||||
.. includecode:: code/docs/actorlambda/fsm/FSMDocTest.java
|
||||
:include: logging-fsm
|
||||
|
||||
The :meth:`logDepth` defaults to zero, which turns off the event log.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue