用于休息服务请求的 Spring 状态机配置 [英] Spring state machine configuration for rest service requests

查看:41
本文介绍了用于休息服务请求的 Spring 状态机配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的订单处理应用程序,并尝试应用弹簧状态机来处理订单状态.我想知道如何在来自休息服务的多个请求期间处理同一订单的订单状态.

I have a simple order processing application and trying to apply spring state machine for handling states of the order. And I wondering how can I handle order's states for the same order during multiple request from rest service.

订单状态:

enum OrderEvents {
    FULFILL,
    PAY,
    CANCEL
}

订单事件:

enum OrderStates {
    SUBMITTED,
    PAID,
    FULFILLED,
    CANCELLED    
}

状态机配置:

@Log
@Configuration
@EnableStateMachineFactory
class SimpleEnumStatemachineConfiguration extends StateMachineConfigurerAdapter<OrderStates, OrderEvents> {

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderStates, OrderEvents> transitions) throws Exception {
        transitions
                .withExternal().source(OrderStates.SUBMITTED).target(OrderStates.PAID).event(OrderEvents.PAY)
                .and()
                .withExternal().source(OrderStates.PAID).target(OrderStates.FULFILLED).event(OrderEvents.FULFILL)
                .and()
                .withExternal().source(OrderStates.SUBMITTED).target(OrderStates.CANCELLED).event(OrderEvents.CANCEL)
                .and()
                .withExternal().source(OrderStates.PAID).target(OrderStates.CANCELLED).event(OrderEvents.CANCEL);
    }

    @Override
    public void configure(StateMachineStateConfigurer<OrderStates, OrderEvents> states) throws Exception {
        states
                .withStates()
                .initial(OrderStates.SUBMITTED)
                .state(OrderStates.PAID)
                .end(OrderStates.FULFILLED)
                .end(OrderStates.CANCELLED);
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderStates, OrderEvents> config) throws Exception {
        config.withConfiguration()
                .autoStartup(true)
    }
}

在我的订单服务中,我调用

In my order service I call

StateMachine<OrderStates, OrderEvents> sm = this.factory.getStateMachine(orderIdKey);

但似乎每次都会为相同的 orderIdKey 创建新的状态机.那么,如何访问在下一个状态提交订单时创建的状态机?

But it seems that every time is new state machine created even for the same orderIdKey. So, how can I get access to state machine created when order was submitted on next state?

推荐答案

你基本上有两个选择:

a) 使用 状态机持久化此处.

a) persist the state machine for the given orderId, using a state machine persister as explained here.

b) 为给定的 orderId(每个 HTTP 请求)创建一个新的状态机,并根据给定 order entity 的状态重新水化 SM 状态>orderId.SM 对象被认为是轻量级的,因此这也是一种可行的方法.下面是一个代码示例:

b) create a new state machine for given orderId (per HTTP request) and rehydrate the SM state based on the state of the order entity for the given orderId. SM objects are considered lightweight, so this is a viable approach as well. Below is a code sample:

StateMachine<Status, Event> build(long orderId) {
  orderService.getOrder(orderId) //returns Optional
  .map(order -> {
     StateMachine<Status, Event> sm = stateMachineFactory.getStateMachine(Long.toString(orderId));
     sm.stop();
     rehydrateState(sm, sm.getExtendedState, order.getStatus());
     sm.start();
     return sm;
   })
  .orElseGet(() -> createNewStateMachine(orderId);
}


void rehydrateState(StateMachine<Status, Event> newStateMachine, ExtendedState extendedState, Status orderStatus) {
  newStateMachine.getStateMachineAccessor().doWithAllRegions(sma ->
   sma.resetStateMachine(new DefaultStateMachineContext<>(orderStatus, null, null, extendedState));
  });
}

这篇关于用于休息服务请求的 Spring 状态机配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆