1,我通过网络创建了数据库的机器配置数据;
@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,我通过StateMachineFactory创建了statemachine并启动它,然后我通过statemachine的sendEvent方法发送事件,但它不能正常工作;
@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);
}
在sendEvent之后,机器的当前状态没有变化,它的初始状态是'S0',请告诉我为什么或如何使用spring statemachine,谢谢!
在这种情况下,我使用persit,但它也不起作用
答案 0 :(得分:0)