Skip to content

@WithStateMachine may register too many processors #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
playaround88 opened this issue Jun 2, 2017 · 3 comments
Closed

@WithStateMachine may register too many processors #370

playaround88 opened this issue Jun 2, 2017 · 3 comments
Labels
type/bug Is a bug report
Milestone

Comments

@playaround88
Copy link

playaround88 commented Jun 2, 2017

hi admin
i am newer of ssm, and i write the simple example of "quick start".
when i run, i found the MyBean with @WithStateMachine run tiwce,it's depressing!
can help me? (english is poor, sorry)
logs

信息: Starting beans in phase 0
INFO  [demo] 2017-06-02 17:46:22,372 [com.ai.ssm.business.MyBean.toState1] - to-----state1:1
INFO  [demo] 2017-06-02 17:46:22,377 [com.ai.ssm.business.MyBean.toState1] - to-----state1:2
六月 02, 2017 5:46:22 下午 org.springframework.statemachine.support.LifecycleObjectSupport start
信息: started org.springframework.statemachine.support.DefaultStateMachineExecutor@69e9e330
六月 02, 2017 5:46:22 下午 org.springframework.statemachine.support.LifecycleObjectSupport start
信息: started STATE2 STATE1  / STATE1 / uuid=ec77819b-441a-4a64-bfe5-f7912a555e57 / id=myMachine
INFO  [demo] 2017-06-02 17:46:22,388 [com.ai.ssm.business.MyBean.toState2] - to-----state2:3
INFO  [demo] 2017-06-02 17:46:22,388 [com.ai.ssm.business.MyBean.toState2] - to-----state2:4
INFO  [demo] 2017-06-02 17:46:22,389 [com.ai.ssm.business.MyBean.toState1] - to-----state1:5
INFO  [demo] 2017-06-02 17:46:22,390 [com.ai.ssm.business.MyBean.toState1] - to-----state1:6

code is show below

@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States,Events> {
	@Override
	public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
		config.withConfiguration()
			.machineId("myMachine");
	}

	@Override
	public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
		states.withStates()
			.initial(States.STATE1)
			.states(EnumSet.allOf(States.class));
	}

	@Override
	public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
		transitions
			.withExternal()
				.source(States.STATE1).target(States.STATE2)
				.event(Events.EVENT1)
				.and()
			.withExternal()
				.source(States.STATE2).target(States.STATE1)
				.event(Events.EVENT2);
	}
}
@WithStateMachine(id="myMachine")
public class MyBean {
	private static final Logger LOG=LoggerFactory.getLogger(MyBean.class);
	private static AtomicInteger i=new AtomicInteger(0);

    @OnTransition(target = "STATE1")
    public void toState1() {
    	int r=i.incrementAndGet();
    	LOG.info("to-----state1:"+r);
    }

    @OnTransition(target = "STATE2")
    public void toState2() {
    	int r=i.incrementAndGet();
    	LOG.info("to-----state2:"+r);
    }
}
public static void main(String[] args) throws Exception {
		AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
		ac.register(AppConfig.class);
		ac.register(StateMachineConfig.class);
		ac.scan("com.ai.ssm.business"); // where "MyBean" is 
		ac.refresh();
		
		StateMachine<States, Events> stateMachine = ac.getBean(StateMachine.class);
		stateMachine.start();
		stateMachine.sendEvent(Events.EVENT1);
		stateMachine.sendEvent(Events.EVENT2);
		
		ac.close();
	}
@playaround88
Copy link
Author

maybe i should ask in stackoverflow, but stackoverflow is not available in china. some external js failed to load, and cannot sign up!

@jvalkeal
Copy link
Contributor

jvalkeal commented Jun 7, 2017

This is a some sort of a bug I need to fix. It's caused by you using

config
  .withConfiguration()
    .machineId("myMachine");

Proper way to register statemachine with a non-default bean name is(without using machineId("myMachine"))

@Configuration
@EnableStateMachine(name = "myMachine")
class XXX {}

Which then works correctly with @WithStateMachine(id="myMachine") where you should get method called only once.

Anyway, afaik it should not break like this and there's is some sort of bug around registration I need to fix. Thanks for finding this!

@jvalkeal jvalkeal added the type/bug Is a bug report label Jun 7, 2017
@jvalkeal jvalkeal added this to the 1.2.6.RELEASE milestone Jun 7, 2017
@jvalkeal jvalkeal changed the title @WithStateMachine method execute tiwce @WithStateMachine method executed twice Jun 7, 2017
@jvalkeal jvalkeal changed the title @WithStateMachine method executed twice @WithStateMachine may register too many processors Jun 8, 2017
jvalkeal added a commit to jvalkeal/spring-statemachine that referenced this issue Jun 8, 2017
- Change processor registration so that if @WithStateMachine is
  used with 'id', 'name' is skipped because it defaults to 'stateMachine'
  and would cause extra registration.
- Fixes spring-projects#370
@jvalkeal
Copy link
Contributor

jvalkeal commented Jun 8, 2017

Merged per 1dd3790

@jvalkeal jvalkeal closed this as completed Jun 8, 2017
jvalkeal added a commit that referenced this issue Jul 9, 2017
- Change processor registration so that if @WithStateMachine is
  used with 'id', 'name' is skipped because it defaults to 'stateMachine'
  and would cause extra registration.
- Relates #307
- Backport #370
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Is a bug report
Projects
None yet
Development

No branches or pull requests

2 participants