【问题标题】:Spring StateMachine - Configure from DatabaseSpring StateMachine - 从数据库配置
【发布时间】:2019-02-22 08:45:53
【问题描述】:

在我在网上查到的所有示例中,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,然后循环遍历列表以“流畅”的方式进行配置。

这可能吗?

【问题讨论】:

    标签: spring java-8 spring-statemachine


    【解决方案1】:

    是的,可以通过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 模型所需的任何内容。以下是来自official doc 的示例:

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

    您可以轻松地从数据库动态加载状态和转换并填充ConfigurationData

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2015-12-07
      • 1970-01-01
      • 2017-08-10
      • 2016-06-30
      • 2019-02-03
      相关资源
      最近更新 更多