在Spring Batch中以编程方式运行作业时出现NoSuchJobException

Car*_*rrm 4 configuration jobs spring-batch

我在启动时运行了一个Job.我希望以编程方式在我的应用程序的特定位置运行此作业,而不是在我启动应用程序时.

在启动时运行时我没有问题,但是No job configuration with the name [importCityFileJob] was registered当我尝试以编程方式运行它时,我得到了一个"NoSuchJobException"().

在网上看后,我认为这是与JobRegistry有关的问题,但我不知道如何解决它.

注意:我的整个批处理配置是以编程方式设置的,我不使用任何XML文件来配置我的批处理和我的工作.这是我问题的一个重要部分,而我缺少例子......

这是我运行Job的代码:

public String runBatch() {
    try {
        JobLauncher launcher = new SimpleJobLauncher();
        JobLocator locator = new MapJobRegistry();
        Job job = locator.getJob("importCityFileJob");
        JobParameters jobParameters = new JobParameters(); // ... ?
        launcher.run(job, jobParameters);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Something went wrong");
    }
    return "Job is running";
}
Run Code Online (Sandbox Code Playgroud)

我的工作声明:

@Bean
public Job importCityFileJob(JobBuilderFactory jobs, Step step) {
    return jobs.get("importFileJob").incrementer(new RunIdIncrementer()).flow(step).end().build();
}
Run Code Online (Sandbox Code Playgroud)

(我试过,以取代importCityFileJobimportFileJob我runBatch方法,但它没有工作)

我的BatchConfiguration文件包含上面的作业声明,步骤声明,itemReader/itemWriter/itemProcessor,以及所有.我使用@EnableBatchProcessing注释.

我是Spring Batch的新手,我坚持这个问题.欢迎任何帮助.

谢谢


编辑:我已经解决了我的问题.我在答案中写了我的解决方案

Car*_*rrm 21

以下是我必须要解决的问题:

将以下Bean添加到BatchConfiguration:

@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
    JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
    jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
    return jobRegistryBeanPostProcessor;
}
Run Code Online (Sandbox Code Playgroud)

通过@AutowiredJobRegistry 替换JobLocator,并使用@AutowiredJobLauncher而不是创建一个.我的run方法现在有以下代码:

@Autowired
private JobRegistry jobRegistry;

@Autowired
private JobLauncher launcher;

public String runBatch() {
    try {
        Job job = jobRegistry.getJob("importCityFileJob");
        JobParameters jobParameters = new JobParameters();
        launcher.run(job, jobParameters);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Something went wrong");
    }
    return "OK";
}
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助别人.


Mic*_*lla 5

AJobRegistry不会填充自身。在您的示例中,您正在创建一个新实例,然后尝试从中获取作业,而无需首先注册它。通常,将JobRegistry配置为 bean 以及AutomaticJobRegistrar在启动时将所有作业加载到注册器中的 bean 。这并不意味着它们将被执行,只是被注册以便以后可以定位它们。

如果您使用 Java 配置,这应该使用@EnableBatchProcessing注释自动发生。使用该注释,您只需注入所提供的JobRegistry,并且作业应该已经存在。

您可以@EnableBatchProcessing在此处阅读有关文档的更多信息:http : //docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.html

您还可以AutomaticJobRegistrar在此处阅读文档中的内容:http : //docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.html

  • 谢谢你的解释。我已经在使用`@EnableBatchProcessing` 注释(阅读文档后,我只添加了`modular=true` 部分),但是AutomaticJobRegistrar 不起作用,我仍然有错误。如果我使用`@EnableBatchProcessing(modular=true)`,我应该在我的批处理配置中添加更多的东西,或者 AutomaticJobRegistrar 应该已经在这里了?这只是一个了解发生了什么的问题,而我已经使用 JobRegistryBeanPostProcessor 解决了我的问题。 (4认同)