springBatch学习 batch的使用方式(5)
首先讲一下batch框架提供的一组job执行的api 如下图
说明:
应用场景 包含三种 标准的web ,定时任务调度,命令行
1.命令行
通过命令行在单独的jvm中调用 进行批处理作业 springbatch提供了命令行执行类 commandLineJobRunner
1.将项目构件为jar
2. 进入jar项目目录 执行jar包
注意: 默认从classpath路径开始加载配置文件
-restart 根据job名称重启最后一次失败作业
-stop 根据job名称停止正在执行的作业
-abandon 废弃正在执行的作业
-next 根据jobparameters去执行下一次作业
如果需要自定义任务退出状态 实现exicodetmapper 然后配置即可
2.与定时任务集成
结合spring schdule 可以很简单的实现定时集成
1.定义一个schdule 提供执行定时任务的线程
2.定义所需要的方法和调度周期
<task:scheduler id="scheduler" pool-size="10" /> 定义线程池大小 <!-- 每一秒钟,执行对象schedulerLauncher的launch方法一次 --> <task:scheduled-tasks scheduler="scheduler"> <task:scheduled ref="schedulerLauncher" method="launch" fixed-rate="1000" /> fixrate代表一秒执行一次 在schduleLaucher中的lauch方法 </task:scheduled-tasks> <bean:bean id="schedulerLauncher" class="com.juxtapose.example.ch04.scheduler.SchedulerLauncher"> <bean:property name="job" ref="helloworldJob" /> <bean:property name="jobLauncher" ref="jobLauncher" /> </bean:bean> 省略其余配置
3.与web集成
spring barch 有spring基础开发完成 内嵌在spring框架中提供http协议远成调用或者web系统定时调用
如下图:
//定义一个controller提供调用
import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller public class JobLauncherController { private static final String JOB_NAME = "jobName"; private JobLauncher jobLauncher; private JobRegistry jobRegistry; public JobLauncherController(JobLauncher jobLauncher, JobRegistry jobRegistry) { this.jobLauncher = jobLauncher; this.jobRegistry = jobRegistry; } @RequestMapping(value="executeJob",method=RequestMethod.GET) public void launch(@RequestParam String jobName,HttpServletRequest request) throws Exception { JobParameters jobParameters = bulidParameters(request); jobLauncher.run( jobRegistry.getJob(jobName),jobParameters); } private JobParameters bulidParameters(HttpServletRequest request) { JobParametersBuilder builder = new JobParametersBuilder(); @SuppressWarnings("unchecked") Enumeration<String> paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); if(!JOB_NAME.equals(paramName)) { builder.addString(paramName,request.getParameter(paramName)); } } return builder.toJobParameters(); } }
配置文件
<!-- 作业仓库 --> <job-repository id="jobRepository" data-source="dataSource" transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE" table-prefix="BATCH_" max-varchar-length="1000" /> <!-- 作业调度器 --> <bean:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <bean:property name="jobRepository" ref="jobRepository"/> </bean:bean>
通过这个类 可以在job被加载后加入注册。然后被获取 <bean:bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor"> <bean:property name="jobRegistry" ref="jobRegistry" /> </bean:bean> <bean:bean id="jobRegistry" job注册 class="org.springframework.batch.core.configuration.support.MapJobRegistry" /> <!-- 事务管理器 --> <bean:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <bean:property name="dataSource" ref="dataSource" /> </bean:bean> <!-- 数据源 --> <bean:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <bean:property name="driverClassName"> <bean:value>com.mysql.jdbc.Driver</bean:value> </bean:property> <bean:property name="url"> <bean:value>jdbc:mysql://127.0.0.1:3306/test1289</bean:value> </bean:property> <bean:property name="username" value="root"></bean:property> <bean:property name="password" value="000000"></bean:property> </bean:bean> </bean:beans>
构造controlller
<bean class="com.juxtapose.example.ch04.web.JobLauncherController">
<constructor-arg ref="jobLauncher" />
<constructor-arg ref="jobRegistry" />
</bean>
</beans>
别的配置 如web.xml之类的省略。
启动web项目 就可以通过url访问了
基本就以上三种方法