Spring State Machine implementation using Spring Boot
Spring Statemachine is a framework for application developers to use state machine concepts with Spring applications. State machines are powerful because behaviour is always guaranteed to be consistent, making it relatively easy to debug. This is because operational rules are written in stone when the machine is started. The idea is that your application may exist in a finite number of states and certain predefined triggers can take your application from one state to the next. Such triggers can be based on either events or timers.
Common Use cases
Message (Event) based applications
Events on state changes
UI application with action triggers
Application behaviour changes based on known states
Terminology
- States:- The specific state of the state machine. Finite and predetermined values.Frequently defined in an enumeration.
- Events:- Something that happens to the system — may or may not change the state.
- Actions:- The response of the State Machine to events. Can be changing variables, calling a method or changing to a different state.
- Transitions:- Type of action which changes state
- Guards:- Boolean conditions
- Extended State:- State Machine variables (in addition to state)
Now let’s create a Spring Boot Project and see Spring State machine in Action
Here, we will create a demo application that shows the functionality of processing the payment states.
Step 1: Create the Demo spring boot app
Create a spring boot project and add the dependencies as below. Here I’m using Intellij Idea to create a new project from spring initializr. The spring state machine dependency needs to be added separately. Below images shows the dependencies needed for the project
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
Step 2: Create the domain & repository objects
Step 3: State configuration
State machine can be configured with the annotation @EnableStateMachineFactory . We can override the configure method to define the various states. We can also explore the different classes inside the state-machine-core jar.
In the overridden confifure methods we have defined the state transitions.For ex: If the event is PRE_AUTH_APPROVED then change the state from NEW to PRE_AUTH
The unit test shows the output.
Step 4: Payment Service
We can also retrieve the state machine from the database. The build() method in serviceimpl class achieve the same.
Step 5: Sending events to State Machine & State Change interceptor
State machine supports standard spring messages.
We can invoke the sendEvent() method now.
Step 6: Progressing from NEW to PRE_AUTH state
We can test this by writing the the unit test for service impl class and checking the logger statements.
Step 7: State Machine Actions
The pre-auth action method can be created in Config class itself.
Step 8: State Machine Guards
If the payment_id header is null, the transaction will not proceed.
Conclusion
I hope this article gave you an overall idea on Spring State machine implementation using Spring Boot.
The next step is to proceed to documentation and understand more about the framework and different use cases examples.
https://docs.spring.io/spring-statemachine/docs/current/reference/
Happy Learning :)
Github Repo: https://github.com/hareeshav/springstatemachine-demo
Thanks to @JoeThompson for his detailed course on Spring State Machine framework.