Spring Statemachine如何持续使用嵌套区域的机器

发布于 2025-01-27 02:18:34 字数 3219 浏览 3 评论 0 原文

我有最后提到的配置的statemachine,我想在数据库中持续存在。我正在关注本教程坚持下去。

但是,当我的statemachine处于parallel_tasks状态时,我在数据库中仅看到一行

//i.sstatic.net/dl4f0.png“ alt =”此处的图像描述在此处”>

是否不建议显示3行(1对于父状态parallel_tasks和2对于子状态unlocking_excessive_point_pointssss_started,processing_payment_payment_started)?

有人可以告诉我如何解决吗?我的配置有什么问题?

@Configuration
@EnableStateMachineFactory(name = "SampleConfig")
@Qualifier("SampleConfig")
public class SampleConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {

    @Autowired
    private JpaPersistingStateMachineInterceptor<OrderState, OrderEvent, String> persister;

    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
        states
                .withStates()
                .initial(OrderState.ORDER_CREATED)
                .state(OrderState.ORDER_CREATED)
                .state(OrderState.PARALLEL_TASKS)
                .end(OrderState.ORDER_COMPLETED)
                .and()
                .withStates()
                .parent(OrderState.PARALLEL_TASKS)
                .region("R1")
                .initial(OrderState.UNLOCKING_EXCESSIVE_POINTS_STARTED)
                .state(OrderState.UNLOCKING_EXCESSIVE_POINTS_STARTED)
                .state(OrderState.UNLOCKED_EXCESSIVE_POINTS)
                .and()
                .withStates()
                .parent(OrderState.PARALLEL_TASKS)
                .region("R2")
                .initial(OrderState.PROCESSING_PAYMENT_STARTED)
                .state(OrderState.PROCESSING_PAYMENT_STARTED)
                .state(OrderState.PROCESSED_PAYMENT)
        ;
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {
        transitions
                .withExternal()
                .source(OrderState.ORDER_CREATED)
                .target(OrderState.PARALLEL_TASKS)
                .event(OrderEvent.ORDER_SUBMITTED_EVENT)
                .and()
                .withExternal()
                .source(OrderState.UNLOCKING_EXCESSIVE_POINTS_STARTED)
                .target(OrderState.UNLOCKED_EXCESSIVE_POINTS)
                .event(OrderEvent.UNLOCKED_POINTS_SUCCESS)
                .and()
                .withExternal()
                .source(OrderState.PROCESSING_PAYMENT_STARTED)
                .target(OrderState.PROCESSED_PAYMENT)
                .event(OrderEvent.PAYMENT_PROCESSED_SUCCESS);
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderState, OrderEvent> config) throws Exception {


        config.withConfiguration()
                .autoStartup(false)
                .regionExecutionPolicy(RegionExecutionPolicy.PARALLEL)
                .and()
                .withPersistence()
                .runtimePersister(persister)
        ;
    }


}

I have statemachine with configuration mentioned at the end, which i want to persist in the database. I am following this tutorial https://docs.spring.io/spring-statemachine/docs/3.1.0/reference/#statemachine-examples-datajpamultipersist to persist it.

However, when my statemachine is in PARALLEL_TASKS state then i see only one row in database

enter image description here

is it not suppose to show 3 rows (1 for parent state PARALLEL_TASKS and 2 for sub-states UNLOCKING_EXCESSIVE_POINTS_STARTED, PROCESSING_PAYMENT_STARTED)?

Can someone please tell me how can i fix it? what is wrong with my configuration?

@Configuration
@EnableStateMachineFactory(name = "SampleConfig")
@Qualifier("SampleConfig")
public class SampleConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {

    @Autowired
    private JpaPersistingStateMachineInterceptor<OrderState, OrderEvent, String> persister;

    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
        states
                .withStates()
                .initial(OrderState.ORDER_CREATED)
                .state(OrderState.ORDER_CREATED)
                .state(OrderState.PARALLEL_TASKS)
                .end(OrderState.ORDER_COMPLETED)
                .and()
                .withStates()
                .parent(OrderState.PARALLEL_TASKS)
                .region("R1")
                .initial(OrderState.UNLOCKING_EXCESSIVE_POINTS_STARTED)
                .state(OrderState.UNLOCKING_EXCESSIVE_POINTS_STARTED)
                .state(OrderState.UNLOCKED_EXCESSIVE_POINTS)
                .and()
                .withStates()
                .parent(OrderState.PARALLEL_TASKS)
                .region("R2")
                .initial(OrderState.PROCESSING_PAYMENT_STARTED)
                .state(OrderState.PROCESSING_PAYMENT_STARTED)
                .state(OrderState.PROCESSED_PAYMENT)
        ;
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {
        transitions
                .withExternal()
                .source(OrderState.ORDER_CREATED)
                .target(OrderState.PARALLEL_TASKS)
                .event(OrderEvent.ORDER_SUBMITTED_EVENT)
                .and()
                .withExternal()
                .source(OrderState.UNLOCKING_EXCESSIVE_POINTS_STARTED)
                .target(OrderState.UNLOCKED_EXCESSIVE_POINTS)
                .event(OrderEvent.UNLOCKED_POINTS_SUCCESS)
                .and()
                .withExternal()
                .source(OrderState.PROCESSING_PAYMENT_STARTED)
                .target(OrderState.PROCESSED_PAYMENT)
                .event(OrderEvent.PAYMENT_PROCESSED_SUCCESS);
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderState, OrderEvent> config) throws Exception {


        config.withConfiguration()
                .autoStartup(false)
                .regionExecutionPolicy(RegionExecutionPolicy.PARALLEL)
                .and()
                .withPersistence()
                .runtimePersister(persister)
        ;
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2025-02-03 02:18:35

好的,我真的不喜欢这么说,因为我是春季机器的忠实拥护者,但是告诉你真相持久性不是它的大装杆之一,大部分时间都在给我感觉,这是这样做的能够说“我们具有持久能力”。

州机器可能非常复杂(查看UML规范),我不认为持久层旨在处理所有这些复杂性。

如果您使用的是弹簧状态机来建模购物图表,预订等在此版本中,您的业务发展了,并且您拥有不同的状态机器设计。

我发现以下框架在上述问题上做得更好。机器用它 event/schema >功能。它还具有一个单独的 persistance 您的层面控制,因此您不会像您上面提到的那样遭受场景。

现在,您上面描述的内容似乎是春季状态机器中的一个错误,您可能会得到一个修复程序,但是我强烈建议您在将更多项目资源投入到Spring State Machine上之前检查Akka框架,Spring State Machine对某些人来说是很棒的问题,但不是所有的。

现在,当您阅读Akka文档时,您会发现它是用Scala编写的,并且使用Scala编程状态机是更自然的(可以使用Java进行编程,但它太笨拙了),如果您不愿意编程在Scala中,可以像我在这些博客中所展示的Scala/Java Hybrid编程,blog1,

在以下 blog3

我知道这不是您要问的内容,但由于开发人员可能与您走同一条路,我喜欢发出公平的警告。

Ok, I really don't like to say this because I am big fan of Spring State Machine but to tell you the truth persistence is not one of its big fortes, it is most of the time giving me the feeling, it is done to be able to say 'look we have persistence capablities'.

State Machines can be quite complex (look to the UML Specification) and I don't believe persistance layer is designed to deal with all of these complexities.

If you are using Spring State Machine for modelling Shopping Charts, Booking, etc let me ask what would be your (Spring State Machine's) strategy when your business case evolves, what happens when you have to restore a State Machine persisted with the previous release and in this release your business evolved and you have complete different State Machine design.

I found the following framework does much better with above mentioned problems, Akka Finite State Machine with it's Event / Schema Evolution capabilities. It has also a separate Persistance layer that you have much better control so you will not suffer scenarios as you mentioned above.

Now what you describe above seems like a bug in Spring State Machine and you might get a fix for it, but I strongly advice you to check Akka Framework before you invest more your project resource to Spring State Machine, Spring State Machine is awesome for some problems but not for everything.

Now when you read the Akka Documentation, you will see that it is written in Scala and it is more natural to program your State Machine with Scala (it is possible to program with Java but it gets too clumsy ), if you are reluctant to program in Scala, it is possible to program it as Scala / Java hybrid as I demonstrated in these blogs blog1, blog2.

Event / Schema Evolution capabilities is also demonstrated in the following blog3.

I know this is not answer for what you are asking but as a developer probably walk the same road as you, I like to give a fair warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文