栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Batch的拦截器实现(四)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Batch的拦截器实现(四)

一、拦截器实现说明

1.拦截器实现的方式

Spring Batch项目实现拦截器有两种方法:

(1)实现接口:org.springframework.batch.core.JobExecutionListener

public interface JobExecutionListener {
    ///ob执行之前调用该方法
    void beforeJob(JobExecution var1);

    //Job执行之后调用该方法
    void afterJob(JobExecution var1);
}

(2)通过Annotation机制实现

  • @BeforeJob 
  • @AfterJob

2.拦截器的配置

通过下面的方法配置拦截器:

   
       
                      . . . . . .
       

       
       
           
       

   

     
   

3.拦截器异常

       拦截器方法如果抛出异常会影响Job的正常执行,所以在执行自定义的拦截器时候,要考虑对拦截器发生的异常做处理,避免影响业务。如果拦截器发生异常,会导致Job执行的状态为“FAILED”。

4.拦截器执行顺序

        在配置文件中可以配置多个listener,拦截器之间的执行顺序按照listener定义的顺序执行。before方法按照listener定义的顺序执行,after方法按照相反的顺序执行。(参考下面项目的运行结果)

二、拦截器项目实现

1.项目结构:

 2.代码实现:

Demo3BatchMain.java:

package com.xj.demo3;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Demo3BatchMain {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("demo3/job/demo3-job.xml");
        //Spring Batch的作业启动器
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        //在demo3-jobContext.xml中配置一个作业
        Job job = (Job) context.getBean("demo3TaskletJob");
        
        try{
            //开始执行这个作业,获得出来结果(要运行的job,job参数对象)
            JobExecution result = launcher.run(job, new JobParameters());
            System.out.println(result.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

MyAnnotationListener.java:

package com.xj.demo3;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.annotation.AfterJob;
import org.springframework.batch.core.annotation.BeforeJob;


public class MyAnnotationListener {

    //Job执行之前调用该方法
    @BeforeJob
    public void beforeJob(JobExecution jobExecution){
        System.out.println("MyAnnotationListener——before: create time:" + jobExecution.getCreateTime());

    }

    //Job执行之后调用该方法
    @AfterJob
    public void afterJob(JobExecution jobExecution){
        System.out.println("MyAnnotationListener——after: create time:" + jobExecution.getCreateTime());

    }
}

MyJobExecutionListener.java:

package com.xj.demo3;

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


public class MyJobExecutionListener implements JobExecutionListener {

    //Job执行之前调用该方法
    @Override
    public void beforeJob(JobExecution jobExecution) {
        System.out.println("MyJobExecutionListener——before: create time:" + jobExecution.getCreateTime());
    }

    //Job执行之后调用该方法
    @Override
    public void afterJob(JobExecution jobExecution) {
        System.out.println("MyJobExecutionListener——after: create time:" + jobExecution.getCreateTime());
    }
}

MyTasklet.java:

package com.xj.demo3;

import org.apache.commons.collections.bag.SynchronizedSortedBag;
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 class MyTasklet implements Tasklet {
    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

        System.out.println("This is a test for interceptor(拦截器)");

        return RepeatStatus.FINISHED;
    }
}

demo3-job.xml:




    

    
        
            
        
        
        
            
            
        
    

demo3-jobContext.xml:




    

    

    
        
    

    
    

    
    

    
    

3.运行结果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号