Java Code Examples for org.springframework.statemachine.StateMachine#start()

The following examples show how to use org.springframework.statemachine.StateMachine#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StateMachineBuilderIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenUseStateMachineBuilder_thenBuildSuccessAndMachineWorks() throws Exception {
    StateMachineBuilder.Builder<String, String> builder = StateMachineBuilder.builder();
    builder.configureStates().withStates()
            .initial("SI")
            .state("S1")
            .end("SF");

    builder.configureTransitions()
            .withExternal()
            .source("SI").target("S1").event("E1")
            .and().withExternal()
            .source("S1").target("SF").event("E2");

    StateMachine machine = builder.build();

    machine.start();

    machine.sendEvent("E1");
    assertEquals("S1", machine.getState().getId());

    machine.sendEvent("E2");
    assertEquals("SF", machine.getState().getId());
}
 
Example 2
Source File: MachineFactory.java    From agile-service-old with Apache License 2.0 5 votes vote down vote up
private StateMachine<String, String> buildInstance(Long organizationId, String serviceCode, Long stateMachineId) {
    StateMachineBuilder.Builder<String, String> builder = instanceCache.getBuilder(stateMachineId);
    if (builder == null) {
        builder = getBuilder(organizationId, serviceCode, stateMachineId);
        logger.info("build StateMachineBuilder successful,stateMachineId:{}", stateMachineId);
        instanceCache.putBuilder(stateMachineId, builder);
    }
    StateMachine<String, String> smInstance = builder.build();
    smInstance.start();
    return smInstance;
}
 
Example 3
Source File: StateMachineTest.java    From spring-statemachine-learning with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    StateMachine<StateEnum, EventEnum> stateMachine = stateMachineFactory.getStateMachine();
    stateMachine.start();
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S1);
    stateMachine.sendEvent(EventEnum.E1);
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S1);
    stateMachine.sendEvent(EventEnum.E2);
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S1);
}
 
Example 4
Source File: StatemachineService.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
public void execute(Integer businessId, TurnstileEvents event, Map<String, Object> context) {
    // 利用随记ID创建状态机,创建时没有与具体定义状态机绑定
    StateMachine<TurnstileStates, TurnstileEvents> stateMachine = stateMachineFactory.getStateMachine(UUID.randomUUID());
    stateMachine.start();
    try {
        // 在BizStateMachinePersist的restore过程中,绑定turnstileStateMachine状态机相关事件监听
        stateMachinePersist.restore(stateMachine, businessId);
        // 本处写法较为繁琐,实际为注入Map<String, Object> context内容到message中
        MessageBuilder<TurnstileEvents> messageBuilder = MessageBuilder
                .withPayload(event)
                .setHeader("BusinessId", businessId);
        if (context != null) {
            context.entrySet().forEach(p -> messageBuilder.setHeader(p.getKey(), p.getValue()));
        }
        Message<TurnstileEvents> message = messageBuilder.build();

        // 发送事件,返回是否执行成功
        boolean success = stateMachine.sendEvent(message);
        if (success) {
            stateMachinePersist.persist(stateMachine, businessId);
        } else {
            System.out.println("状态机处理未执行成功,请处理,ID:" + businessId + ",当前context:" + context);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stateMachine.stop();
    }
}
 
Example 5
Source File: StateMachineAdapter.java    From spring-statemachine-learning with Apache License 2.0 4 votes vote down vote up
public StateMachine<S, E> create() {
    StateMachine<S, E> stateMachine = stateMachineFactory.getStateMachine();
    stateMachine.start();
    return stateMachine;
}