Spring Batch并行运行多个作业 [英] Spring batch run multiple jobs in parallel

查看:951
本文介绍了Spring Batch并行运行多个作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring批处理的新手,无法弄清楚该怎么做.

I am new to Spring batch and couldn't figure out how to do this..

基本上我有一个春季批处理文件,并且两个文件都必须并行运行,即当我请求execute_job1然后必须执行BatchConfig1,而当我请求execute_job2然后必须执行BatchConfig2.我该怎么办?

Basically I have a spring batch files and both are have to run parallel i.e when I request execute_job1 then BatchConfig1 have to execute and when I request execute_job2 then BatchConfig2 have to execute. How can I do this?

@RestController
public class JobExecutionController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    /**
     * 
     * @return
     */
    @RequestMapping("/execute_job1")
    @ResponseBody
    public void executeBatchJob1() {

    }

    /**
     * 
     * @return
     */
    @RequestMapping("/execute_job2")
    @ResponseBody
    public void executeBatchJob2() {

    }

}

BatchConfig1

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Step stepOne(){
        return steps.get("stepOne")
                .tasklet(new MyTaskOne())
                .build();
    }

    @Bean
    public Step stepTwo(){
        return steps.get("stepTwo")
                .tasklet(new MyTaskTwo())
                .build();
    }  

    @Bean
    public Job demoJob(){
        return jobs.get("exportUserJob1")
                .incrementer(new RunIdIncrementer())
                .start(stepOne())
                .next(stepTwo())
                .build();
    }
}

BatchConfig2:

@Configuration
@EnableBatchProcessing
public class BatchConfig2 {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;


    @Bean
    public JdbcCursorItemReader<User> reader() {
        JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
        reader.setDataSource(dataSource);
        reader.setSql("SELECT id,name FROM user");
        reader.setRowMapper(new UserRowMapper());
        return reader;
    }

    public class UserRowMapper implements RowMapper<User> {

        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setName(rs.getString("name"));

            return user;
        }

    }

    @Bean
    public UserItemProcessor processor() {
        return new UserItemProcessor();
    }

    @Bean
    public FlatFileItemWriter<User> writer() {
        FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
        writer.setResource(new ClassPathResource("users.csv"));
        writer.setLineAggregator(new DelimitedLineAggregator<User>() {
            {
                setDelimiter(",");
                setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
                    {
                        setNames(new String[] { "id", "name" });
                    }
                });
            }
        });

        return writer;
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<User, User>chunk(10).reader(reader()).processor(processor())
                .writer(writer()).build();
    }

    @Bean
    public Job exportUserJob() {
        return jobBuilderFactory.get("exportUserJob2").incrementer(new RunIdIncrementer()).flow(step1()).end().build();
    }
}

推荐答案

您可以使用JobLauncher运行作业.控制器的代码:

You can run a job with JobLauncher. The code for the controller:

@RestController
public class JobExecutionController {

public JobExecutionController(JobLauncher jobLauncher, 
                              @Qualifier("demoJob") Job demoJob, 
                              @Qualifier("exportUserJob") Job exportUserJob) {
    this.jobLauncher = jobLauncher;
    this.demoJob = demoJob;
    this.exportUserJob = exportUserJob;
}

JobLauncher jobLauncher;

Job demoJob;

Job exportUserJob;


@RequestMapping("/execute_job1")
@ResponseBody
public void executeBatchJob1() {
    try {
        JobExecution jobExecution = jobLauncher.run(demoJob, new JobParameters(generateJobParameter()));
        log.info("Job started in thread :" + jobExecution.getJobParameters().getString("JobThread"));
    } catch (JobExecutionAlreadyRunningException | JobRestartException | JobParametersInvalidException | JobInstanceAlreadyCompleteException e) {
        log.error("Something sent wrong during job execution", e);
    }

}

@RequestMapping("/execute_job2")
@ResponseBody
public void executeBatchJob2() {
    try {
        JobExecution jobExecution = jobLauncher.run(exportUserJob, new JobParameters(generateJobParameter()));
        log.info("Job started in thread :" + jobExecution.getJobParameters().getString("JobThread"));
    } catch (JobExecutionAlreadyRunningException | JobRestartException | JobParametersInvalidException | JobInstanceAlreadyCompleteException e) {
        log.error("Something sent wrong during job execution", e);
    }

}

private JobParameters generateJobParameter() {
    Map<String, JobParameter> parameters = new HashMap<>();
    parameters.put("Job start time", new JobParameter(Instant.now().toEpochMilli()));
    parameters.put("JobThread", new JobParameter(Thread.currentThread().getId()));

    return new JobParameters(parameters);
}
}

要防止在应用程序处启动作业,请在下一个春季批处理配置中将其添加到application.properties中:spring.batch.job.enabled = false

To prevent starting your jobs at the application start add to the application.properties next spring batch configuration: spring.batch.job.enabled=false

这篇关于Spring Batch并行运行多个作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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