spring-statemachine finite state machine

Under the strategy and medium-sized Taiwan, the Taiwan public sinking ability of the company's business, and adopt more rational, reusable architecture and technology to achieve these basic skills. In the electrical industry firm, will face the procurement of goods, merchandise shelves, transaction, change order status, customer service and other large state intervention and maintenance. A strong association between each logical state, such as: a refund operation after shipment and prior process is completely different, a refund process order shown in FIG.

 

1 Refund flowchart of FIG.

Thus, complex state management is a business depends on the demand of the changing scene. In the start-up company, may be hard-coded, the state is determined for each operation, each operation customize a logical link. With the increase of business, customized link is clearly not elegant, a large number of processes the code can not be maintained, this time in the general-purpose solution ideas is particularly important, finite state machine (Finite State Machine, abbreviation: FSM) began landing in Taiwan.

A finite state machine

 

   

Finite state machine (hereinafter referred to as FSM), also known as finite state machines, the state machine abbreviated. Wikipedia defines the mathematical model is a finite number of states and behavior and actions such as transfer between these states.

This problem model and business experience in Taiwan is very consistent. FIG. 1 is a state transition diagram, a state machine may be used to represent, in addition may be used to represent the state transition table. as shown in picture 2:

FIG state transition table 2

As can be seen, the operation of the FSM and by the abstract state management model of finite state transition. Action is described in the activities to be carried out at a given moment, we summarize the type of action are as follows:

  • Into the action: in entering the state

  • Exit action: in the state on exit

  • Input Action: dependent on the current state and input conditions

  • Transition operation: performed during a particular transfer

In the framework of the FSM, the transfer state of the pipeline flow is abstracted and structured, complex state transition diagram is divided into the smallest unit of adjacent states. This is equivalent to a Lego building on this mechanism can be combined into complex state transition diagram.

2 Spring StateMachine

 

   

Spring Statemachine framework is to help developers to simplify the development process of the state machine, the state machine to a more hierarchical structure, we look at how to achieve Spring SM. Lego smallest first model shown in Figure 3:

3 SM minimum unit of FIG.

If there is a state STATE1, STATE2 and events EVENT1, EVENT2. Event-driven state flow. To analyze the following main code of the next Spring SM.

2.1 rely pom

 

    

<dependencies>
    <dependency>
        <groupId>org.springframework.statemachine</groupId>
        <artifactId>spring-statemachine-core</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
</dependencies>

2.2 create state machine

 

   

    Three elements to the registration status of the machine via annotations: source, target, event

2.3 Notes Listener

 

  

    By listeners perceive the incident, and appropriate treatment-related logic

2.4 running state machine

 

   

3 transactions Taiwan

 

   

In the trading scene, it defines its own state machine framework, in line with the deal abstract scene of the role of the state:

  • The initial state, goal state: state relations

  • Role: Different roles have different operating authority, such as sellers, buyers, systems, customer service

  • Operation: the corresponding event

  • handler: the appropriate action to achieve operational events

So we can be defined as an event: the role of A, in the initial state S1, perform operations OP1, will use the handler to process, the successful implementation of the state to target state S2.

3.1 Personalized abstract FSM

 

   

In view of the needs of individual transactions, the state table expanded condition, and at the same time using Java reflection handler, further structured logic code. After this step, we can template data stored in the database. Figure 4:

FIG 4 FSM sets the transaction status table

Through the transformation of the core code execution engine FSM fewer than 100 lines. By registering service handler, can be flexibly expanded operational capacity. Maintains state is through the state table, without relying on manual coding, which for code quality assurance, engineering and regression testing are saving a lot of time. Also prepared the way for the realization of the configuration of the station.

3.2 Taiwan-enabled business

 

   

Taiwan precipitation in the basic skills, how to achieve? How in Taiwan energized business, business satisfied with it?

A look at the following example, based transactions, C2C, the self is great difference between the two businesses, they have completely different sets of business processes. C2C trading platform needs to be secured at both ends, and more self-assurance is to give the buyer equity. Starter process shown in Figure 5:

Figure 5 a simplified version of the transaction process

FSM by the table capacity, as long as we can state of FIG drawn, then the corresponding flow state table configuration also been produced. handler only need to focus on the business logic of the current operation, a great decoupling the state and business.

It is no exaggeration to say that a new business over, the station can configure a single person to complete the development of a state machine on line within two days. This is the efficiency of the station.

4 Summary

 

 

 

  

FSM solve complex business problems of the state of flow, and to carry out the transaction business, for example. But FSM application scenarios far more than trade. Such as customer service work order, commodity status. But not all of the processes require the use of FSM, business processes need to make a compromise, just as in Taiwan more suitable strategies 10-100-stage companies the same.

While the FSM is only a frame, but also need to build on its periphery a set of business logic. In the state transfer process, the business logic is our muscles. Like skeletal framework constrains us, so that technology grow more healthy, and maybe that is the charm of the station.

Reference: https: //projects.spring.io/spring-statemachine/

Published 18 original articles · won praise 588 · Views 1.03 million +

Guess you like

Origin blog.csdn.net/hellozhxy/article/details/103979671