Mastering State Machines: From Theory to Spring and COLA Implementations

This article introduces the fundamentals of state machines, explains the four core concepts, explores domain‑specific languages (DSL) and their classifications, and provides practical Java examples using Spring Statemachine and the COLA framework, ending with a comparison and adoption guidance.

JD Cloud Developers
JD Cloud Developers
JD Cloud Developers
Mastering State Machines: From Theory to Spring and COLA Implementations

State Machine Introduction

A state machine, short for finite‑state automaton, is a mathematical model that abstracts the operational rules of real‑world entities. A state represents a condition (e.g., an automatic door can be open or closed ), an event triggers a transition (pressing the open button), an action is the operation performed after the event, and a transition moves the system from one state to another.

Given the current state and an input event, the next state can be deterministically computed.

Four Core Concepts

State – the distinct conditions of the system.

Event – the trigger that initiates a change.

Action – the operation executed when the event occurs.

Transition – the movement from one state to another.

Domain‑Specific Language (DSL)

A DSL is a programming language tailored to a specific problem domain with limited expressiveness, focusing on clarity and ease of communication between developers and domain experts. It consists of three key elements: language nature, limited expressiveness, and domain focus.

DSLs are classified into:

Internal DSL – a fluent API built on top of a host language (e.g., a state‑machine builder in Java).

External DSL – a separate language with its own syntax (e.g., XML configuration, PlantUML).

Language Workbench – IDE‑based visual tooling for DSLs.

builder.externalTransition()
        .from(None)
        .to(UN_ASSIGN_CARRIER)
        .on(Create_Event)
        .when(checkCondition())
        .perform(doAction());

builder.externalTransition()
        .from(UN_ASSIGN_CARRIER)
        .to(UN_ASSIGN_CAR)
        .on(Assign_Carrier_Event)
        .when(checkCondition())
        .perform(doAction());

Spring Statemachine

Spring Statemachine is a Spring‑based framework that enables developers to model and execute state machines within Java applications.

Flat one‑level state machine for simple use cases.

Hierarchical state machine structure for complex configurations.

State machine regions for even more intricate setups.

Support for triggers, transitions, guards, and actions.

Type‑safe configuration adapter.

Builder pattern for use outside a Spring context.

Recipes for common scenarios.

Distributed state machine based on Zookeeper listeners.

UML modeling with Eclipse Papyrus.

Persistent storage of machine configuration.

Spring IoC integration to associate beans with the state machine.

Typical Maven dependency:

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

Example configuration (Java):

@Configuration
@EnableStateMachine
@Slf4j
public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> {
    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
        states.withStates()
            .initial("SI")
            .end("SF")
            .states(new HashSet<>(Arrays.asList("S1", "S2", "S3")));
    }
    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions.withExternal()
            .source("SI").target("S1").event("E1").action(initAction())
            .and()
            .withExternal()
            .source("S1").target("S2").event("E2").action(s1Action())
            .and()
            .withExternal()
            .source("S2").target("SF").event("end");
    }
    @Bean
    public Action<String, String> initAction() {
        return ctx -> log.info("Init Action -- DO: {}", ctx.getTarget().getId());
    }
    @Bean
    public Action<String, String> s1Action() {
        return ctx -> log.info("S1 Action -- DO: {}", ctx.getTarget().getId());
    }
}
Spring Statemachine diagram
Spring Statemachine diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaDSLspringstate machineCOLA
JD Cloud Developers
Written by

JD Cloud Developers

JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.