Spring Batch simple example

Use Spring Batch framework as batch, the amount of data to complete a conventional off-line calculation is not particularly large.

Now write a simple example of Starter Edition.

The default here we have mastered the basics of Spring Batch, examples of practice just to get started quickly

Goal 1: The program randomly generated string, after the Spring Batch, unified after adding the string "---- PROCESSED", and output

Goal 2: txt file reading program, after Spring Batch, was added as a unified field, and outputs

Spring Batch Process

  1. Read data ---- itemReader
  2. Data processing ---- itemProcess
  3. Data is written ---- itemWrite

Analysis found the target, the target two different input data sources, substantially consistent treatment, consistent with the data after completion of the write rule

Whereby completion code segment

itemReader

A target

Spring Batch not used here comes centralized reader, the reader so custom randomly generated string

Here the code is not perfect, reader wireless loop will generate a random string, but does not affect the purpose of this study

 
public class MyItemReader implements ItemReader<String> {
    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        return RandomStringUtils.randomAlphabetic(10);
    }
}

Objective Two

Because it is to read the contents of the file, so do not realize custom reader can be used directly FlatFileItemReader, configured in config Batch in to

    @Bean
    public ItemReader<String> textReader(){
 
        FlatFileItemReader<String> reader=new FlatFileItemReader<>();
        File file = new File("D:\\FTP\\ttest.txt");
        reader.setResource(new FileSystemResource(file));
        reader.setLineMapper(new LineMapper<String>() {
            @Override
            public String mapLine(String line, int lineNumber) throws Exception {
                return line;
            }
        });
        return reader;
 
    }

itemProcess

Here you can use the same approach

public class MyItemProcessor implements ItemProcessor<String,String> {
 
    @Override
    public String process(String s) throws Exception {
        return s+"---------PROCESSED";
    }
}

itemWriter

It can also use the same

public class MyItemWriter implements ItemWriter<String> {
    @Override
    public void write(List<? extends String> items) throws Exception {
        for (String item : items) {
            System.out.println(item);
        }
    }
}

Batch Config configuration

@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends DefaultBatchConfigurer {
 
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
 
    @Bean
    public MyItemProcessor processor(){
        return new MyItemProcessor();
    }
 
    @Bean
    public ItemWriter<String> writer(){
        return new MyItemWriter();
    }
 
    @Bean
    public ItemReader<String> textReader(){
        FlatFileItemReader<String> reader=new FlatFileItemReader<>();
        File file = new File("D:\\FTP\\ttest.txt");
        reader.setResource(new FileSystemResource(file));
        reader.setLineMapper(new LineMapper<String>() {
            @Override
            public String mapLine(String line, int lineNumber) throws Exception {
                return line;
            }
        });
        return reader;
    }
 
    @Bean
    public ItemReader<String> stringReader(){
        return new MyItemReader();
    }
 
    @Override
    public void setDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);
    }
 
    @Bean
    public Step myStep(){
        return stepBuilderFactory
                .get("step1")
                //这个chunk size是最后调用写入的时候,一次性写入多少条已处理的数据
                .<String,String>chunk(10)
//                .reader(textReader())
                .reader(stringReader())
                .processor(processor())
                .writer(writer())
                .build();
 
    }
 
    @Bean
    public Job MyJob(){
        return jobBuilderFactory
                .get("MyJOB")
                .listener(new JobExecutionListenerSupport(){
                    //所有处理结束后调用
                    @Override
                    public void afterJob(JobExecution jobExecution) {
                        if(jobExecution.getStatus() == BatchStatus.COMPLETED){
                            System.out.println("OK");
                        }
                    }
                })
                .flow(myStep())
                .end()
                .build();
    }
}

End

Last spring boot program can be run directly

Guess you like

Origin www.cnblogs.com/tilv37/p/12019652.html