用于休息服务请求的Spring状态机配置

问题描述 投票:2回答:1

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

订单状态:

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)
    }
}

在我的订单服务中,我打电话

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

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

spring-boot spring-statemachine state-machine-workflow
1个回答
0
投票

你基本上有两个选择:

a)使用orderId坚持使用state machine persister的状态机,如here所述。

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

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));
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.