JobLauncher支持对作业的同步、异步两种调用模式。默认情况下,JobLauncher的run操作通过同步方式调用Job,即任何调用Job的客户端需要等待Job的执行结果返回后才能结束。
现在可以通过异步调用模式,即调用Job的客户端无需一直等待Job执行结果,可以先返回,等过段时间来查询当前作业的执行情况。
一、异步调用配置
(1).配置一个线程池
(2).为JobLauncher配置线程池
二、项目举例
1.项目架构
2.代码实现
Demo7BatchMain.java:
package com.xj.demo7;
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/18 8:37
* @Description :异步JobLauncher测试
*/
public class Demo7BatchMain {
public static void main(String[] args) {
Demo7BatchMain batchMain = new Demo7BatchMain();
batchMain.executeJob("demo7/job/demo7-job.xml", "jobLauncher", "demo7Job", new JobParameters());
}
public void executeJob(String jobXmlPath, String jobLauncherId, String jobId, JobParameters jobParameters){
//获取Job的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(jobXmlPath);
//获取JobLauncher
JobLauncher jobLauncher = (JobLauncher) context.getBean(jobLauncherId);
//获取要执行的Job
Job job = (Job)context.getBean(jobId);
try {
//执行Job
JobExecution result = jobLauncher.run(job,jobParameters);
//输出执行结果
System.out.println(result.toString());
}catch (Exception e){
e.printStackTrace();
}
}
}
MyTasklet.java:
package com.xj.demo7;
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/18 8:49
* @Description :demo7的Tasklet
*/
public class MyTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("*****demo7的MyTasklet执行啦*****");
return RepeatStatus.FINISHED;
}
}
demo7-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:demo7/job/demo7-jobContext.xml"/>
<!--定义Job-->
<batch:job id="demo7Job">
<batch:step id="demo7Step">
<batch:tasklet ref="myTasklet"/>
</batch:step>
</batch:job>
</beans>
demo7-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" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!--定义JobRepository-->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<!--定义异步JobLauncher 线城池为5-->
<task:executor id="executor" pool-size="5"/>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
<!--为作业调度器配置执行的线程池,作业执行期间会从线程池中选择一个线程执行Job-->
<property name="taskExecutor" ref="executor"/>
</bean>
<!--定义TransactionManager-->
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<!--定义Tasklet-->
<bean id="myTasklet" class="com.xj.demo7.MyTasklet"/>
</beans>
3.执行结果
仔细观察控制台输出:首先是输出Job Execution的信息,然后输出具体的Job执行的具体信息。即在JobLauncher的run方法被调用后,客户单立刻返回;同时后台使用其他线程执行Job作业。