This paper describes the use of Spring Batch framework of analysis. Through detailed examples of code for each person to study or work has a certain reference and learning value, need friends can reference.
Use as a batch batch spring frame, can be done off-line in conventional amounts calculated data is not particularly large.
Now write a simple entry-level example. http://m.jlnk3659999.com
The default setting here is that everyone has mastered the basics of Spring Batch, this example only to quickly realize hands.
Goal 1: The program randomly generated string in the spring after batch, unified add "-processed" after the string, and outputs them
Goal 2: Program to read txt file, in the spring after batch, adding the unified field and outputs them.
Spring batch process
Read data - processing the data item is read - writing the data item and - writing items
The analysis of the target, the two target different input data sources, the process is basically the same, the same data is written after the completion of the rule.
Whereby completion code segment http://mip.0834jl.com
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>() {
@Overridehttp://www.jl0834.com
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";
}
}