Giter Site home page Giter Site logo

non-blocking-state-machine's Introduction

non-blocking-state-machine

This Spring Boot Java project extends the simple-state-machine project to make the processors asynchronous and non-blocking.

Usage

The first step in using the framework is to configure the state transitions. An online order processing system is considered as an example for the demo. The order processing system is assumed to have the following state transitions:

Initial State Pre-event Processor Post-event Final State
DEFAULT submit OrderProcessor orderCreated PMTPENDING
PMTPENDING pay PaymentProcessor paymentError PMTERROREMAILPENDING
PMTERROREMAILPENDING errorEmail PaymentErrorEmailProcessor pmtErrorEmailSent PMTPENDING
PMTPENDING pay PaymentProcessor paymentSuccess PMTSUCCESSEMAILPENDING
PMTSUCCESSEMAILPENDING successEmail PaymentSuccessEmailProcessor pmtSuccessEmailSent COMPLETED

where the PaymentProcessor is considered a long running process and made asynchronous and non-blocking. All other processors are assumed synchronous. The above state transitions are configured in Java enums, OrderState and OrderEvent.

The demo includes an OrderController with two APIs to test the following scenarios:

Testing

Build using:
$ ./gradlew build

Run using:
$ \gradlew bootRun

Submit an order

User submits an order
System responds with an order ID.
Log messages confirm the state transition.

$ curl -X POST http://localhost:8080/order/items

Submit an invalid payment (amount = 0.00)

User submits an invalid payment for the order
System responds with a message "We are processing your payment. Please check your email for the order confirmation number."
System also sends an email with an error message.
Log messages confirm the state transition.

$ curl -X POST http://localhost:8080/orders/1e6092da-3bef-4377-8a02-5a4cb93f4a96/payment/0

Submit a valid payment (amount > 0.00)

User submits a valid payment for the order
System responds with a message "We are processing your payment. Please check your email for the order confirmation number."
System also sends an email with a success message.
Log messages confirm the state transition.

$ curl -X POST http://localhost:8080/orders/1e6092da-3bef-4377-8a02-5a4cb93f4a96/payment/1

Submit a valid payment for an order which is already completed

User submits a valid payment for the order
System responds with a message "Order is completed for orderId=1e6092da-3bef-4377-8a02-5a4cb93f4a96."
No state transition occurs in this case.

$ curl -X POST http://localhost:8080/orders/1e6092da-3bef-4377-8a02-5a4cb93f4a96/payment/2

Interested readers can run the included Apache JMeter test plan to test for multiple simultaneous users placing orders and paying for the orders.

More Info

More info about this project is also available at: dzone

non-blocking-state-machine's People

Contributors

mapteb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.