Skip to content

jakkapongz/poc-spring-statemachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README for Spring Boot State Machine Experiment


Project Overview

This project demonstrates how to control application states using Spring Boot State Machine. We implemented a system with three main states (STANDBY, CONNECTING_TO_EX, LIVE) and ensured that messages are processed only in the LIVE state. Messages sent in other states are rejected globally using a filter or an AOP-based solution.


States and Events

  • States:

    1. STANDBY: The system is inactive and awaiting a connection to external services.
    2. CONNECTING_TO_EX: The system is attempting to establish a connection to external services.
    3. LIVE: The system is fully operational and ready to process messages.
  • Events:

    1. STANDBY: Transition the system to STANDBY mode.
    2. CONNECTING_TO_EX: Begin the process of connecting to external services.
    3. LIVE: Transition to LIVE mode, allowing the system to handle messages.

Implemented Features

  1. State Machine Configuration:
    The state machine was set up to handle transitions between the defined states. Events trigger state changes such as moving from STANDBY to CONNECTING_TO_EX, and eventually to LIVE.

  2. Message Handling Logic:
    Messages are accepted and processed only when the system is in the LIVE state. If the system is in any other state, the message is rejected with an appropriate response.

  3. Global Message Filtering:
    We implemented a Spring filter to globally intercept incoming requests and reject them if the system is not in the LIVE state. The filter improves maintainability by removing the need to add state checks in each controller method.

  4. Aspect-Oriented Programming (AOP):
    An AOP aspect was added to handle state validation for specific controller methods. This approach ensures that messages are conditionally processed without modifying the controller logic directly.

  5. Dynamic State Transitioning:
    The state machine dynamically handles actions and guards, ensuring that transitions are controlled based on system requirements and conditions.


Technology Stack

  • Spring Boot: Framework for building and running the application.
  • Spring State Machine: Manages state transitions and events within the application.
  • Spring Web: Provides REST API functionality for testing state transitions and message handling.
  • Spring AOP: Enables the use of aspects to enforce global rules on method execution.
  • Java: Programming language used to implement the system logic.

How to Run

  1. Clone the project and navigate to the project directory.

  2. Build and run the project using Maven or Gradle:

    ./mvnw spring-boot:run

    or

    ./gradlew bootRun
  3. Test the REST API endpoints to trigger events (STANDBY, CONNECTING_TO_EX, LIVE) and send messages.

  4. Observe the behavior when messages are sent in different states, including message rejection when the state is not LIVE.


Future Improvements

  • Implement a retry mechanism for rejected messages.
  • Add more complex state flows (e.g., error recovery and maintenance modes).
  • Integrate with monitoring tools to observe state transitions in real-time.
  • Simulate real external dependencies for the CONNECTING_TO_EX state.

This concludes the description of the Spring Boot State Machine Experiment. This project showcases how to manage states efficiently and apply global validation for message handling in a distributed system.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages