This module demonstrates a realistic implementation of the kFSM state machine framework with:
- MySQL database for persistent storage via Testcontainers
- Transactional outbox pattern using
lib-jooqfor reliable effect execution - Mock virus scanner running on a separate thread to simulate async external services
- Mock file storage to simulate S3-like storage
┌─────────────────────────────────────────────────────────────────┐
│ Document Upload Workflow │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ ┌──────────┐ ┌──────────────┐ ┌────────┐ │
│ │ Created │───▶│ Uploading│───▶│ AwaitingScan │───▶│Scanning│ │
│ └─────────┘ └──────────┘ └──────────────┘ └────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌────────┐ ┌────────┐ ┌──────────┐ │
│ │ Failed │ │ Failed │ │ Accepted │ │
│ └────────┘ └────────┘ └──────────┘ │
│ │ │
│ ▼ │
│ ┌────────────┐│
│ │ Quarantined││
│ └────────────┘│
└─────────────────────────────────────────────────────────────────┘
DocumentUpload- The value being transitionedDocumentState- Sealed class of all states with transition rulesDocumentEffect- Side effects to execute (upload, scan, notify)
Transition classes with pure decide() functions that determine state changes and effects.
JooqDocumentRepository- Persists documents and outbox messages atomicallyJooqOutbox(from lib-jooq) - Reads and manages outbox message lifecycle with SKIP LOCKEDDocumentOutboxSerializer- Serializes DocumentEffect for database storageMockVirusScanner- Simulates async virus scanning on a separate threadMockFileStorage- In-memory file storage simulation
Executes effects by calling external services and returns follow-up transitions.
# Requires Docker to be running for Testcontainers
./gradlew :example:test- Transactional Outbox: State changes and effects are persisted atomically using lib-jooq's
JooqOutbox - SKIP LOCKED: Concurrent effect processing without blocking using lib-jooq's entity-based claiming
- Async Effect Execution: Virus scanner runs on separate thread with callbacks
- Terminal Effects: Some effects (notifications, deletions) don't produce follow-up transitions
- Pure Decision Logic: All transitions use
decide()for testable business logic - Error Handling: Graceful transitions to Failed state on errors