Spring Batch Tasklet example(二)

In Spring batch, the Tasklet is 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 Tasklet to clean up the resource (folders) after a batch job is completed.

P.S The FileDeletingTasklet example below is taken from the Spring Batch samples project.

1. Tasklet Example

A Java class to implement Tasklet interface, 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 fileDeletingTasklet to 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>
	

转载于:https://www.cnblogs.com/yzhming/p/4890538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值