无法在Spring Batch中启动Asyc Job

时间:2018-01-15 12:26:59

标签: java spring spring-mvc spring-batch

我想创建一个可以异步调用Spring Batch的休息服务,这样我就可以启动作业,它可以在后台运行,并且可以关闭HTTP连接。

我正在使用JobOperator以下列配置启动作业:

@Bean
    public JobLauncher jobLauncher() {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
        simpleAsyncTaskExecutor.setConcurrencyLimit(10);
        jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
        return jobLauncher;
    }


    @Bean
    public JobOperator jobOperator() {
        SimpleJobOperator jobOperator = new SimpleJobOperator();
        jobOperator.setJobExplorer(jobExplorer);
        jobOperator.setJobLauncher(jobLauncher());
        jobOperator.setJobRegistry(jobRegistry);
        jobOperator.setJobRepository(jobRepository);
        return  jobOperator;
    };

通过上述配置,我无法按照我想要的方式启动作业。 Here我读到我们可以在不同的线程中启动工作,但没有观察到。任何想法为什么会观察到这种行为?

1 个答案:

答案 0 :(得分:3)

最后,我得到了解决方案。问题是在宣告豆。 JobLauncher bean被Spring批处理的默认实现覆盖。我做了以下重命名,它按预期工作:

@Bean
public SimpleJobLauncher simpleJobLauncher() {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor());
    return jobLauncher;
}

public SimpleAsyncTaskExecutor simpleAsyncTaskExecutor() {
    SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
    simpleAsyncTaskExecutor.setConcurrencyLimit(10);
    return simpleAsyncTaskExecutor;
}

@Bean
public JobOperator jobOperator() {
    SimpleJobOperator jobOperator = new SimpleJobOperator();
    jobOperator.setJobLauncher(simpleJobLauncher());
    jobOperator.setJobExplorer(jobExplorer);
    jobOperator.setJobRegistry(jobRegistry);
    jobOperator.setJobRepository(jobRepository);
    return  jobOperator;
};