SpringBoot+SpringBatch一个配置文件搞定ETL处理和任务调度

SpringBatch Configuration

@Configuration
@EnableBatchProcessing
public class JobConfiguration {
    //注入创建任务对象的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory ;

    //注入创建setp对象的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource ;

    //创建任务对象
    @Bean
    public Job deciderJob(){
        return jobBuilderFactory.get("deciderJob3")
                .listener(new MyJobListener())
                .start(step1())
                .next(firstDecider())
                .from(firstDecider()).on("step2").to(step2()).next(step4())
                .from(firstDecider()).on("step3").to(step3()).next(step4())
                .end()
                .build();
    }

    //创建step对象
    @Bean
    public Step step1() {
        TaskletStep step1 = stepBuilderFactory.get("step1").listener(new MyStepListener()).tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello, the batch job start..............");
                System.out.println(chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext());
                System.out.println(chunkContext.getStepContext().getJobExecutionContext());
                chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("info","huwenchao2");
                return RepeatStatus.FINISHED;
            }
        }).build();
        step1.setAllowStartIfComplete(true);
        return step1;
    }

    @Bean
    public Step step2() {
        TaskletStep step2 = stepBuilderFactory.get("step2").listener(new MyStepListener()).tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello, this is the step 2 ");
                System.out.println("jobParameters:info= " + chunkContext.getStepContext().getJobExecutionContext().get("info"));
                return RepeatStatus.FINISHED;
            }
        }).build();
        step2.setAllowStartIfComplete(true);
        return step2;
    }

    @Bean
    public Step step3() {
        TaskletStep step3 = stepBuilderFactory.get("step3").listener(new MyStepListener()).tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello, this is the step 3 ");
                System.out.println("jobParameters:info= " + chunkContext.getStepContext().getJobExecutionContext().get("info"));
                return RepeatStatus.FINISHED;
            }
        }).build();
        step3.setAllowStartIfComplete(true);
        return step3;
    }

    @Bean
    @StepScope
    public ItemReader<User> itemReader(){
        JdbcPagingItemReader itemReader = new JdbcPagingItemReader();

        itemReader.setDataSource(dataSource);
        itemReader.setFetchSize(1);
        itemReader.setRowMapper(new RowMapper() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt(1));
                user.setName(rs.getString(2));
                user.setAge(rs.getInt(3));
                user.setEmail(rs.getString(4));
                return user;
            }
        });

        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        provider.setSelectClause("id, name , age, email");
        provider.setFromClause("from hwc_users");
        Map<String, Order> sort = new HashMap<>();
        sort.put("name", Order.DESCENDING);
        provider.setSortKeys(sort);


        itemReader.setQueryProvider(provider);

        return itemReader;
    }

    @Bean
    public Step step4() {
        TaskletStep step4 = stepBuilderFactory.get("step4")
                .listener(new MyStepListener())
                .<User, User>chunk(2)
                .reader(itemReader())
                .writer(new ItemWriter<User>() {
                    @Override
                    public void write(List<? extends User> items) throws Exception {
                        for(User user : items){
                            System.out.println(user);
                        }
                    }
                })
                .build();
        step4.setAllowStartIfComplete(true);
        return step4;
    }

    @Bean
    public JobExecutionDecider firstDecider(){
        return new JobExecutionDecider() {
            @Override
            public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
                int random = new Random().nextInt();
                System.out.println("random number is : " + random);
                if(random % 2 == 0)
                    return new FlowExecutionStatus("step2");
                return new FlowExecutionStatus("step3");
            }
        };
    }

}

你可能感兴趣的:(spring学习笔记,spring,java,mysql,数据库)