javascript
springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二) -凯发ag旗舰厅登录网址下载
文章目录
- 一、抽取顺序读取多文件公共reader
- 二、抽取写文件公共基于multiresourceitemwriter
- 三、实现自己的resourcesuffixcreator
- 四、读取多文件job
- 五、运行结果
前言:上一章我们讲解了springbatch读取单个文件和写文件的具体用法,这一张我们继续讲解读取多个文件的用法。
我们在一些特殊的业务场景中,需要顺序读取多个文件,幸运的是springbatch已经给我们提供了公共reader multiresourceitemreader
代码已上传github上面地址:https://github.com/fadehub/spring-boot-learn/tree/master/spring-boot-springbatch
springbatch其它文章直通车:
springbatch读单个文件(flatfileitemreader)和写单个文件(flatfileitemwriter)(一)
springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二)
springbatch读数据库(mybatispagingitemreader)(三)
springbatch读文件(flatfileitemreader)写据库(mybatisbatchitemwriter)(四)
springbatch 监听器之job监听器(jobexecutionlistener)和step监听器(stepexecutionlistener)(五)
springbatch 监听器之chunk监听器(chunklistener)和skip监听器(skiplistener)(六)
一、抽取顺序读取多文件公共reader
commonmultiresourceitemreader继承multiresourceitemreader
/*** 顺序读取多个文件* @author shuliangzhao* @title: commonmultiresourceitemreader* @projectname spring-boot-learn* @description: todo* @date 2019/9/8 12:34*/ public class commonmultiresourceitemreader敲黑板:setdelegate(new commonfileitemreader<>(clz));这个commonfileitemreader是上一章公共读取单个文件的reader
二、抽取写文件公共基于multiresourceitemwriter
commonmultiresourceitemwriter继承multiresourceitemwriter
*** @author shuliangzhao* @title: commonmultiresourceitemwriter* @projectname spring-boot-learn* @description: todo* @date 2019/9/8 13:05*/ public class commonmultiresourceitemwriter三、实现自己的resourcesuffixcreator
csvresourcesuffixcreator 实现resourcesuffixcreator 接口
package com.sl.resource;import org.springframework.batch.item.file.resourcesuffixcreator;/*** @author shuliangzhao* @title: csvresourcesuffixcreator* @projectname spring-boot-learn* @description: todo* @date 2019/9/8 13:20*/ public class csvresourcesuffixcreator implements resourcesuffixcreator {@overridepublic string getsuffix(int i) {return i ".csv" ;} }四、读取多文件job
读取多文件job使用上一章job改造之后的
package com.sl.config;import com.sl.common.commonfileitemwriter; import com.sl.common.commonmultiresourceitemreader; import com.sl.common.commonmultiresourceitemwriter; import com.sl.entity.people; import com.sl.entity.student; import com.sl.processor.studentprocessor; import org.springframework.batch.core.job; import org.springframework.batch.core.step; import org.springframework.batch.core.configuration.annotation.enablebatchprocessing; import org.springframework.batch.core.configuration.annotation.jobbuilderfactory; import org.springframework.batch.core.configuration.annotation.stepbuilderfactory; import org.springframework.batch.core.configuration.annotation.stepscope; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration;/*** @author shuliangzhao* @title: studentmulticonfiguration* @projectname spring-boot-learn* @description: todo* @date 2019/9/8 12:41*/ @configuration @enablebatchprocessing public class studentmulticonfiguration {@autowiredprivate jobbuilderfactory jobbuilderfactory;@autowiredprivate stepbuilderfactory stepbuilderfactory;@autowiredprivate studentprocessor studentprocessor;@beanpublic job studentmultijob() {return jobbuilderfactory.get("studentmultijob").start(studentmulitstep()).build();}@beanpublic step studentmulitstep() {return stepbuilderfactory.get("studentmulitstep").五、运行结果
这是读取多文件的文件个数,每个文件有10条数据
写文件文件条数总共是30条。
以上就是multiresourceitemreader和multiresourceitemwriter的基本用法
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: springbatch读文件(flatf