在Spring Batch中以编程方式运行作业时出现NoSuchJobException [英] NoSuchJobException when running a job programmatically in Spring Batch

查看:528
本文介绍了在Spring Batch中以编程方式运行作业时出现NoSuchJobException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在启动时运行的作业。我想以编程方式在应用程序的特定位置运行此作业,而不是在启动应用程序时运行。

I have a Job running on startup. I want to run this job programmatically at a particular point of my application, not when I start my app.

在启动时运行我没有问题,但是我遇到了当我尝试以编程方式运行它时,出现 NoSuchJobException(未注册名称为[importCityFileJob]的作业配置)。

When running on startup I have no problem, but I got a "NoSuchJobException" (No job configuration with the name [importCityFileJob] was registered) when I try to run it programmatically.

在网络上浏览后,我认为这是与JobRegistry相关的问题,但我不知道如何解决。

After looking on the web, I think it's a problem related to JobRegistry, but I don't know how to solve it.

注意:我的整个批处理配置是以编程方式设置,我不使用任何XML文件来配置我的批处理和工作。这是我遇到的大部分问题,虽然我没有示例...

Note : my whole batch configuration is set programmatically, I don't use any XML file to configure my batch and my job. That's a big part of my problem while I lack the examples...

这是我运行作业的代码:

Here is my code to run the 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";
}

我的工作宣言:

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

(我尝试替换 importCityFileJob importFileJob 在我的runBatch方法中,但是没有用)

(I tried to replace importCityFileJob by importFileJob in my runBatch method, but it didn't work)

我的BatchConfiguration文件包含上面的作业声明,步骤声明,itemReader / itemWriter / itemProcessor,仅此而已。
我使用 @EnableBatchProcessing 注释。

My BatchConfiguration file contains the job declaration above, a step declaration, the itemReader/itemWriter/itemProcessor, and that's all. I use the @EnableBatchProcessing annotation.

我是Spring Batch&的新手。我陷入了这个问题。

I'm new to Spring Batch & I'm stuck on this problem. Any help would be welcome.

谢谢

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

Edit : I've solved my problem. I wrote my solution in the answers

推荐答案

A JobRegistry 不会自我填充。在您的示例中,您正在创建一个新实例,然后尝试从中获取作业,而没有首先对其进行注册。通常, JobRegistry AutomaticJobRegistrar 一起配置为Bean,它将在启动时将所有作业加载到注册器中。这并不意味着它们将被执行,只需注册即可,以便以后使用。

A JobRegistry won't populate itself. In your example, you're creating a new instance, then trying to get the job from it without having registered it in the first place. Typically, the JobRegistry is configured as a bean along with an AutomaticJobRegistrar that will load all jobs into the registrar on startup. That doesn't mean they will be executed, just registered so they can be located later.

如果您使用的是Java配置,则应该使用<$自动进行c $ c> @EnableBatchProcessing 注释。有了该批注,您只需注入提供的 JobRegistry 并且作业应该已经存在。

If you're using Java configuration, this should happen automatically using the @EnableBatchProcessing annotation. With that annotation, you'd just inject the provided JobRegistry and the jobs should already be there.

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

You can read more about the @EnableBatchProcessing in the documentation here: 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

You can also read about the AutomaticJobRegistrar in the documentation here: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.html

这篇关于在Spring Batch中以编程方式运行作业时出现NoSuchJobException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆