springbatch自学之路-09(监听器)

目录

1.启动类

2.Job监听器

3.chunk监听器

4.配置类


1.启动类

package com.springbatch._07listener;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBatchDemo项目启动类
 *
 * @Package: PACKAGE_NAME
 * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
 * @author: zq
 * @since: 2020/5/22 20:31
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {

    public static void main(String[] args) {

        SpringApplication.run(SpringBatchConfig.class, args);

    }

}

2.Job监听器

package com.springbatch._07listener;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

/**
 * Job监听器
 *
 * @Package: com.springbatch._07listener
 * @ClassName: MyJobListener
 * @author: zq
 * @since: 2020/6/14 18:57
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
public class MyJobListener implements JobExecutionListener {

    @Override
    public void beforeJob(JobExecution jobExecution) {

        System.out.println(jobExecution.getJobInstance().getJobName() + "before...");

    }

    @Override
    public void afterJob(JobExecution jobExecution) {

        System.out.println(jobExecution.getJobInstance().getJobName() + "after...");

    }
}

3.chunk监听器

package com.springbatch._07listener;

import org.springframework.batch.core.annotation.AfterChunk;
import org.springframework.batch.core.annotation.BeforeChunk;
import org.springframework.batch.core.scope.context.ChunkContext;

/**
 * Trunck监听器
 *
 * @Package: com.springbatch._07listener
 * @ClassName: MyChunkListener
 * @author: zq
 * @since: 2020/6/14 18:58
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
public class MyChunkListener {

    @BeforeChunk
    public void before(ChunkContext chunkContext) {

        System.out.println(chunkContext.getStepContext().getStepName() + "before===");

    }

    @AfterChunk
    public void after(ChunkContext chunkContext) {

        System.out.println(chunkContext.getStepContext().getStepName() + "after===");

    }

}

4.配置类

package com.springbatch._07listener;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class ListenerJobConfig {

    //创建 创建job的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //创建 创建step的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job listenerJob() {

        return jobBuilderFactory.get("listenerJob")
                .start(myChunkStep())
                .listener(new MyJobListener())
                .build();

    }

    @Bean
    public Step myChunkStep() {
        return stepBuilderFactory.get("myChunkStep")
                .<String, String>chunk(2)
                .faultTolerant()
                .listener(new MyChunkListener())
                .reader(read())
                .writer(write())
                .build();
    }

    @Bean
    public ItemWriter<String> write() {
        return new ItemWriter() {
            @Override
            public void write(List list) throws Exception {
                for (Object o : list) {

                    System.out.println(o);

                }
            }
        };
    }

    @Bean
    public ItemReader<String> read() {

        return new ListItemReader<>(Arrays.asList("java", "c", "c++"));

    }


}

5.执行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值