第一篇讲到普通job 配置 那么spring  batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等。

下面看一个动态设置读取文件的配置

1.动态文件读取

<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<bean:import resource="classpath:ch04/job-context.xml"/> <!-- 账单作业 -->
<job id="billJob">
<step id="billStep">
<tasklet transaction-manager="transactionManager">
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval="">
</chunk>
</tasklet>
</step>
</job> <!-- 读取信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<bean:property name="resource"
value="#{jobParameters['inputResource']}"/> 获取动态参数文件
<bean:property name="lineMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<bean:property name="lineTokenizer" ref="lineTokenizer"/>
<bean:property name="fieldSetMapper">
<bean:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="creditBill">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <!-- lineTokenizer -->
<bean:bean id="lineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="delimiter" value=","/>
<bean:property name="names">
<bean:list>
<bean:value>accountID</bean:value>
<bean:value>name</bean:value>
<bean:value>amount</bean:value>
<bean:value>date</bean:value>
<bean:value>address</bean:value>
</bean:list>
</bean:property>
</bean:bean> <!-- 写信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter"
scope="step">
<bean:property name="resource" value="file:target/ch04/outputFile.csv"/>
<bean:property name="lineAggregator">
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<bean:property name="delimiter" value=","></bean:property>
<bean:property name="fieldExtractor">
<bean:bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<bean:property name="names"
value="accountID,name,amount,date,address">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <bean:bean id="creditBill" scope="prototype"
class="com.juxtapose.example.ch04.CreditBill">
</bean:bean>
<bean:bean id="creditBillProcessor" scope="step" 给定生命周期 只在当前步骤
class="com.juxtapose.example.ch04.CreditBillProcessor">
</bean:bean>
</bean:beans>

下面看测试类。 省略基础配置和实体。

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class JobLaunchStepScope { /**
* 执行批处理作业.<br>
* @param jobPath 作业配置文件
* @param jobName 作业名
* @param builder 作业参数构造器
*/
public static void executeJob(String jobPath, String jobName, JobParametersBuilder builder) {
ApplicationContext context = new ClassPathXmlApplicationContext(jobPath);
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean(jobName);
try {
JobExecution result = launcher.run(job, builder.toJobParameters());
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
} /**
* @param args
*/
// adddString 添加一个参数 在配置文件读取
public static void main(String[] args) {
executeJob("ch04/job/job-stepscope.xml", "billJob",
new JobParametersBuilder().addDate("date", new Date())
.addString("inputResource", "classpath:ch04/data/credit-card-bill-201303.csv"));
}
}

  2.继承父类,包括监听,自定义自己监听。包括方便结合定时

<!-- 异步作业调度器 -->

<task:executor id="executor" pool-size="1" />
<bean:bean id="jobLauncherAsyn"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<bean:property name="jobRepository" ref="jobRepository"/>
<bean:property name="taskExecutor" ref="executor" />
</bean:bean>

<task:scheduler id="scheduler" pool-size="10" /> 开启定时

<!-- 每一秒钟,执行对象schedulerLauncher的launch方法一次 -->
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="schedulerLauncher" method="launch" fixed-rate="1000" />
</task:scheduled-tasks>

<bean:bean id="schedulerLauncher"
class="com.juxtapose.example.ch04.scheduler.SchedulerLauncher">
<bean:property name="job" ref="helloworldJob" />
<bean:property name="jobLauncher" ref="jobLauncher" />
</bean:bean>

<!-- 参数校验 --> 采用默认校验 传入的时候必须携带date参数。最多传入两个

还有一个类支持一组校验 compositionJobParametersValitor
<bean:bean id="validator" class="org.springframework.batch.core.job.DefaultJobParametersValidator" >
<bean:property name="requiredKeys" >
<bean:set>
<bean:value>date</bean:value>
</bean:set>
</bean:property>
<bean:property name="optionalKeys" >
<bean:set>
<bean:value>name</bean:value>
</bean:set>
</bean:property>
</bean:bean>

//省略头    
<!-- 抽象基础Job 与java中类似-->
<job id="baseJob" abstract="true"> //定义父类监听 注意abstract
<listeners>
<listener ref="sysoutListener"></listener>
</listeners>
</job>
<!-- 账单作业 -->
<job id="billJob" parent="baseJob">
<step id="billStep">
<tasklet transaction-manager="transactionManager">
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval="">
</chunk>
</tasklet>
</step>
<listeners merge="true"> //代表可以一起使用 但是如果监听异常 执行会返回失败
<listener ref="sysoutAnnotationListener"></listener>
</listeners>
</job> <!-- 读取信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<bean:property name="resource"
value="classpath:ch04/data/credit-card-bill-201303.csv"/>
<bean:property name="lineMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<bean:property name="lineTokenizer" ref="lineTokenizer"/>
<bean:property name="fieldSetMapper">
<bean:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="creditBill">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <!-- lineTokenizer -->
<bean:bean id="lineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="delimiter" value=","/>
<bean:property name="names">
<bean:list>
<bean:value>accountID</bean:value>
<bean:value>name</bean:value>
<bean:value>amount</bean:value>
<bean:value>date</bean:value>
<bean:value>address</bean:value>
</bean:list>
</bean:property>
</bean:bean> <!-- 写信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter"
scope="step">
<bean:property name="resource" value="file:target/ch04/outputFile.csv"/>
<bean:property name="lineAggregator">
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<bean:property name="delimiter" value=","></bean:property>
<bean:property name="fieldExtractor">
<bean:bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<bean:property name="names"
value="accountID,name,amount,date,address">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <bean:bean id="creditBill" scope="prototype"
class="com.juxtapose.example.ch04.CreditBill">
</bean:bean>
<bean:bean id="creditBillProcessor" scope="step"
class="com.juxtapose.example.ch04.CreditBillProcessor">
</bean:bean>
<bean:bean id="sysoutListener"
class="com.juxtapose.example.ch04.listener.SystemOutJobExecutionListener">
</bean:bean>
<bean:bean id="sysoutAnnotationListener"
class="com.juxtapose.example.ch04.listener.SystemOut">
</bean:bean>

使用的实体

import java.util.Map;

import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus; public class HelloWorldTasklet implements Tasklet { /* (non-Javadoc)
* @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)
*/
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
String jobName = chunkContext.getStepContext().getJobName();
System.out.println("Execute job :" + jobName +".");
JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
System.out.println("JobParameters:" + jobParameterToString(jobParameters));
return RepeatStatus.FINISHED;
} /**
* 转换为String类型格式.<br>
* @param jobParameters
* @return
*/
private String jobParameterToString(JobParameters jobParameters){
StringBuffer sb = new StringBuffer();
for(Map.Entry<String, JobParameter> param : jobParameters.getParameters().entrySet()) {
sb.append(String.format(
"%s = %s (%s);",
param.getKey(),param.getValue().getValue(),param.getValue().getType()
));
}
return sb.toString();
} }

接下来看下基础配置中的参数值

回顾下基础参数 包含三个类

jobRepository  作业工厂

datasource 数据源 当选择持久化是需要

transactionManager 事务管理器

jobLauncher job执行者

先从jobrespository说起 摘自springbatch一书

ItemReader  流读取摘自书本

ItemProcessor 对读取数据处理 比如清晰转换校验等

itemwriter

job说明

子元素

我偷懒了 对不起。。。

Spring batch学习 详细配置解读(3)的更多相关文章

  1. Spring Batch学习笔记三:JobRepository

    此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch Job在运行时有很多元数据,这些元数据一般会被保存在内存或者数据库中,由于Spring Batch在默认配置是使用H ...

  2. Spring Batch学习笔记二

    此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch的架构 一个Batch Job是指一系列有序的Step的集合,它们作为预定义流程的一部分而被执行: Step代表一个自 ...

  3. spring batch学习笔记

    Spring Batch是什么?       Spring Batch是一个基于Spring的企业级批处理框架,按照我师父的说法,所有基于Spring的框架都是使用了spring的IoC特性,然后加上 ...

  4. Spring Security(三) —— 核心配置解读

    摘要: 原创出处 https://www.cnkirito.moe/spring-security-3/ 「老徐」欢迎转载,保留摘要,谢谢! 3 核心配置解读 上一篇文章<Spring Secu ...

  5. Spring Batch学习

    今天准备研究下Spring Batch,然后看了一系列资料,如下还是比较好的教程吧. 链接: http://www.cnblogs.com/gulvzhe/archive/2011/12/20/229 ...

  6. Spring Batch学习笔记(一)

    Spring Batch简介 Spring Batch提供了可重复使用的功能,用来处理大量数据.包括记录.跟踪,事务管理,作业处理统计,作业重启,跳过和资源管理. 此外还提供了更高级的技术服务和功能, ...

  7. Spring batch学习 (1)

    Spring Batch 批处理框架 埃森哲和Spring Source研发 主要解决批处理数据的问题,包含并行处理,事务处理机制等.具有健壮性 可扩展,和自带的监控功能,并且支持断点和重发.让程序员 ...

  8. spring cloud学习(六) 配置中心-自动更新

    上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...

  9. Spring batch学习 持久化表结构详解(2)

    #接上一篇 这一篇讲一下持久化需要表 batch_job_execution, batch_job_execution_context, batch_job_execution_params, bat ...

随机推荐

  1. undefined 与 null

    typeof null  -   'object typeof undefined   -  'undefined' Boolean(null)    -  false Boolean(undefin ...

  2. TensorFlow实现FM

    看了网上的一些用tf实现的FM,很多都没有考虑FM实际使用中数据样本稀疏的问题. 我在实现的时候使用 embedding_lookup_sparse来解决这个问题. 对于二阶部分,由于embeddin ...

  3. Android------Button 添加声音效果(两种方式)

    我在先前的案例<Android 的底部导航栏 BottomNavigationBar>中添加以底部 的4个按钮切换添加声音 下来看看案例效果图 使用添加依赖 compile 'com.as ...

  4. UI测试_错题解析

    解析:因为jQuery easyUI是基于jQuery框架在使用之前应该先引入jquery框架否则jQuery easyUI将失效,故D错误 解析:考Link标签和script标签的区别,Link引入 ...

  5. Spring Boot 集成 FreeMarker 详解案例(十五)

    一.Springboot 那些事 SpringBoot 很方便的集成 FreeMarker ,DAO 数据库操作层依旧用的是 Mybatis,本文将会一步一步到来如何集成 FreeMarker 以及配 ...

  6. windows下mysql多实例安装

    在学习和开发过程中有时候会用到多个MySQL数据库,比如Master-Slave集群.分库分表,开发阶段在一台机器上安装多个MySQL实例就显得方便不少. 在 MySQL教程-基础篇-1.1-Wind ...

  7. Vue.js组件设计原则

    页面上把每个独立可以交互的区域视为一个组件 每个组件对应一个工程目录,组件所需要的各种资源在这个目录下就近维护 页面不过是组件的容器,组件可以嵌套自由组合形成完整的页面

  8. Jenkins用户权限管理

    一.插件安装 插件:Role-based Authorization Strategy版本:2.3.2 二.全局安全配置 进入Jenkins后点击系统管理进入全局安全配置 当插件安装好的时候,授权策略 ...

  9. istringstream、ostringstream、stringstream类介绍

    本文转载自: http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html 一.C++的输入输出分为三种: (1)基于控制台的I/O ( ...

  10. Sql 基础问题

    Ref Projection and Selection 联结查询的原理(笛卡尔积) 设计 MySQL 数据表的时候一般都有一列为自增 ID,这样设计原因是什么,有什么好处?