Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring状态机并行实例中子状态的异步执行_Java_Spring_Spring Statemachine - Fatal编程技术网

Java spring状态机并行实例中子状态的异步执行

Java spring状态机并行实例中子状态的异步执行,java,spring,spring-statemachine,Java,Spring,Spring Statemachine,我试图从一个uml模型创建几个状态机实例。我使用stateMachineFactory。我希望这些机器独立和异步工作 如果我只使用“基本”状态,一切都会很好。机器实例可以独立地转到stateB和StateC。然而,当我使用区域和子状态(stateD)时,机器实例一个接一个地执行操作(insidestate1)。请看 我发现状态是通过stateMachineTaskExecutor(默认为SyncTaskExecutor)执行的,但子状态是通过taskScheduler(默认为Concurrent

我试图从一个uml模型创建几个状态机实例。我使用stateMachineFactory。我希望这些机器独立和异步工作

如果我只使用“基本”状态,一切都会很好。机器实例可以独立地转到stateB和StateC。然而,当我使用区域和子状态(stateD)时,机器实例一个接一个地执行操作(insidestate1)。请看

我发现状态是通过stateMachineTaskExecutor(默认为SyncTaskExecutor)执行的,但子状态是通过taskScheduler(默认为ConcurrentTaskScheduler)执行的

这是配置:

@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends StateMachineConfigurerAdapter<String, String> {

    @Autowired
    StateMachineComponentResolver<String, String> stateMachineComponentResolver;

    @Bean
    public StateMachineModelFactory<String, String> modelFactory() {
        UmlStateMachineModelFactory umlStateMachineModelFactory = new UmlStateMachineModelFactory("classpath:uml/testSM1.uml");
        umlStateMachineModelFactory.setStateMachineComponentResolver(stateMachineComponentResolver);
        return umlStateMachineModelFactory;
    }

    @Override
    public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
        model
                .withModel()
                .factory(modelFactory());
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
        config
                .withConfiguration()
               // .taskExecutor()  // I tried various taskExecutors
               // .taskScheduler() // I tried various taskSchedulers
                ;
    }
}
@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
    config
        .withConfiguration()
            //other configs
            .taskExecutor(myAsyncTaskExecutor())
}

public TaskExecutor myAsyncTaskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    return taskExecutor;
}
@配置
@EnableStateMachineFactory
公共类StateMachineConfig扩展了StateMachineConfigureAdapter{
@自动连线
StateMachineComponentResolver StateMachineComponentResolver;
@豆子
public StateMachineModelFactory模型工厂(){
UmlStateMachineModelFactory UmlStateMachineModelFactory=新的UmlStateMachineModelFactory(“类路径:uml/testSM1.uml”);
umlStateMachineModelFactory.setStateMachineComponentResolver(stateMachineComponentResolver);
返回umlStateMachineModelFactory;
}
@凌驾
public void configure(StateMachineModelConfigurer模型)引发异常{
模型
.withModel()
.factory(modelFactory());
}
@凌驾
public void configure(stateMachineConfiguration配置器配置)引发异常{
配置
.withConfiguration()
//.taskExecutor()//我尝试了各种taskExecutor
//.taskScheduler()//我尝试了各种taskScheduler
;
}
}

从同一模型中获得多个状态机实例的正确方法是什么?

可以通过以下方法获得多个SM实例

在中创建的配置适用于所有SM实例

Spring状态机用于区域执行(不管是顶层还是嵌套区域),默认情况下是同步的。要实现异步执行,需要覆盖默认任务执行器。这可以通过以下配置实现:

@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends StateMachineConfigurerAdapter<String, String> {

    @Autowired
    StateMachineComponentResolver<String, String> stateMachineComponentResolver;

    @Bean
    public StateMachineModelFactory<String, String> modelFactory() {
        UmlStateMachineModelFactory umlStateMachineModelFactory = new UmlStateMachineModelFactory("classpath:uml/testSM1.uml");
        umlStateMachineModelFactory.setStateMachineComponentResolver(stateMachineComponentResolver);
        return umlStateMachineModelFactory;
    }

    @Override
    public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
        model
                .withModel()
                .factory(modelFactory());
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
        config
                .withConfiguration()
               // .taskExecutor()  // I tried various taskExecutors
               // .taskScheduler() // I tried various taskSchedulers
                ;
    }
}
@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
    config
        .withConfiguration()
            //other configs
            .taskExecutor(myAsyncTaskExecutor())
}

public TaskExecutor myAsyncTaskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    return taskExecutor;
}

用于操作执行(与状态或转换关联的操作),而不用于子状态。

感谢您的回复。你的回答启发我检查了一些东西。我创建了两个实现接口动作的类,并通过StateMachineComponentResolver将其添加到状态机:与转换关联的动作(StateA->StateB)和与stateC关联的动作(函数行为:insideStateC)。我发现TaskExecutor用于与转换和保护关联的操作,而TaskScheduler用于与状态关联的操作执行。我说得对吗?我必须重写TaskScheduler(在ThreadPoolTaskScheduler上更改)。是的,如果希望以异步方式执行操作,则必须用一个生成多个线程的线程重写默认taskScheduer。