1 Star 0 Fork 0

slacrey / spring-statemachine-jpa-and-rest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Build Status

Spring Statemachine using JPA and REST

This is just a sample project trying out the combination of Spring-Statemachine, JPA and Spring Data REST.

The State Machine

The state machine is a simulation of an order fulfillment process, offering two different flows: One for pay-before-shipping (prepayment, paypal etc.), and one for ship-before-payment (cash-on-delivery, invoice). Ultimately they form one state machine. Some transitions have guards depending on the "paid" status of the order, which gets set when the "ReceivePayment" event occurs (and un-set on "Refund"). Other than that, the state machine is pretty straight-forward.

The two flows are shown below.

    +----------------------------------------------------------------------------------------------------------------------------+
    |                                                     pre-payment flow                                                       |
    +----------------------------------------------------------------------------------------------------------------------------+
    |                                (1)                            (2) [if paid]                 (3) [if paid]                  |
    |     +------------------+ ReceivePayment  +-- ---------------+  Deliver +------------------+  Refund  +------------------+  |
    | *-->|       Open       |---------------->| ReadyForDelivery |--------->|    Completed     |--------->|     Canceled     |  |
    |     |                  |                 |                  |          |                  |          |                  |  |
    |     |                  |                 |  ReceivePayment  |          |                  |          |  ReceivePayment  |  |
    |     |                  |                 |  +------------+  |          |                  |          |  +------------+  |  |
    |     |                  |                 |  |    (11)    |  |          |                  |          |  |    (12)    |  |  |
    |     |                  |                 |  |            v  |          |                  |          |  |            v  |  |
    |     +------------------+                 +------------------+          +------------------+          +------------------+  |
    |        | ^                                             | |         [if paid] (4) Refund                ^   ^       | ^     |
    |        | |                                             | +---------------------------------------------+   |       | |     |
    |        | |                                             |                                                   |       | |     |
    |        | |                                             |           [if !paid]  (8) Cancel                  |       | |     |
    |        | |           (5) Reopen                        +---------------------------------------------------+       | |     |
    |        | +---------------------------------------------------------------------------------------------------------+ |     |
    |        |                                              (6) Cancel                                                     |     |
    |        +-------------------------------------------------------------------------------------------------------------+     |
    |                                                                                                                            |
    +----------------------------------------------------------------------------------------------------------------------------+


    +-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    |                                                                     post-payment flow                                                                       |
    +-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    |                                (7)                            (9) [if !paid]                 (10)                            (3) [if paid]                  |
    |     +------------------+ UnlockDelivery  +-- ---------------+  Deliver +------------------+ ReceivePayment +---------------+  Refund  +------------------+  |
    | *-->|       Open       |---------------->| ReadyForDelivery |--------->| AwaitingPayment  |--------------->|   Completed   |--------->|     Canceled     |  |
    |     |                  |                 |                  |          |                  |                |               |          |                  |  |
    |     |                  |                 |  ReceivePayment  |          |                  |                |               |          |  ReceivePayment  |  |
    |     |                  |                 |  +------------+  |          |                  |                |               |          |  +------------+  |  |
    |     |                  |                 |  |    (11)    |  |          |                  |                |               |          |  |    (12)    |  |  |
    |     |                  |                 |  |            v  |          |                  |                |               |          |  |            v  |  |
    |     +------------------+                 +------------------+          +------------------+                +---------------+          +------------------+  |
    |        | ^                                             |  |          [if paid] (4) Refund                                               ^    ^      | ^     |
    |        | |                                             |  +-----------------------------------------------------------------------------+    |      | |     |
    |        | |                                             |                                                                                     |      | |     |
    |        | |                                             |             [if !paid] (8) Cancel                                                   |      | |     |
    |        | |           (5) Reopen                        +-------------------------------------------------------------------------------------+      | |     |
    |        | +------------------------------------------------------------------------------------------------------------------------------------------+ |     |
    |        |                                              (6) Cancel                                                                                      |     |
    |        +----------------------------------------------------------------------------------------------------------------------------------------------+     |
    |                                                                                                                                                             |
    +-------------------------------------------------------------------------------------------------------------------------------------------------------------+

UML state machine

Persistence

I use a simple domain object as state machine context (my Order entity), which holds the StateMachineContext in a binary-serialized form, using Kryo.

The REST-ful API

The REST API exposes links for receiving events to trigger state transitions for the state machine. The server either responds with 202 Accepted if an event was accepted, or 422 Unprocessable Entity if the event was not accepted by the state machine.

Current State and Links

The resource intentionally does not expose its current state as enum, but as a localized text, so that clients are aware of the fact they should not build business logic upon that. Instead the client should use the links for triggering the state transitions that are possible for the given state. You can get more details about why you should expose links instead of status properties in Oliver Gierke's talk about DDD and REST.

{
    "_links": {
        "cancel": {
            "href": "http://localhost:8080/orders/1/receive/Cancel"
        }, 
        "order": {
            "href": "http://localhost:8080/orders/1"
        }, 
        "receive-payment": {
            "href": "http://localhost:8080/orders/1/receive/ReceivePayment"
        }, 
        "self": {
            "href": "http://localhost:8080/orders/1"
        }, 
        "unlock-delivery": {
            "href": "http://localhost:8080/orders/1/receive/UnlockDelivery"
        }
    }
}

User guides

Further information can be found by the documentation guides generated from the latest master build.

Order-specfic components

  • Order : My entity acting as context object

  • OrderStateMachineConfiguration : The setup of the state machine.

  • OrderCreatedEventListener : An AbstractRepositoryEventListener getting notified when a new order is created, initializing it with a new state machine.

  • OrderEventController : RestController for receiving events.

Generic components

  • ContextEntity : Interface combining the role of a statemachine context object and spring-hateoas Identifiable

  • ContextObjectResourceProcessor : For adding the links to the response.

  • StateMachineContextConverter : The converter from StateMachineContext to byte[] and back.

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/slacrey/spring-statemachine-jpa-and-rest.git
git@gitee.com:slacrey/spring-statemachine-jpa-and-rest.git
slacrey
spring-statemachine-jpa-and-rest
spring-statemachine-jpa-and-rest
master

搜索帮助