如何以一定顺序运行Spring Batch Jobs(Spring Boot)? [英] How to run Spring Batch Jobs in certain order (Spring Boot)?

查看:672
本文介绍了如何以一定顺序运行Spring Batch Jobs(Spring Boot)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot使用Spring Batch进行开发.

I'm developing with Spring Batch using Spring Boot.

我使用Spring Boot提供的最小配置,并定义了一些Jobs(根本没有XML配置).但是当我运行该应用程序时,

I'm with the minimal configuration provided by Spring Boot and defined some Jobs (no XML configuration at all). But when I run the application,

SpringApplication.run(App.class, args);

以任意顺序依次执行作业.

the jobs are sequentially executed in some arbitrary order.

我正在以这种方式在@Configuration带注释的类中定义作业,其余的由Spring完成:

I'm defining the jobs this way in @Configuration annotated classes, Spring do the rest:

@Bean
public Job requestTickets() {
    return jobBuilderFactory.get(Config.JOB_REQUEST_TICKETS)
            .start(stepRequestTickets())
            .build();
}

如何指示框架按特定顺序运行作业?

How can I instruct the framework to run the jobs in a certain order?

此警告可以提示吗? (也许没事)

Could this warning give a hint? (Maybe has nothing to be)

2016-12-29 17:45:33.320  WARN 3528 --- [main] o.s.b.c.c.a.DefaultBatchConfigurer: No datasource was provided...using a Map based JobRepository

推荐答案

1.您首先通过在 application.properties

2.在您的主类中,执行-ApplicationContext ctx = SpringApplication.run(SpringBatchMain.class, args);,假设您的主类名为-SpringBatchMain.java.

2.In your main class, do - ApplicationContext ctx = SpringApplication.run(SpringBatchMain.class, args); assuming your main class is named - SpringBatchMain.java.

这将初始化上下文而不启动任何作业.

This will initialize context without starting any jobs.

3.一旦上下文被初始化,您可以在主类中对此JobLauncher bean执行-JobLauncher jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");Autowired,并通过调用jobLauncher.run(job, jobParameters)以特定的顺序依次启动特定的作业.

3.Once context is initialized, either you can do - JobLauncher jobLauncher = (JobLauncher) ctx.getBean("jobLauncher"); or do Autowired for this JobLauncher bean in main class and launch specific jobs sequentially in specific sequential order by invoking , jobLauncher.run(job, jobParameters).

您可以从在步骤2中初始化的上下文中获取特定的job实例.

You can get specific job instances from context initialized at step # 2.

您始终可以使用任何有序集合将您的作业放在那里并通过迭代该集合来启动作业.

You can always use any ordered collection to put your jobs there and launch jobs by iterating over that collection.

4.只要您的JobLauncher配置为同步,即主线程等待jobLauncher.run()调用完成,并且该行为是jobLauncher的默认行为,则上述技术有效.

4.This above technique works as long as your JobLauncher is configured to be synchronous i.e. main thread waits for jobLauncher.run() call to complete and that is default behavior of jobLauncher.

如果已将jobLauncher定义为使用AsyncTaskExecutor,则作业将并行启动,并且不会保持顺序排列.

If you have defined your jobLauncher to use AsyncTaskExecutor then jobs will be started in parallel and sequential ordering will not be maintained.

希望有帮助!

我正在尝试使用Stephane Nicoll指出的@Order批注,它似乎仅有助于创建作业的有序集合,并且您可以按此顺序迭代和启动作业.

I was experimenting with @Order annotation as pointed by Stephane Nicoll and it seems to help only in creating an Ordered collection of jobs and that you can iterate and launch jobs in that order.

以下组件为我提供了按指定顺序提供的工作,

This below component gives me jobs in Order specified ,

@Component
public class MyJobs {
    @Autowired
    private List<Job> jobs;

    public List<Job> getJobs() {
        return jobs;
    }
}

我可以做,只要定义了bean,就可以在主类中MyJobs myJobs = (MyJobs) ctx.getBean("myJobs");

and I can do , MyJobs myJobs = (MyJobs) ctx.getBean("myJobs"); in main class provided bean is defined,

@Bean
    public MyJobs myJobs() {
        return new MyJobs();
    }

我可以遍历myJobs并按@Order注释指定的顺序启动作业.

I can iterate over myJobs and launch jobs in that order as specified by @Order annotation.

这篇关于如何以一定顺序运行Spring Batch Jobs(Spring Boot)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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