aful
aful

Reputation: 1

why does not spring statemachine work,when i create statemachine by stateMachineFactory?

1、I created machine configuration data to database by web ;

@Override
public void createMachine(MachineCfgDto data) {
    String machineId = data.getName();
    Set<String> status = new HashSet<String>(); 
    String initStatus = null;
    for(int i=0;i<data.getCfg().size();i++) {
        if(i==0) {
            initStatus =data.getCfg().get(i).getSrc();
        }
        status.add(data.getCfg().get(i).getSrc());
        status.add(data.getCfg().get(i).getTarget());
    }
    Map<String,JpaRepositoryState> machineStatus = new HashMap<String,JpaRepositoryState>();
    for(String s : status) {
        JpaRepositoryState mStatus = stateRepository.save(new JpaRepositoryState(machineId,s,initStatus.equals(s)?true:false));
        machineStatus.put(s, mStatus);
    }

    for(CfgData cfg : data.getCfg()) {
        JpaRepositoryTransition trans = new JpaRepositoryTransition(machineStatus.get(cfg.getSrc()),machineStatus.get(cfg.getTarget()),cfg.getEvent());
        trans.setKind(TransitionKind.EXTERNAL);
        transitionRepository.save(trans);
    }
}

2、I created statemachine by StateMachineFactory and start it ,then I send event by statemachine's sendEvent method, but it dose not work well;

@Override
public void submit(String machineId, String curStatus,Long[] ids) {
    StateMachine<String, String> machine = stateMachineFactory.getStateMachine(machineId);
    PersistStateMachineHandler handler = new PersistStateMachineHandler(machine);
    handler.addPersistStateChangeListener(new TestListener(jdbcTemplate));
    System.out.println("状态机是否终结:" + machine.isComplete() + "machine-uuid:"+machine.getUuid());
    State<String, String> s = machine.getInitialState();
    System.out.println("状态id:" + s.getId() );
    if(machine.isComplete()) {
        machine.start();
    }
    machine.sendEvent("start");
    System.out.println("状态机是否终结:" + machine.isComplete()+ machine.getState().getId());
    handler.handleEventWithState(MessageBuilder.withPayload("start").setHeader("ids", ids).build(), curStatus);

}

after sendEvent,the machine's current state is not change,it's initial state 'S0' all the time,please tell me why or how to corrent using spring statemachine,thanks!

and in this case, I use persit ,but it don't work too

Upvotes: 0

Views: 266

Answers (1)

aful
aful

Reputation: 1

sorry,I found the problem! when I create the transation ,I forget set machineId to it.So,using the machine by sendEvent, haven't transation.

Upvotes: 0

Related Questions