spring 多线程并发状态机

spring statemachine for multiple thread concurrently

我在单独的线程中有 20 个并发用户处理订单。如何使用 spring 状态机维护状态?

我正在尝试:

public class myclass extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvents>

 @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvents> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(OrderState.A).target(OrderState.B)
                .event(OrderEvents.X)
                .and()
                .withExternal()
                .source(OrderState.A).target(OrderState.C)
                .event(OrderEvents.Y)
@Override
    public void configure(StateMachineConfigurationConfigurer<OrderState, OrderEvents> config) throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .listener(new OrderStateMachineListener());
    }

但是它在;y 告诉我状态机,而不是哪个命令:

@Override
    public void stateChanged(State<OrderState, OrderEvents> from, State<OrderState, OrderEvents> to) {
        System.out.println("Order state changed to " + to.getId());
    }

我还有其他选择,但这个选项对每个事件都不太合适:

stateMachine.stop();
        stateMachine
                .getStateMachineAccessor()
                .doWithAllRegions(access -> access.resetStateMachine(new DefaultStateMachineContext<>(sourceState, null, null, null)));
        stateMachine.start();
        stateMachine.sendEvent(event);

终于成功了。我不得不在配置级别使用@EnableStateMachineFactory 而不是@EnableStateMachine,并且调用程序:

@Autowired
StateMachineFactory<String, String> factory;

    public void getOrderStatus(long id) {
        StateMachine<String, String> stateMachine = factory.getStateMachine();
        stateMachine.start();
        stateMachine.sendEvent();
    }