zoukankan      html  css  js  c++  java
  • SpringBatch的ItemWriterDemo

    跟着视频写的一个小Demo

    目录结构:

    MyWriter.class
    package com.practice.itemwriter;
    
    import org.springframework.batch.item.ItemWriter;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    @Component("myWriter")
    public class MyWriter   implements ItemWriter<String>
    {
        @Override
        public void write(List<? extends String> items) throws Exception {
            System.out.println(items.size());
            for(String str:items){
                System.out.println(str);
            }
        }
    }
    ItemWriterDemo.class
    package com.practice.itemwriter;
    
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.Step;
    import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
    import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
    import org.springframework.batch.item.ItemReader;
    import org.springframework.batch.item.ItemWriter;
    import org.springframework.batch.item.support.ListItemReader;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    public class ItemWriterDemo {
        @Autowired
        private JobBuilderFactory jobBuilderFactory;
    
        @Autowired
        private StepBuilderFactory stepBuilderFactory;
        @Autowired
        @Qualifier("myWriter")
        private  ItemWriter<String> myWriter;
        @Bean
        public Job itemWriterDemoJob(){
            return jobBuilderFactory.get("itemWriterDemoJob").start(itemWriterDemoStep()).build();
        }
        @Bean
        public Step itemWriterDemoStep() {
    
            return stepBuilderFactory.get("itemWriterDemoStep")
                    .<String,String>chunk(10).reader(myRead()).writer(myWriter).build();
        }
    
        public ItemReader<String> myRead() {
            List<String> items = new ArrayList<>();
            for(int i = 0;i<=50;i++){
                items.add("java"+i);
            }
            return new ListItemReader<String>(items);
        }
    }
    DemoApplication
    package com.practice.itemwriter;
    
    import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @EnableBatchProcessing
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    正常输出:

  • 相关阅读:
    Firefly多路人脸识别解决方案
    Jquery插件开发之图片放大镜效果(仿淘宝)
    html5 Game开发系列文章之 零[开篇]
    html5 Game开发系列文章之 一 精灵(上)
    html5 Game开发系列文章之 三 搭建基本游戏框架(代码封装)
    html5 Game开发系列文章之 二 精灵(下)
    JQEURY 插件之 简洁小提示框效果[ToolTips]
    18位身份证和组织机构代码校验ORACLE函数
    linux下apache+SVN搭建完美版
    MYSQL的常用命令和增删改查语句和数据类型
  • 原文地址:https://www.cnblogs.com/Syue/p/12506821.html
Copyright © 2011-2022 走看看