Spring batch Tasklet 例子(源码下载)

在spring batch里面, Tasklet 是一个接口. 通常被调用去执行单个任务, 比如在在一个step之前或之后执行某一些动作, 作为测试,做了一个在一个job完成之后删除掉一些资源文件. 这个例子 FileDeletingTasklet  其实也可以在 spring batch  官方的例子中找到。

1. 首先实现 Tasklet接口
程序代码 程序代码

package com.yihaomen.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 job 的定义:
有如下步骤:1, 从csv/inputs文件夹中读取多个文件,然后写到output文件夹中去。2. 在第一步完成之后,运行 fileDeletingTasklet 删除文件夹 csv/inputs
程序代码 程序代码

<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="multiResourceReader"
        class=" org.springframework.batch.item.file.MultiResourceItemReader">
        <property name="resources" value="file:csv/inputs/domain-*.csv" />
        <property name="delegate" ref="flatFileItemReader" />
    </bean>

    <bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="names" value="id, domain" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="domain" />
                    </bean>
                </property>
            </bean>
        </property>

    </bean>


运行,得到如下结果:
程序代码 程序代码

INFO: Executing step: [deleteDir]
csv\inputs\domain-1-3-2013.csv is deleted!
csv\inputs\domain-2-3-2013.csv is deleted!
csv\inputs\domain-3-3-2013.csv is deleted!
十二月 04, 2013 7:43:50 下午 org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=readMultiFileJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status : COMPLETED
Exit Status : []
Done


注意事项,必须的jar包参考 上一篇文章: spring batch入门教程
在这个例子中,使用的是内存方式,不是数据库方式,另外在执行之前先删除outpus下的文件. 然后在运行app.java 中的main 方法,就可以得到上面的结果了。整个工程如下:



源代码下载:
下载文件 Spring batch Tasklet 例子

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: springbatch
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.