Spring StateMachine - 从数据库配置

2024-04-09

在我在线查找的所有示例中,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);
 }

我想通过从数据库获取源、目标、事件并循环列表来“动态”配置状态机,以“流动”方式配置它。

这可能吗?


是的,可以通过自定义实现StateMachineModelFactory https://docs.spring.io/spring-statemachine/docs/current/api/org/springframework/statemachine/config/model/StateMachineModelFactory.html。你可以使用它挂钩StateMachineModelConfigurer https://docs.spring.io/spring-statemachine/docs/current/api/org/springframework/statemachine/config/builders/StateMachineModelConfigurer.html像这样:

@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 模型所需的任何内容。下面是一个来自官方文档 https://docs.spring.io/spring-statemachine/docs/2.0.3.RELEASE/reference/htmlsingle/#configuring-model:

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 https://docs.spring.io/spring-statemachine/docs/current/api/org/springframework/statemachine/config/model/ConfigurationData.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring StateMachine - 从数据库配置 的相关文章

随机推荐