Skip to content

The extended variable set for the first time after the state machine is initialized is null when it is obtained #1181

Open
@SoldierRMB

Description

@SoldierRMB

I am using Eclipse UML for the state machine configuration. The language is set in another thread and is the first extension variable set in the Bean Action. It seems that only in this case does this happen. I have used Guard to guard the transition to ensure that there are no errors. Only when Guard is true can the second Bean Action be executed, even in a separate thread. StateMachineHelper is just a tool class for getting extended variables. In the StateMachine instance passed in, I have obtained the extended variable value of language through debug, but it is null when I actually get it. I have tested multiple times and verified that this only happens when the state machine is initialized for the first time, even if I have used ApplicationRunner to get a state machine, set a variable, and then delete it...
Here is some of my code:

@Bean
Action<String, String> action1() {
  return context -> {
    StateMachine<String, String> stateMachine = context.getStateMachine();
    String text = (String) context.getExtendedState().getVariables().get("text");

    this.requestService.detectLanguage(text)
        .flatMap(language -> {
          List<String> supportedLanguages = List.of("xx1", "xx2", "xx3");
          boolean isLanguageSupported = supportedLanguages.contains(language);
          Map<String, Object> languageMap = new HashMap<>();
          languageMap.put("language", language);
          languageMap.put("isLanguageSupported", isLanguageSupported);

          StateMachineHelper.putExtendedStateVariable(stateMachine, "language", language);
          StateMachineHelper.putExtendedStateVariable(stateMachine, "isLanguageSupported",
              isLanguageSupported);

          return Mono.empty();
        })
        .subscribeOn(Schedulers.boundedElastic())
        .subscribe();
  };
}

@Bean
Action<String, String> action2() {
  return context -> {
    StateMachine<String, String> stateMachine = context.getStateMachine();
    String language = StateMachineHelper.getExtendedStateVariable(stateMachine, "language", String.class); // Got null value here
  };
}

@Bean
Guard<String, String> isLanguageSupportedGuard() {
  return context -> this.booleanVariableValue(context, "isLanguageSupported");
}

private boolean booleanVariableValue(StateContext<String, String> context, String extendedVariableKey) {
  StateMachine<String, String> stateMachine = context.getStateMachine();
  boolean hasExtendedVariable = StateMachineHelper.containsKey(stateMachine, extendedVariableKey);
  if (hasExtendedVariable) {
    return StateMachineHelper.getExtendedStateVariable(stateMachine, extendedVariableKey, Boolean.class);
  } else {
    return false;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/need-triageTeam needs to triage and take a first look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions