博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Batch 简述:使用入门 (一)(LT项目开发参考)
阅读量:6935 次
发布时间:2019-06-27

本文共 5669 字,大约阅读时间需要 18 分钟。

  在LT eip里,springbatch是常用到的批处理框架。小批量简单数据结构可以直接用其加上rowmap做批量同步,大数据量复杂数据结构转换同步可以用batch+mq(分发到多个服务处理)+smooks。

      以下是以erp600中海关3个基础资料(成品电子账册、料件电子账册、账册备案信息)批量同步到k3cloud系统为例 

     1、创建batch的job(任务)配置文件,参考RESTEipClient\src\main\resources\META-INF\spring\batch\customs.xml(因为erp600是LT自己开发的erp系统,命名规范是按拼音首字母,虽然不是正规的命名方式,但在此也如此处理,方便后期开发维护人员对照)

View Code

以cpdzzc(成品电子账册)为例

1 
2
3
4
5
6
7
8
9
10
11
12 13 ……14 15
16
19
21
22
23
24

读取数据结果映射类

@Component("cpdzzcTempRowMapper")public class CpdzzcTempRowMapper implements RowMapper{    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {        // TODO Auto-generated method stub        CpdzzcTemp temp=new CpdzzcTemp();        temp.setSrcid(rs.getInt("id"));        temp.setFhandbookid(processString(rs.getString("scxh")));        temp.setFcancel(processString(rs.getString("zfr")));        temp.setFcanceldate(rs.getDate("zfrq"));        temp.setFhsid(processString(rs.getString("hsspbm")));        temp.setFproductname(processString(rs.getString("cppm")));        temp.setFtype(processString(rs.getString("ggxh")));        temp.setFunit(processString(rs.getString("zcdw")));        temp.setFremark(processString(rs.getString("bz")));        temp.setFcreator(processString(rs.getString("bzr")));        temp.setFcreatedate(rs.getDate("bzrq"));        temp.setFmodifydate(rs.getDate("zhxgrq"));        temp.setFproducttype(processString(rs.getString("cplx")));        temp.setFtesttype(processString(rs.getString("jylx")));        return temp;    }    public String processString( String value) {                   value=null!=value&&value.length()>0?value:" ";        return value;    }}
View Code

 cpDzzcListener需要实现org.springframework.batch.core.StepExecutionListener接口,接口有2个方法

public interface StepExecutionListener extends StepListener {        //执行前的处理    void beforeStep(StepExecution stepExecution);        //执行后的处理    ExitStatus afterStep(StepExecution stepExecution);}

写入执行类

@Component("CpdzzcTempWriter") public class CpdzzcTempWriter implements ItemWriter
{ @Autowired private OrclDaoImpl dao; public void write(List
items) throws Exception { for(CpdzzcTemp item:items){ dao.saveCpdzzc(item); } }}

 如果想程序调用,则如下,其中行7的原因是Spring Batch Job同一个job instance,成功执行后是不允许重新执行的【失败后是否允许重跑,可通过配置Job的restartable参数来控制,默认是true】,如果需要重新执行,可以变通处理,

添加一个JobParameters构建类,以当前时间作为参数,保证其他参数相同的情况下却是不同的job instance 

1 public String trunsCustoms(){ 2  3     try { 4         JobLauncher jobLauncher=(JobLauncher)SpringContextUtil.getBean("jobLauncher");  5         Job customsJob=(Job)SpringContextUtil.getBean("customsJob"); 6         Map
parameters = new HashMap
(); 7 parameters.put(UUID.randomUUID().toString(), new JobParameter(System.currentTimeMillis())); 8 jobLauncher.run(customsJob, new JobParameters(parameters)); 9 return "true";10 11 } catch (JobExecutionAlreadyRunningException e) {12 e.printStackTrace();13 } catch (JobRestartException e) {14 e.printStackTrace();15 } catch (JobInstanceAlreadyCompleteException e) {16 e.printStackTrace();17 } catch (JobParametersInvalidException e) {18 e.printStackTrace();19 }20 return "false"; 21 }

 

 

如果想用quartz定时调用,则如下

1、定义一个QuartzJob

1 @Component("quartzCustomsJob") 2 public class QuartzSaleLogisJob { 3  4     @Autowired 5     private JobLauncher launcher; 6      7     @Autowired 8     private Job customsJob; 9     10     @Autowired11     JobParametersBuilder builder;12 13     public void execute() throws Exception{14         15         builder.addDate("date", new Date());16         launcher.run(customsJob, builder.toJobParameters());        17     }18     19 }

配置文件增加quartz配置信息

execute
0 0/30 7-19 * * ?

 

转载于:https://www.cnblogs.com/treeliang/p/3346751.html

你可能感兴趣的文章
mysql 锁
查看>>
android动画-动画分类及代码演示样例
查看>>
Java中的锁(转)
查看>>
Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心
查看>>
用cocos2d-x 3.2 实现的FlappyBird
查看>>
http://jadethao.iteye.com/blog/1926525
查看>>
菜鸟要做架构师(三)——单元测试的七种境界
查看>>
Replication的犄角旮旯(七)-- 一个DDL引发的血案(下)(聊聊logreader的延迟)
查看>>
类在编写过程中的一些注意事项
查看>>
怎样解决栈溢出
查看>>
iTextSharp带中文转换出来的PDF文档显示乱码
查看>>
分享改进 完全定制自己的代码生成器
查看>>
object-c 获得目录(包括子目录)下所有文件和文件夹路径
查看>>
nginx自定义模块编写-实时统计模块--转载
查看>>
【leetcode】 search Insert Position(middle)
查看>>
我爱免费之FreeFileSync文件夹同步软件
查看>>
lufylegend:图片的加载和显示
查看>>
献给所有从事IT行业拥有梦想的英语渣们
查看>>
Linux命令-更新系统时间和硬件时间
查看>>
音频AAC编码浅析
查看>>