Spring Batch Tasklet example(二)
2015-09-28 10:58
661 查看
In Spring batch, the
only, like clean or set up resources before or after any step execution. In this example, we will show you how to use
clean up the resource (folders) after a batch job is completed.
P.S The
Batch samples project.
A Java class to implement
FileDeletingTasklet.java
A batch job to perform following steps :
Step 1 - To read multiple files from
somewhere.
Step 2 - After step 1 is completed, run
all the files from directory
spring-batch-job.xml
Taskletis an interface, which will be called to perform a single task
only, like clean or set up resources before or after any step execution. In this example, we will show you how to use
Taskletto
clean up the resource (folders) after a batch job is completed.
P.S The
FileDeletingTaskletexample below is taken from the Spring
Batch samples project.
1. Tasklet Example
A Java class to implement Taskletinterface, and delete all the files in the given directory.
FileDeletingTasklet.java
package com.mkyong.tasklet; import java.io.File; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.UnexpectedJobExecutionException; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.util.Assert; public class FileDeletingTasklet implements Tasklet, InitializingBean { private Resource directory; @Override public void afterPropertiesSet() throws Exception { Assert.notNull(directory, "directory must be set"); } @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { File dir = directory.getFile(); Assert.state(dir.isDirectory()); File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { boolean deleted = files[i].delete(); if (!deleted) { throw new UnexpectedJobExecutionException( "Could not delete file " + files[i].getPath()); } else { System.out.println(files[i].getPath() + " is deleted!"); } } return RepeatStatus.FINISHED; } public Resource getDirectory() { return directory; } public void setDirectory(Resource directory) { this.directory = directory; } }
2. Batch Jobs
A batch job to perform following steps :Step 1 - To read multiple files from
csv/inputs/, and write it to
somewhere.
Step 2 - After step 1 is completed, run
fileDeletingTaskletto delete
all the files from directory
csv/inputs/.
spring-batch-job.xml
<job id="readMultiFileJob" xmlns="http://www.springframework.org/schema/batch"> <step id="step1" next="deleteDir"> <tasklet> <chunk reader="multiResourceReader" writer="flatFileItemWriter" commit-interval="1" /> </tasklet> </step> <step id="deleteDir"> <tasklet ref="fileDeletingTasklet" /> </step> </job> <bean id="fileDeletingTasklet" class="com.mkyong.tasklet.FileDeletingTasklet" > <property name="directory" value="file:csv/inputs/" /> </bean> <bean id="multiResourceReader" class=" org.springframework.batch.item.file.MultiResourceItemReader"> <property name="resources" value="file:csv/inputs/domain-*.csv" /> <property name="delegate" ref="flatFileItemReader" /> </bean>
相关文章推荐
- 解决eclipse快捷方式无法启动
- Win7下安装,配置Java, Ant,Maven
- Java中的23种设计模式
- 类的加载机制和反射
- vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值 ...
- javaweb导入项目名修改
- struts2中的各种各样的内建校验器的小例子
- 单向链表(singleLinkedList) java实现
- java学习笔记--随记
- java设计模式之适配器模式
- spring进阶二
- 【转】java数据库操作
- 【springmvc+mybatis项目实战】杰信商贸-28.POI百万数据打印
- 用java来实现线程之间的wait、notify()通信
- eclipse环境搭建(JDK+虚拟机)
- 【springmvc+mybatis项目实战】杰信商贸-27.POI由HSSF升级为XSSF
- 定时任务用到的cron时间表达式 CronTrigger
- myeclipse部署时An internal error occurred 错误的几种情况
- 数据处理----Java数据处理之RMI
- Spring与Mybatis整合,jar版本要求