springbatch读取文件_Spring batch学习 详细配置解读(3)

本文详细介绍了Spring Batch如何实现动态设置文件读取,包括FlatFileItemReader和FlatFileItemWriter的配置,以及如何通过JobParametersBuilder设置参数。此外,还讨论了如何继承父类,添加监听器,如SystemOutJobExecutionListener,以及如何结合定时任务。内容涵盖了Spring Batch的Job、Step、监听器和任务执行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

1.动态文件读取

class="org.springframework.batch.item.file.FlatFileItemReader"scope="step">

获取动态参数文件

class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

accountID

name

amount

date

address

class="org.springframework.batch.item.file.FlatFileItemWriter"scope="step">

class="com.juxtapose.example.ch04.CreditBill">

class="com.juxtapose.example.ch04.CreditBillProcessor">

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

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 {

/**

* 执行批处理作业.

* @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.继承父类,包括监听,自定义自己监听。包括方便结合定时

class="org.springframework.batch.core.launch.support.SimpleJobLauncher">

开启定时

class="com.juxtapose.example.ch04.scheduler.SchedulerLauncher">

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

还有一个类支持一组校验 compositionJobParametersValitor

date

name

//省略头

//定义父类监听 注意abstract

//代表可以一起使用 但是如果监听异常 执行会返回失败

class="org.springframework.batch.item.file.FlatFileItemReader"scope="step">

class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

accountID

name

amount

date

address

class="org.springframework.batch.item.file.FlatFileItemWriter"scope="step">

class="com.juxtapose.example.ch04.CreditBill">

class="com.juxtapose.example.ch04.CreditBillProcessor">

class="com.juxtapose.example.ch04.listener.SystemOutJobExecutionListener">

class="com.juxtapose.example.ch04.listener.SystemOut">

使用的实体

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 classHelloWorldTasklet 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)*/

publicRepeatStatus 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));returnRepeatStatus.FINISHED;

}/**

* 转换为String类型格式.

* @param jobParameters

* @return*/

privateString jobParameterToString(JobParameters jobParameters){

StringBuffer sb= newStringBuffer();for(Map.Entryparam : jobParameters.getParameters().entrySet()) {

sb.append(String.format("%s = %s (%s);",

param.getKey(),param.getValue().getValue(),param.getValue().getType()

));

}returnsb.toString();

}

}

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

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

jobRepository  作业工厂

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

transactionManager 事务管理器

jobLauncher job执行者

先从jobrespository说起 摘自springbatch一书

ItemReader  流读取摘自书本

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

itemwriter

job说明

子元素

我偷懒了 对不起。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值