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

The following examples show how to use org.springframework.statemachine.StateMachine#sendEvent() . 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: MachineFactory.java    From agile-service-old with Apache License 2.0 6 votes vote down vote up
/**
 * 开始实例
 *
 * @param serviceCode
 * @param stateMachineId
 * @return
 */
public ExecuteResult startInstance(Long organizationId, String serviceCode, Long stateMachineId, InputDTO inputDTO) {
    StateMachine<String, String> instance = buildInstance(organizationId, serviceCode, stateMachineId);
    //存入instanceId,以便执行guard和action
    instance.getExtendedState().getVariables().put(INPUT_DTO, inputDTO);
    //执行初始转换
    Long initTransformId = transformService.getInitTransform(organizationId, stateMachineId).getId();
    instance.sendEvent(initTransformId.toString());

    //缓存实例
    instanceCache.putInstance(serviceCode, stateMachineId, inputDTO.getInstanceId(), instance);
    Object obj = instance.getExtendedState().getVariables().get(EXECUTE_RESULT);
    ExecuteResult executeResult = new ExecuteResult();
    if (obj != null) {
        executeResult = (ExecuteResult) obj;
    } else {
        executeResult.setSuccess(false);
        executeResult.setErrorMessage("触发事件失败");
    }
    return executeResult;
}
 
Example 2
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 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: RedisPersistConfigTest.java    From spring-statemachine-learning with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    StateMachine<StateEnum, EventEnum> stateMachine = stateMachineAdapter.create();
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S1);
    stateMachine.sendEvent(EventEnum.E1);
    stateMachineAdapter.persist(stateMachine, "1");
    stateMachine = stateMachineAdapter.restore("1");
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S2);
    StateMachine<StateEnum, EventEnum> stateMachine2 = stateMachineAdapter.create();
    stateMachineAdapter.persist(stateMachine2, "2");
    stateMachine = stateMachineAdapter.restore("2");
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S1);
}
 
Example 5
Source File: StateMachineInEntityPersistTest.java    From spring-statemachine-learning with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    StateMachine<StateEnum, EventEnum> stateMachine = stateMachineAdapter.create();
    Assert.assertEquals(stateMachine.getState().getId(), StateEnum.S1);
    EnityWithSateMachine enityWithSateMachine = new EnityWithSateMachine();
    stateMachine.sendEvent(EventEnum.E1);
    stateMachineAdapter.persist(stateMachine, enityWithSateMachine);
    StateMachine<StateEnum, EventEnum> stateMachine1 = stateMachineAdapter.restore(enityWithSateMachine);
    Assert.assertEquals(stateMachine1.getState().getId(), StateEnum.S2);
}
 
Example 6
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 7
Source File: SkipperStateMachineService.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
private boolean handleMessageAndCheckAccept(Message<SkipperEvents> message, String machineId) {
	StateMachine<SkipperStates, SkipperEvents> stateMachine = stateMachineService.acquireStateMachine(machineId);
	return stateMachine.sendEvent(message);
}
 
Example 8
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Test
public void testInstallDeniedWhileUpgrading() throws Exception {
	Manifest manifest = new Manifest();
	Release release = new Release();
	release.setManifest(manifest);
	Mockito.when(releaseReportService.createReport(any(), any(), any(boolean.class))).thenReturn(new ReleaseAnalysisReport(
			new ArrayList<>(), new ReleaseDifference(), release, release));
	Mockito.when(upgradeStrategy.checkStatus(any()))
			.thenReturn(false);
	Mockito.when(upgradeStrategyFactory.getUpgradeStrategy(any())).thenReturn(upgradeStrategy);

	UpgradeRequest upgradeRequest = new UpgradeRequest();

	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.UPGRADE)
			.setHeader(SkipperEventHeaders.UPGRADE_REQUEST, upgradeRequest)
			.build();

	Message<SkipperEvents> message2 = MessageBuilder
			.withPayload(SkipperEvents.INSTALL)
			.setHeader(SkipperEventHeaders.PACKAGE_METADATA, new PackageMetadata())
			.setHeader(SkipperEventHeaders.INSTALL_PROPERTIES, new InstallProperties())
			.setHeader(SkipperEventHeaders.VERSION, 1)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testInstallDeniedWhileUpgrading");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStateChanged(6)
					.and()
				.build();
	plan.test();

	// install event is not accepted
	boolean accepted = stateMachine.sendEvent(message2);
	assertThat(accepted).isFalse();
}