我想使用timerOnce进行延迟,但是它不起作用

时间:2019-04-15 07:30:45

标签: timer spring-statemachine

我想从状态 REPEATED_EXPORT_TO_A 转到状态 EXPORT_TO_A_AGAIN ,但动作 reexportEvent()尚未执行。我有以下状态机:

    public StateMachineBuilder.Builder<StatusId, ActionId> construct(StateMachineBuilder.Builder<StatusId, ActionId> builder) throws Exception {
builder.configureStates().withStates()
        .states(ImmutableSet.of(OBTAINED_BY_B, FOR_EXPORT_TO_A, EXPORTING_TO_A,
                EXPORT_TO_A_ERROR, EXPORTING_TO_A_TIMEOUT,
                RECEIVED_BY_A, NOT_RECEIVED_BY_A, RECEIVED_A_ERROR, REPEATED_EXPORT_TO_A))
        .state(FOR_EXPORT_TO_A, checkPassedAction(), null)
        .state(EXPORTING_TO_A, exportedAction(), null)

        .choice(EXPORTED_TO_A_OR_NOT)
        .choice(EXPORT_TO_A_AGAIN);

builder.configureTransitions().withExternal()
        .source(OBTAINED_BY_B).target(FOR_EXPORT_TO_A)
        .and().withExternal()
        .source(FOR_EXPORT_TO_A).target(EXPORTED_TO_A_OR_NOT)

        .and().withChoice()
        .source(EXPORTED_TO_A_OR_NOT)
        .first(EXPORTING_TO_A, exportingToAGuardSsm)
        .last(REPEATED_EXPORT_TO_A)

        .and().withExternal()
        .source(REPEATED_EXPORT_TO_A)
        .target(EXPORT_TO_A_AGAIN)
        .event(REEXPORT_TO_A)

        .and().withChoice()
        .source(EXPORT_TO_A_AGAIN)
        .first(EXPORTING_TO_A, exportingToAGuardSsm)
        .then(EXPORT_TO_A_ERROR, checkRepeatExportGuard)
        .then(REPEATED_EXPORT_TO_A, repeatExportToBGuardSsm)
        .last(EXPORT_TO_A_ERROR)

此内部转换会延迟

   .and().withInternal()
    .source(REPEATED_EXPORT_TO_A)
    .action(reexportEvent())
    .timerOnce(5000)

但此操作尚未执行

 private Action<StatusId, ActionId> reexportEvent() {
    //some code
      return context -> {
          Doc doc = SsmUtil.getDoc(context);
          doc.setRepeatCount(doc.getRepeatCount() + 1);
          context.getStateMachine().sendEvent(REEXPORT_TO_A);
      };
  }

1 个答案:

答案 0 :(得分:0)

对于Spring State Machine中的触发器,有一件关键的事情要理解-它们与一个状态相关联,您需要保持该状态才能执行触发器。延迟从您进入状态的那一刻开始-如果您在达到延迟时间之前从状态退出,则触发器不会执行。

示例:

  • 指定状态“ A”的触发器,延迟时间为5秒

  • 进入状态“ A”

  • 第二个第二退出状态“ A”

触发器不会执行,因为您在定义的时间延迟到期之前已从状态“ A”退出。

签出演示此行为here的单元测试(TestTriggersDelay)。