怎么在SpringBatch中對(duì)框架進(jìn)行處理

怎么在Spring Batch中對(duì)框架進(jìn)行處理?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供巴楚網(wǎng)站建設(shè)、巴楚做網(wǎng)站、巴楚網(wǎng)站設(shè)計(jì)、巴楚網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、巴楚企業(yè)網(wǎng)站模板建站服務(wù),10余年巴楚做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

目標(biāo)1:程序隨機(jī)生成字符串,經(jīng)過Spring Batch后,統(tǒng)一在字符串后加入“----PROCESSED”,并輸出

目標(biāo)2:程序讀取txt文件,經(jīng)過Spring Batch后,統(tǒng)一加入如上字段,并輸出

Spring Batch的流程

  • 讀取數(shù)據(jù)----itemReader

  • 處理數(shù)據(jù)----itemProcess

  • 數(shù)據(jù)寫入----itemWrite

分析目標(biāo)可知,兩個(gè)目標(biāo)的輸入數(shù)據(jù)源不同,處理方式基本一致,數(shù)據(jù)完成后的寫入規(guī)則一致

由此可以分段完成代碼

itemReader

目標(biāo)一

這里沒有使用Spring Batch自帶的集中reader,所以自定義了隨機(jī)生成字符串的reader

這里代碼并不完善,reader會(huì)無線循環(huán)生成隨機(jī)字符串,但不影響本次學(xué)習(xí)的目的

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

目標(biāo)二

由于是讀取文件中的內(nèi)容,所以不用自定義reader實(shí)現(xiàn),可直接使用FlatFileItemReader,在Batch的config中配置即可

  @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

這里采用同一種處理方式即可

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

itemWriter

也采用同一種即可

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
@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")
        //這個(gè)chunk size是最后調(diào)用寫入的時(shí)候,一次性寫入多少條已處理的數(shù)據(jù)
        .<String,String>chunk(10)
//        .reader(textReader())
        .reader(stringReader())
        .processor(processor())
        .writer(writer())
        .build();
 
  }
 
  @Bean
  public Job MyJob(){
    return jobBuilderFactory
        .get("MyJOB")
        .listener(new JobExecutionListenerSupport(){
          //所有處理結(jié)束后調(diào)用
          @Override
          public void afterJob(JobExecution jobExecution) {
            if(jobExecution.getStatus() == BatchStatus.COMPLETED){
              System.out.println("OK");
            }
          }
        })
        .flow(myStep())
        .end()
        .build();
  }
}

關(guān)于怎么在Spring Batch中對(duì)框架進(jìn)行處理問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

文章標(biāo)題:怎么在SpringBatch中對(duì)框架進(jìn)行處理
當(dāng)前網(wǎng)址:http://muchs.cn/article34/gdijse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、靜態(tài)網(wǎng)站搜索引擎優(yōu)化、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站