Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the SpringBatch batch Framework

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use SpringBatch batch framework, the article is very detailed, has a certain reference value, interested friends must read it!

Using Spring Batch as a batch processing framework, it is possible to perform off-line calculations with regular data volumes that are not particularly large.

Now write a simple starter example.

By default, you have mastered the basic knowledge of Spring Batch. The example is just for quick practice.

Goal 1: The program randomly generates a string. After Spring Batch, add "---PROCESSED" after the string and output it.

Goal 2: The program reads the txt file, and after Spring Batch, uniformly adds the above fields and outputs them.

Spring Batch Process

Read Data---itemReader Process Data---itemProcess Data Write---itemWrite

Analyzing the targets shows that the input data sources of the two targets are different, the processing methods are basically the same, and the writing rules after data completion are the same.

Thus, the code can be completed in stages

itemReader

goal one

Spring Batch's centralized reader is not used here, so the reader for randomly generated strings is customized.

Here the code is not perfect, the reader will generate random strings wirelessly, but it does not affect the purpose of this study.

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

goal two

Because it is to read the contents of the file, so do not use custom reader implementation, you can directly use FlatFileItemReader, in the batch config configuration can be

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

itemProcess

The same treatment can be used here

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

itemWriter

The same can be used.

public class MyItemWriter implements ItemWriter { @Override public void write(List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report