Spring Batch并行运行多个作业

时间:2019-06-27 12:30:14

标签: spring spring-boot spring-batch

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

基本上我有一个春季批处理文件,并且两个文件都必须并行运行,即当我请求execute_job1然后BatchConfig1必须执行,而当我请求execute_job2然后BatchConfig2必须执行我该怎么做时可以有人帮我吗

控制器

@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();
    }
}

2 个答案:

答案 0 :(得分:0)

好像您可以使用多线程步骤并行步骤

签出文档:https://docs.spring.io/spring-batch/4.0.x/reference/html/scalability.html#scalability

答案 1 :(得分:0)

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

@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