Spring Statemachine-从数据库配置



在我在线查找的所有示例中

 @Override
public void configure(StateMachineTransitionConfigurer<BookStates, BookEvents> transitions) throws Exception {
    transitions
            .withExternal()
            .source(BookStates.AVAILABLE)
            .target(BookStates.BORROWED)
            .event(BookEvents.BORROW)
            .and()
            .withExternal()
            .source(BookStates.BORROWED)
            .target(BookStates.AVAILABLE)
            .event(BookEvents.RETURN)
            .and()
            .withExternal()
            .source(BookStates.AVAILABLE)
            .target(BookStates.IN_REPAIR)
            .event(BookEvents.START_REPAIR)
            .and()
            .withExternal()
            .source(BookStates.IN_REPAIR)
            .target(BookStates.AVAILABLE)
            .event(BookEvents.END_REPAIR);
 }

我想通过从列表中获取源,目标,循环的源,目标,事件,以"流体"方式配置它来配置statemachine。

这可能吗?

是的,可以通过StateMachineModelFactory的自定义实现。您可以使用StateMachineModelConfigurer挂钩:

@Configuration
@EnableStateMachine
public static class Config1 extends StateMachineConfigurerAdapter<String, String> {
    @Override
    public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
        model
            .withModel()
                .factory(modelFactory());
    }
    @Bean
    public StateMachineModelFactory<String, String> modelFactory() {
        return new CustomStateMachineModelFactory();
    }
}

在实施中,您可以动态加载外部服务中SM模型所需的任何内容。以下是官方文档的一个示例:

public static class CustomStateMachineModelFactory implements StateMachineModelFactory<String, String> {
    @Override
    public StateMachineModel<String, String> build() {
        ConfigurationData<String, String> configurationData = new ConfigurationData<>();
        Collection<StateData<String, String>> stateData = new ArrayList<>();
        stateData.add(new StateData<String, String>("S1", true));
        stateData.add(new StateData<String, String>("S2"));
        StatesData<String, String> statesData = new StatesData<>(stateData);
        Collection<TransitionData<String, String>> transitionData = new ArrayList<>();
        transitionData.add(new TransitionData<String, String>("S1", "S2", "E1"));
        TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData);
        StateMachineModel<String, String> stateMachineModel = new DefaultStateMachineModel<String, String>(configurationData,
                statesData, transitionsData);
        return stateMachineModel;
    }
    @Override
    public StateMachineModel<String, String> build(String machineId) {
        return build();
    }
}

您可以轻松地加载状态并从DB动态过渡并填充ConfigurationData

相关内容

  • 没有找到相关文章

最新更新