gpt4 book ai didi

java - 如何向 Spring 状态机 Action 添加动态参数?

转载 作者:行者123 更新时间:2023-12-01 22:51:46 24 4
gpt4 key购买 nike

我有这个简单的状态机配置:

@Configuration 
@EnableStateMachine
public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapter<State, Boolean> {

@Override
public void configure(StateMachineStateConfigurer<State, Boolean> states) throws Exception {
states.withStates()
.initial(State.INITIAL)
.states(EnumSet.allOf(State.class));
}

@Override
public void configure(StateMachineTransitionConfigurer<State, Boolean> transitions) throws Exception {
transitions
.withExternal()
.source(State.INITIAL)
.target(State.HAS_CUSTOMER_NUMBER)
.event(true)
.action(retrieveCustomerAction())
// here I'd like to retrieve the customer from this action, like:
// stateMachine.start();
// stateMachine.sendEvent(true);
// stateMachine.retrieveCustomerFromAction();
.and()
.withExternal()
.source(State.INITIAL)
.target(State.NO_CUSTOMER_NUMBER)
.event(false)
.action(createCustomerAction());
// here I'd like to send the customer instance to create, like:
// stateMachine.start();
// stateMachine.sendEvent(false);
// stateMachine.sendCustomerToAction(Customer customer);
}

@Bean
public Action<State, Boolean> retrieveCustomerAction() {
return ctx -> System.out.println(ctx.getTarget().getId());
}

@Bean
public Action<State, Boolean> createCustomerAction() {
return ctx -> System.out.println(ctx.getTarget().getId());
}

}

是否可以改进 Action 定义,以便能够通过动态参数与它们进行交互?我如何将消费者或提供者行为添加到这些操作中?

最佳答案

Is it possible to improve actions definition to be able to interact with them with dynamics parameters?

是的,这是可能的。您可以将变量存储在上下文存储中,然后在任何您想要的地方检索。

public class Test {

@Autowired
StateMachine<State, Boolean> stateMachine;

public void testMethod() {

stateMachine.getExtendedState().getVariables().put(key, value);
stateMachine.start();
stateMachine.sendEvent(true);
}
}

并且您可以使用键从上下文中检索该值。假设该值是 String 类型,那么可以像这样检索它:-

    @Bean
public Action<State, Boolean> retrieveCustomerAction() {
return ctx -> {
String value = ctx.getExtendedState().get(key, String.class);
// Do Something
};
}

更多信息可以引用linkthis

How could I add consumer or provider behaviors to those actions?

您能否详细说明一下这个问题

关于java - 如何向 Spring 状态机 Action 添加动态参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60317146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com