Member-only story

State Machine implementation for Spring Boot Project

Ishan Liyanage
Nov 10, 2020

--

What is State Machine

Well I do not write a lot about it. You have plenty of resources to read about it.

The behavior of state machines can be observed in many devices in modern society that perform a predetermined sequence of actions depending on a sequence of events with which they are presented.

Spring Framework’s Implementation

Spring Statemachine is a framework for application developers to use state machine concepts with Spring applications.

Simple Order Management Use case

Lets take below simple example

  1. Customer Submit an order.
  2. Customer pay or cancel.
  3. Order will be fulfilled or cancelled.

How do we map this to Spring State-Machine

We can start Spring boot project using Spring initializer

Make sure to add Spring Statemachine dependency

Maven:


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

Or if you use Gradle


compile group: 'org.springframework.statemachine', name: 'spring-statemachine-core', version: '2.2.0.RELEASE'

Main Implementation classes

OrderStates -enum class to hold all the states in the state machine

OrderEvents -enum class to hold all the events that can trigger in the state machine

--

--

Ishan Liyanage
Ishan Liyanage

Written by Ishan Liyanage

Passionate Technical Lead, Senior Software Developer and free and open source software advocate. Based in Singapore.

No responses yet

Write a response