弹簧状态机 使用两种状态机配置保留配方



我正在尝试将弹簧状态机的持久化示例扩展到两种不同的状态机配置。http://docs.spring.io/spring-statemachine/docs/1.0.0.RELEASE/reference/htmlsingle/#statemachine-examples-persist

因此,我

  • 添加了新架构
  • 添加了一些测试数据
  • 复制了Persist,PersistCommand的代码,并使它们适应我的情况

到目前为止没什么大不了的。现在进入配置:

  • 我删除了StateMachineConfig(以及随之而来的@EnableStateMachine注释(
  • 将 StateMachineConfiguration 作为 Bean 添加到 PersistHandlerConfig 中,并使用 Builder
  • 复制了该配置并将其调整为我的用例
  • 显然给我装了一个像订单这样的类 为我的情况订购

此外,我调整了 AbstractStateMachineCommands 类,并自动连线了其中的状态机列表。启动/停止和状态方法现在启动/停止和打印每个状态机的状态(这里我不关心打印,变量(。

出现的问题是:

  • 持久不再起作用
  • 我可以启动应用程序和两个状态机
  • 我可以使用所有持久化和myUseCase调用,但没有处理持久数据。
  • 例如,如果我调用持久进程 1,则应用程序将底层 SM 的状态更改为处理,但丢失的数据不会更改。
  • 调试中,我能够解决在LifecycleObjectSupport中getTaskExecutor方法返回null的问题(而在原始示例中,bean工厂返回SyncTaskExecutor的实例(。
  • 此外,TestEventListener似乎不再适用于任何状态机
  • 当我在任何包含状态机 Bean 的配置中使用 @EnableStateMachine 时,NPE 会在 StateMachineConfiguration 的 afterPropertiesSet 中发生。

那么,谁能告诉我我在哪里搞砸了?还是持久配方不适用于两个状态机?

多谢。

代码示例:应用程序.java现在包含以下配置和实体:

    @Configuration
static class PersistHandlerConfig {
    @Bean
    public Persist persist() throws Exception {
        return new Persist(persistStateMachineHandler());
    }
    @Bean
    public PersistStateMachineHandler persistStateMachineHandler() throws Exception {
        return new PersistStateMachineHandler(persistSm());
    }
    @Bean
    public StateMachine<String, String> persistSm() throws Exception{
        Builder<String, String> builder = StateMachineBuilder.builder();
        builder.configureStates()
            .withStates()
                .initial("PLACED")
                .state("PROCESSING")
                .state("SENT")
                .state("DELIVERED");
        builder.configureTransitions()
            .withExternal()
                .source("PLACED").target("PROCESSING")
                .event("PROCESS")
                .and()
            .withExternal()
                .source("PROCESSING").target("SENT")
                .event("SEND")
                .and()
            .withExternal()
                .source("SENT").target("DELIVERED")
                .event("DELIVER");
        return builder.build();
    }
}
@Configuration
static class TicketPersistHandlerConfig {
  @Bean
  public TicketPersist ticketPersist() throws Exception {
    return new TicketPersist(ticketPersistStateMachineHandler());
  }
  @Bean
  public PersistStateMachineHandler ticketPersistStateMachineHandler() throws Exception {
    return new PersistStateMachineHandler(buildMachine());
  }
  @Bean
  public StateMachine<String, String> buildMachine() throws Exception {
       Builder<String, String> builder = StateMachineBuilder.builder();
       builder.configureStates()
           .withStates()
               .initial("PRINTED")
               .state("BOOKED")
               .state("SOLD")
               .state("DELIVERED");
       builder.configureTransitions()
           .withExternal()
               .source("PRINTED").target("BOOKED")
               .event("BOOK")
               .and()
           .withExternal()
               .source("BOOKED").target("SOLD")
               .event("SELL")
               .and()
           .withExternal()
               .source("SOLD").target("DELIVERED")
               .event("DELIVER");
       return builder.build();
   }
}
public static class Order {
    int id;
    String state;
    public Order(int id, String state) {
        this.id = id;
        this.state = state;
    }
    @Override
    public String toString() {
        return "Order [id=" + id + ", state=" + state + "]";
    }
}
public static class Ticket {
  int id;
  String state;
  public Ticket(int id, String state) {
    this.id = id;
    this.state = state;
  }
  @Override
  public String toString() {
    return "Ticket [id=" + id + ", state=" + state + "]";
  }
}
TicketPersist.java

和 TicketPersistCommand.java 与订单(只是用票证替换订单(相同。我通过以下方式改编了AbstractStateMachineCommand:

@Autowired
private List<StateMachine<S, E>> stateMachines;
@CliCommand(value = "sm start", help = "Start a state machine")
public String start() {
  for (StateMachine<S, E> stateMachine : stateMachines)
  {
    stateMachine.start();
  }
    return "State machines started";
}
@CliCommand(value = "sm stop", help = "Stop a state machine")
public String stop() {
  for (StateMachine<S, E> stateMachine : stateMachines)
  {
    stateMachine.stop();
  }
    return "State machines stopped";
}

普通注释配置(使用 @EnableStateMachine 和适配器(和手动构建器之间存在概念上的区别。后者实际上是为了在 spring 应用程序上下文之外使用,虽然您可以将从中创建的机器注册为 bean(就像您尝试的那样(,但不会应用许多自动配置。我可能需要在测试中更加关注这个用例(用户从注册为 @Bean 的构建器返回机器(。

  1. 如果在使用@EnableStateMachine创建两台机器时获得NPE,这是我需要调查的错误。您应该使用name字段,@EnableStateMachine指示如果要创建多台机器,适配器/javaconfig 将使用 Bean 名称。 @EnableStateMachine默认为 Bean 名称stateMachine并且具有多个具有相同名称的 @EnableStateMachine 适配器将尝试配置同一台计算机。对于多台机器,它会像 @EnableStateMachine(name = "sm1") .

  2. TaskExecutor的问题很明显,但是没有一台机器不应该使用您发布的代码,因为我没有看到它在任何地方创建。通常TaskExecutor要么显式设置实例,要么来自 bean 工厂(如果已设置(作为回退。http://docs.spring.io/spring-statemachine/docs/1.0.0.RELEASE/reference/htmlsingle/#statemachine-config-commonsettings 配置接口中有一些用于设置这些的钩子。

  3. 这些默认使用的示例@EnableStateMachine自动进行上下文集成,这意味着 spring 应用程序上下文事件发布者也被注册(手动构建器的机器不会发生这种情况(,因此创建ApplicationListener的正常方法不再有效 https://github.com/spring-projects/spring-statemachine/blob/master/spring-statemachine-samples/src/main/java/demo/CommonConfiguration.java#L57。您还可以使用StateMachineListenerAdapter并通过配置将它们注册到计算机。

我不会尝试围绕示例中的特定 shell 概念构建任何应用。Shell 只是用来提供从命令行与机器交互的简单方法。我看起来您可以通过为所有机器使用不同的 bean 名称来摆脱所有麻烦,即 @EnableStateMachine(name = "sm1") .

我将尝试根据这些用例创建一些gh问题。人们似乎总是有不同的方式尝试使用这些东西,这是我们在测试中没有预料到的。

我尝试使用存储库模型工厂配置 2 组配置状态机工厂,每组都给出一个名称。

然后,在使用持久配方时,我需要为状态机工厂传递状态机 id 的字符串参数以获取状态机实例并传递它以构造处理程序,并使用处理程序进行更新,如示例中所示。

所以问题来了,如何用参数配置处理程序 bean。与其@Autowired处理程序或任何需要处理程序的东西,不如使用 beanFactory.getBean() 获取它。

仅在配方实施中,它就对我有用。但从技术上讲,它应该使用模型工厂进行配置。

最新更新