spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.53k stars 599 forks source link

WithStateMachine annotation not working when using StateMachineService and StateMachineFactory with dynamically generated IDs #1031

Open Per0xyde opened 2 years ago

Per0xyde commented 2 years ago

The issue is quite similar to #941, except that I am using StateMachineService to create a state machine with dynamically generated ID and persist it.


I attach a small sample to illustrate the issue : StateMachineExample.txt pom.txt


My goal is :

  1. To send events to a given state machine, which ID was dynamically generated
  2. To persist the state machine, which is done thanks to the default implementation of the StateMachineService.

To do so :

According to the documentation, if I don't specify any ID in @WithStateMachine, it will match all state machines generated by the factory.

When using StateMachineFactory to generate state machines the state machine using dynamic provided id, bean name will default to stateMachine it's not possible to use @WithStateMachine (id = "some-id") since id is only known at runtime.

In such a cases, use either @WithStateMachine or @WithStateMachine(name = "stateMachine") and all state machines generated by the factory will be atached to your bean or beans.

My issue :

Whenever I send an event to a state machine created at runtime thanks to StateMachineService, the method annotated with @OnStateChanged is ignored. It only works if I specifically write an ID @WithStateMachine(id = "1"), and thus it works only for the state machine which will have the dynamically gerated ID of 1.

BrunoDelor commented 5 months ago

Hello, I am facing the same issue.

I want to define a state machine factory with the goal to instantiate any number of machine from that factory to be ran in parallel. Because of this, my IDs will be dynamically generated

I wanted to use @WithStateMachine(name = FACTORY_NAME) in order to use context integration for a cleaner project because, as OP stated, the documentation pretends that every machine with from the factory identified by name should be bound to the annotated methods of the class.

As it currently stands this is not correct. it doesn't seems like the name defined in the annotation is ever used anywhere in the spring code. The annotation is fetched a couple times but only to check the contextEvent param. After that the annotations details are systematically discarded without passing the name to anything