其他分享
首页 > 其他分享> > Spring Batch 框架重温

Spring Batch 框架重温

作者:互联网

 

 

一个Batch(批处理)过程由一个Job(作业)组成。这个实体封装了整个批处理过程。

一个Job(作业)可以由一个或多个Step(步骤)组成。在大多数情况下,一个步骤将读取数据(通过ItemReader),处理数据(使用ItemProcessor),然后写入数据(通过ItemWriter)。

JobLauncher处理启动一个Job(作业)。

最后,JobRepository存储关于配置和执行的Job(作业)的元数据。

 

摘自:https://www.cnblogs.com/liululee/p/11124383.html

核心代码:

@Configuration
public class SampleJobConfigure {

    @Bean
    public Job helloWorlJob(JobBuilderFactory jobBuilders,
                            StepBuilderFactory stepBuilders) {
        return jobBuilders.get("helloWorldJob")
                          .start(helloWorldStep(stepBuilders)).build();
    }

    @Bean
    public Step helloWorldStep(StepBuilderFactory stepBuilders) {
        return stepBuilders.get("helloWorldStep")
                .<Person, String>chunk(10).reader(reader())
                                          .processor(processor()).writer(writer()).build();
    }

    @Bean
    public FlatFileItemReader<Person> reader() {
        return new FlatFileItemReaderBuilder<Person>()
                .name("personItemReader")
                .resource(new ClassPathResource("csv/persons.csv"))
                .delimited().names(new String[] {"firstName", "lastName"})
                .targetType(Person.class).build();
    }

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

    @Bean
    public FlatFileItemWriter<String> writer() {
        return new FlatFileItemWriterBuilder<String>()
                .name("greetingItemWriter")
                .resource(new FileSystemResource(
                        "target/test-outputs/greetings.txt"))
                .lineAggregator(new PassThroughLineAggregator<>()).build();
    }
}

  启动job

 Job job = jobRegistry.getJob(jobName);
jobLauncher.run(job, paramBuilder.toJobParameters());
//JobExecution jobExecution = jobLauncherTestUtils.launchJob();

  

标签:stepBuilders,return,Spring,重温,Batch,Bean,Job,new,public
来源: https://www.cnblogs.com/lgtrajectory/p/14413460.html