Spring Batch框架支持抽象的Job定义和Job的继承特性。
一、抽象的Job
通过定义抽象的Job可以将Job的共性进行抽取,形成父类的Job定义,父Job通常具有较多的共性;然后各个具体的Job可以继承父类的Job特性,并定义自己的的属性。
通过abstract属性可以指定Job为抽象的Job。抽象的Job不能被实例化,只能作为其他Job的父Job,其他Job可以继承父Job的所有特性,甚至可以复写掉父中的特性。
<!--定义抽象Job(父Job)-->
<batch:job id="fatherJob" abstract="true">
<!--抽象Job定义的拦截器,即父拦截器-->
<batch:listeners>
<batch:listener ref="myJobExecutionListener"/>
</batch:listeners>
</batch:job>
二、继承Job
通过parent属性可以指定当前Job的父类,类似于Java世界中的继承一样,子Job继承了父Job中定义的所有属性能力。子Job可以从抽象Job中继承,也可以从普通的Job中继承。
<!--定义子Job 继承fatherJob为父Job-->
<batch:job id="sonJob" parent="fatherJob">
<batch:step id="sonStep">
<batch:tasklet ref="myTasklet"/>
</batch:step>
<!--定义子拦截器-->
<batch:listeners merge="true">
<batch:listener ref="myAnnotationListener"/>
</batch:listeners>
</batch:job>
三、使用场景
假设有这样一个场景,所有的Job都希望拦截器AListener能够执行,而拦截器BListener则由每个具体的Job定义是否执行,通过抽象和继承属性就可以完成上面的场景。
四、项目举例
1.项目框架
2.各个部分代码具体实现
Demo5BatchMain.java:
package com.xj.demo5;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author : xjfu
* @Date : 2021/11/11 19:11
* @Description :启动类
*/
public class Demo5BatchMain {
public static void main(String[] args) {
Demo5BatchMain batchMain = new Demo5BatchMain();
//执行Job
batchMain.executeJob("demo5/job/demo5-job.xml", "sonJob", "jobLauncher", new JobParameters());
}
/**
*执行Job
* @param jobXmlPath 配置job的xml文件路径
* @param jobId job的id
* @param jobLauncherId jobLauncher的id
* @param jobParameters 参数
*/
public void executeJob(String jobXmlPath, String jobId, String jobLauncherId, JobParameters jobParameters){
//获得配置Job的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(jobXmlPath);
//获取JobLauncher
JobLauncher launcher = (JobLauncher) context.getBean(jobLauncherId);
//获取要执行的Job
Job job = (Job) context.getBean(jobId);
try{
//开始执行Job
JobExecution result = launcher.run(job, jobParameters);
//输出执行结果
System.out.println(result.toString());
}catch (Exception e){
e.printStackTrace();
}
}
}
MyAnnotationListener.java:
package com.xj.demo5;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.annotation.AfterJob;
import org.springframework.batch.core.annotation.BeforeJob;
/**
* @Author : xjfu
* @Date : 2021/11/7 18:57
* @Description :通过Annotation实现JobExecutionListener接口的拦截器
*/
public class MyAnnotationListener {
//Job执行之前调用该方法
@BeforeJob
public void beforeJob(JobExecution jobExecution){
System.out.println("Son : MyAnnotationListener——before: create time:" + jobExecution.getCreateTime());
}
//Job执行之后调用该方法
@AfterJob
public void afterJob(JobExecution jobExecution){
System.out.println("Son : MyAnnotationListener——after: create time:" + jobExecution.getCreateTime());
}
}
MyJobExecutionListener.java:
package com.xj.demo5;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
/**
* @Author : xjfu
* @Date : 2021/11/7 18:53
* @Description :实现JobExecutionListener接口的拦截器
*/
public class MyJobExecutionListener implements JobExecutionListener {
//Job执行之前调用该方法
@Override
public void beforeJob(JobExecution jobExecution) {
System.out.println("Father : MyJobExecutionListener——before: create time:" + jobExecution.getCreateTime());
}
//Job执行之后调用该方法
@Override
public void afterJob(JobExecution jobExecution) {
System.out.println("Father : MyJobExecutionListener——after: create time:" + jobExecution.getCreateTime());
}
}
MyTasklet.java:
package com.xj.demo5;
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 : xjfu
* @Date : 2021/11/11 19:27
* @Description :demo5的Tasklet
*/
public class MyTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("**********Demo5的Tasklet执行啦*********");
return RepeatStatus.FINISHED;
}
}
demo5-jobContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--定义JobRepository-->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<!--定义事务管理器-->
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<!--定义JobLauncher-->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<!--定义Tasklet-->
<bean id="myTasklet" class="com.xj.demo5.MyTasklet"/>
<!--定义以继承接口方式实现的拦截器-->
<bean id="myJobExecutionListener" class="com.xj.demo5.MyJobExecutionListener"/>
<!--定义以Annotation方式实现的拦截器-->
<bean id="myAnnotationListener" class="com.xj.demo5.MyAnnotationListener"/>
</beans>
demo5-job.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<import resource="classpath:demo5/job/demo5-jobContext.xml"/>
<!--定义抽象Job(父Job)-->
<batch:job id="fatherJob" abstract="true">
<!--抽象Job定义的拦截器,即父拦截器-->
<batch:listeners>
<batch:listener ref="myJobExecutionListener"/>
</batch:listeners>
</batch:job>
<!--定义子Job 继承fatherJob为父Job-->
<batch:job id="sonJob" parent="fatherJob">
<batch:step id="sonStep">
<batch:tasklet ref="myTasklet"/>
</batch:step>
<!--定义子拦截器-->
<batch:listeners merge="true">
<batch:listener ref="myAnnotationListener"/>
</batch:listeners>
</batch:job>
</beans>
注意:
1.通过abstract将fatherJob定义为一个抽象的Job,再通过parent使sonJob继承fatherJob。
2.merge说明:如果父子Job中均定义了拦截器,则可以通过设置merge属性为true对拦截器列表合并,即songJob运行时,会有两个拦截器,分别是fatherJob的myJobExecutionListener拦截器和自身定义的myAnnotationListener拦截器;如果设置merge属性为false,则子Job中定义的拦截器直接覆盖掉父Job中定义的拦截器。