javascript
Spring Batch之Job级拦截器实现(四)
一、攔截器實現說明
1.攔截器實現的方式
Spring Batch項目實現Job級攔截器有兩種方法:
(1)實現接口:org.springframework.batch.core.JobExecutionListener
public interface JobExecutionListener {//Job執行之前調用該方法void beforeJob(JobExecution var1);//Job執行之后調用該方法void afterJob(JobExecution var1); }(2)通過Annotation機制實現
- @BeforeJob?
- @AfterJob
2.攔截器的配置
通過下面的方法配置攔截器:
? ? <batch:job id="jobId">
? ? ? ? <batch:step id="stepId">
? ? ? ? ? ? ? ? ? ? ? . . . . . .
? ? ? ? </batch:step>
? ? ? ? <!--定義攔截器-->
? ? ? ? <batch:listeners>
? ? ? ? ? ? <batch:listener ref="listenerId"/>
? ? ? ? </batch:listeners>
? ? </batch:job>
? ? ?<!--攔截器實現類-->
? ? <bean id="listenerId" class="com.xj.demo3.MyJobExecutionListener"/>
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;/*** @Author : xjfu* @Date : 2021/11/7 18:29* @Description :*/ 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;/*** @Author : xjfu* @Date : 2021/11/7 18:57* @Description :通過Annotation實現JobExecutionListener接口的攔截器*/ public class MyAnnotationListener {//Job執行之前調用該方法@BeforeJobpublic void beforeJob(JobExecution jobExecution){System.out.println("MyAnnotationListener——before: create time:" + jobExecution.getCreateTime());}//Job執行之后調用該方法@AfterJobpublic 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;/*** @Author : xjfu* @Date : 2021/11/7 18:53* @Description :實現JobExecutionListener接口的攔截器*/ public class MyJobExecutionListener implements JobExecutionListener {//Job執行之前調用該方法@Overridepublic void beforeJob(JobExecution jobExecution) {System.out.println("MyJobExecutionListener——before: create time:" + jobExecution.getCreateTime());}//Job執行之后調用該方法@Overridepublic 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;/*** @Author : xjfu* @Date : 2021/11/7 18:29* @Description :*/ public class MyTasklet implements Tasklet {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println("This is a test for interceptor(攔截器)");return RepeatStatus.FINISHED;} }demo3-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:demo3/job/demo3-jobContext.xml"/><batch:job id="demo3TaskletJob"><batch:step id="demo3Step"><batch:tasklet ref="myTasklet"/></batch:step><!--定義兩個攔截器--><batch:listeners><batch:listener ref="myJobExecutionListener"/><batch:listener ref="myAnnotationListener"/></batch:listeners></batch:job> </beans>demo3-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"><bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"></bean><bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/><bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><property name="jobRepository" ref="jobRepository"/></bean><!--定義MyTasklet類--><bean id="myTasklet" class="com.xj.demo3.MyTasklet"/><!--定義MyJobExecutionListener攔截器--><bean id="myJobExecutionListener" class="com.xj.demo3.MyJobExecutionListener"/><!--定義MyAnnotationListener攔截器--><bean id="myAnnotationListener" class="com.xj.demo3.MyAnnotationListener"/> </beans>3.運行結果:
攔截器的執行順序:
(1)MyJobExecutionListener的before()方法;
(2)MyAnnotationListener的before()方法;
(3)MyAnnotationListener的after()方法;
(4)MyJobExecutionListener的after()方法。
總結
以上是生活随笔為你收集整理的Spring Batch之Job级拦截器实现(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redisTemplate获得key的过
- 下一篇: VMware下Centos7快速搭建vs