Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SoldierRMB opened this issue Feb 20, 2025 · 0 comments
Labels
status/need-triage Team needs to triage and take a first look

Comments

@SoldierRMB
Copy link

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;
  }
}
@github-actions github-actions bot added the status/need-triage Team needs to triage and take a first look label Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/need-triage Team needs to triage and take a first look
Projects
None yet
Development

No branches or pull requests

1 participant