+per #15279 FSM for PersistentActor
This commit is contained in:
parent
122fdedd08
commit
09b6abd614
22 changed files with 3595 additions and 16 deletions
|
|
@ -488,6 +488,61 @@ not accept more messages and it will throw ``AtLeastOnceDelivery.MaxUnconfirmedM
|
|||
The default value can be configured with the ``akka.persistence.at-least-once-delivery.max-unconfirmed-messages``
|
||||
configuration key. The method can be overridden by implementation classes to return non-default values.
|
||||
|
||||
.. _persistent-fsm-java-lambda:
|
||||
|
||||
Persistent FSM
|
||||
==============
|
||||
``AbstractPersistentFSMActor`` handles the incoming messages in an FSM like fashion.
|
||||
Its internal state is persisted as a sequence of changes, later referred to as domain events.
|
||||
Relationship between incoming messages, FSM's states and transitions, persistence of domain events is defined by a DSL.
|
||||
|
||||
A Simple Example
|
||||
----------------
|
||||
To demonstrate the features of the ``AbstractPersistentFSMActor``, consider an actor which represents a Web store customer.
|
||||
The contract of our "WebStoreCustomerFSMActor" is that it accepts the following commands:
|
||||
|
||||
.. includecode:: ../../../akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFsmActorTest.java#customer-commands
|
||||
|
||||
``AddItem`` sent when the customer adds an item to a shopping cart
|
||||
``Buy`` - when the customer finishes the purchase
|
||||
``Leave`` - when the customer leaves the store without purchasing anything
|
||||
``GetCurrentCart`` allows to query the current state of customer's shopping cart
|
||||
|
||||
The customer can be in one of the following states:
|
||||
|
||||
.. includecode:: ../../../akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFsmActorTest.java#customer-states
|
||||
|
||||
``LookingAround`` customer is browsing the site, but hasn't added anything to the shopping cart
|
||||
``Shopping`` customer has recently added items to the shopping cart
|
||||
``Inactive`` customer has items in the shopping cart, but hasn't added anything recently,
|
||||
``Paid`` customer has purchased the items
|
||||
|
||||
.. note::
|
||||
|
||||
``AbstractPersistentFSMActor`` states must inherit from ``PersistentFsmActor.FSMState`` and implement the
|
||||
``String identifier()`` method. This is required in order to simplify the serialization of FSM states.
|
||||
String identifiers should be unique!
|
||||
|
||||
Customer's actions are "recorded" as a sequence of "domain events", which are persisted. Those events are replayed on actor's
|
||||
start in order to restore the latest customer's state:
|
||||
|
||||
.. includecode:: ../../../akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFsmActorTest.java#customer-domain-events
|
||||
|
||||
Customer state data represents the items in customer's shopping cart:
|
||||
|
||||
.. includecode:: ../../../akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFsmActorTest.java#customer-states-data
|
||||
|
||||
Here is how everything is wired together:
|
||||
|
||||
.. includecode:: ../../../akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFsmActorTest.java#customer-fsm-body
|
||||
|
||||
.. note::
|
||||
|
||||
State data can only be modified directly on initialization. Later it's modified only as a result of applying domain events.
|
||||
Override the ``applyEvent`` method to define how state data is affected by domain events, see the example below
|
||||
|
||||
.. includecode:: ../../../akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFsmActorTest.java#customer-apply-event
|
||||
|
||||
Storage plugins
|
||||
===============
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue