码迷,mamicode.com
首页 > 编程语言 > 详细

springbatch---->springbatch的使用(四)

时间:2017-11-02 11:14:22      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:conf   tac   java   exec   监听   ott   trace   ica   set   

  这里我们重点学习一下springbatch里面的各种监听器的使用,以及job参数的传递。追求得到之日即其终止之时,寻觅的过程亦即失去的过程。

 

springbatch的监听器

一、JOB LISTENERS:监听job层面上的操作

public interface JobExecutionListener {
    void beforeJob(JobExecution jobExecution);
    void afterJob(JobExecution jobExecution);
}
  • 类方法的方式
<job id="helloWorldJob">
    <listeners>
        <listener ref="jobExecListener"/>
    </listeners>
    <step ....>
    </step>
</job>

bean的定义

<bean id="jobExecListener" class="spring.batch.helloworld.JobExecListener"/>

 bean的实现

package spring.batch.helloworld;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

/**
 * @Author: huhx
 * @Date: 2017-11-02 上午 8:50
 */
public class JobExecListener implements JobExecutionListener {

    @Override
    public void beforeJob(JobExecution jobExecution) {
        System.out.println("before start time: " + jobExecution.getStartTime());
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        System.out.println("after end time: " + jobExecution.getEndTime());
    }
}
  • 使用注解方式,区别在于bean类的编写。
public class JobExecListener {

    @BeforeJob
    public void beforeJob(JobExecution jobExecution) {
        System.out.println("before start time: " + jobExecution.getStartTime());
    }

    @AfterJob
    public void afterJob(JobExecution jobExecution) {
        System.out.println("after end time: " + jobExecution.getEndTime());
    }
}

这个例子是基于helloworld的例子来的,可以参考博客:springbatch---->springbatch的使用(一).。以下是打印的结果

before start time: Thu Nov 02 09:00:13 CST 2017
Hello 
 World!
after end time: Thu Nov 02 09:00:14 CST 2017

 

二、STEP LISTENERS:监听Step的处理过程

springbatch为我们提供了如下的关于Step的监听器。

1、ChunkListener 
    Called before and after chunk execution
2、ItemProcessListener 
    Called before and after an ItemProcessor gets an item and when that processor throws an exception
3、ItemReadListener 
    Called before and after an item is read and when an
exception occurs reading an item
4、ItemWriteListener 
    Called before and after an item is written and when an exception occurs writing an item
5、SkipListener 
    Called when a skip occurs while reading, processing, or writing an item
6、StepExecutionListener 
    Called before and after a step

Step监听器的接口,用法同上述的Job监听器基本一样。也是有方法的实现方式和注解的实现方式。

 

三、parent与abstract属性的使用

1、abstract 
    When  true, specifies that the job or step element isn’t a concrete element but an abstract one used only for configuration. Abstract configuration enti-
ties aren’t instantiated.
2、parent: 子元素继承了父元素所有的属性,当然子元素可以复写父元素的属性
    The parent element used to configure a given element. The child element has all properties of its parent and can override them.

 在batch.xml文件中增加一个step和一个job。

<!--parent and abstract-->
<step id="parentStep" abstract="true">
    <tasklet transaction-manager="transactionManager">
        <listeners>
            <listener ref="parentStepListener"/>
        </listeners>
    </tasklet>
</step>

<job id="childJob">
    <step id="childStep" parent="parentStep">
        <tasklet ref="childTasklet" transaction-manager="transactionManager"/>
    </step>
</job>

<bean:bean id="childTasklet" class="spring.batch.parentAbstract.ParentTasklet" scope="step">
    <bean:property name="username" value="#{jobParameters[‘password‘]}"/>
</bean:bean>

  parentTasklet的实现类代码:

package spring.batch.parentAbstract;

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;

/**
 * @Author: huhx
 * @Date: 2017-11-02 上午 9:18
 */
public class ParentTasklet implements Tasklet {

    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        System.out.println("parent tasklet" + username);
        return RepeatStatus.FINISHED;
    }
}

打印的结果如下,可以看到childJob已经有了parentStep里面的监听器了。

before step.
parent tasklet123456
after step.

 另外JobLaunch.java的代码如下

public class JobLaunch {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config/batch/batch.xml");
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");

        Job job = (Job) context.getBean("childJob");
        try {
            // 运行Job
            JobParametersBuilder parametersBuilder = new JobParametersBuilder();
            JobParameters jobParameters = parametersBuilder.
                    addString("username", "linux").
                    addString("password", "123456").
                    toJobParameters();
            launcher.run(job, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

友情链接

 

springbatch---->springbatch的使用(四)

标签:conf   tac   java   exec   监听   ott   trace   ica   set   

原文地址:http://www.cnblogs.com/huhx/p/baseusespringbatch4.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!