如何工作(工作流程)项目编写者,项目阅读器,春季批次的项目处理器

时间:2016-12-06 04:31:09

标签: java spring spring-mvc spring-boot spring-batch

我已经用自定义项目阅读器,项目处理器和项目编写器使用java注释编写了一个简单的弹簧批处理应用程序。但是我无法理解这个工作流程。如果有人能够描述读者,作者和处理器,那将非常有用。在春季工作。

主要课程

@SpringBootApplication
@ComponentScan({"com.test"})
public class TestBatch implements CommandLineRunner {

    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(TestConfig.class);
        ConfigurableApplicationContext ctx = app.run(args);

        JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
        Job jobOne = ctx.getBean("jobOne", Job.class);

        JobParameters jobOneParams = new JobParametersBuilder().addDate("date", new Date()).toJobParameters();
        JobExecution jobExcecution = jobLauncher.run(jobOne,jobOneParams);
        System.out.println("Status -->"+jobExcecution.getStatus());
    }

    @Override
    public void run(String... arg) throws Exception {
        System.out.println("running -->");
    }

}

配置类

public class TestConfig {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job jobOne(){
        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .listener(listener())
                .flow(stepOne())
                .end()
                .build();
    }

    @Bean
    public Step stepOne(){
        return stepBuilderFactory.get("stepone")
                .<String, String> chunk(1)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }

    @Bean
    public ItemReader reader() {
        return new MyItemReader();
    }

    @Bean
    public ItemProcessor processor() {
        return new MyItemProcessor();
    }

    @Bean
    public ItemWriter writer() {
        return new MyItemItemWriter();
    }

    @Bean
    public JobExecutionListener listener() {
        return new MyListener();
    }
}
读者类

@Scope("step")
public class MyItemReader implements ItemReader<String>{
    Logger logger = LoggerFactory.getLogger(this.getClass());
    private String date;
    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        System.out.println("---Reading---");
        return "test";
    }
}

处理器类

public class MyItemProcessor implements ItemProcessor<String, String>{

    @Override
    public String process(String content) throws Exception {
        System.out.println("---processing---");
        return content.toUpperCase();
    }

}

作家卡尔斯

public class MyItemItemWriter implements ItemWriter<String>{
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public void write(List<? extends String> messages) throws Exception {
        for(String msg : messages){
            System.out.println("writing-->" + msg);
        }
    }
}

提前致谢。

0 个答案:

没有答案