为了更灵活的使用quartz的功能,持久化job和trigger是非常有必要的。
1、quartz内存方式和持久化方式的对比
优点 | 缺点 | |
内存方式 | 实现简单 | 配置不够灵活、不方便服务的扩充(集群)、不能再中断后恢复 |
持久化方式 | 持久化的缺点 | 速度相对内存方式会慢一点 |
2、spring-quartz-db.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 数据连接 -->
<property name="dataSource" ref="dataSource" />
<!-- 调度名称,会被记录到数据库 -->
<property name="schedulerName" value="myscheduler" />
<!-- quartz配置信息 -->
<property name="configLocation" value="classpath:config/quartz.properties"/>
<!--applicationContextSchedulerContextKey:
是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下文
以key/value的方式存放在了quartz的上下文中了,
可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文
-->
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<!-- 是否自动启动 -->
<property name="autoStartup" value="true" />
<!-- 配置job工厂 -->
<property name="jobFactory" ref="jobFactory" />
</bean>
<!-- 启动触发器的配置结束 -->
<!--job工厂、如果需要使用spring的bean注入到job就需要重写一个jobfactory-->
<bean id="jobFactory" class="com.syx.quartz.MyJobFactory"/>
<!-- 作业服务类 -->
<bean id="myJob" class="com.syx.quartz.MyJob" />
</beans>
3、重写jobFactory
/**
* Job工厂
* 默认的job工厂方式不能实现springbean的注入,因为quartz在spring之前初始化
*
* @author sunyx
* @since JDK 1.8
*/
public class MyJobFactory extends SpringBeanJobFactory {
@Autowired
private AutowireCapableBeanFactory beanFactory;
/*
* 重写SpringBeanJobFactory的createJobInstance方法,对其创建出来的类再进行autowire。
* @see org.springframework.scheduling.quartz.SpringBeanJobFactory#createJobInstance(org.quartz.spi.TriggerFiredBundle)
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
final Object jobInstance = super.createJobInstance(bundle);
beanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
4、创建工作类
/**
* 任务
*
* @author sunyx
* @since JDK 1.8
*/
public class MyJob implements Job{
@Autowired
private UserMapper userMapper;
public void worksyx() {
final UserModel model = userMapper.findUserById("1");
System.out.println("date:" + new Date().toString()+";user:"+model.getName());
}
public void execute(JobExecutionContext context) throws JobExecutionException {
final JobDataMap dataMap= context.getJobDetail().getJobDataMap();
final String table = dataMap.getString("table");
System.out.println("table:"+table);
this.worksyx();
}
}
5、到这一步基本上就完成了,现在数据库里面没有存job和trigger。编写一个业务逻辑来创建job和trigger。
/**
* 控制器
*
* @author sunyx
* @since JDK 1.8
*/
@Controller
public class SchedulerController {
private final boolean init = false;
@Autowired
private JobService jobService;
/**
* 主页面
*
* @author sunyx
* @param request
* @return
* @since JDK 1.8
*/
@RequestMapping("/show")
public String show(HttpServletRequest request){
final List<PageTrigger> triggers = jobService.getTriggersInfo();
<span style="white-space:pre"> </span>request.setAttribute("triggers", triggers);
}
/**
* 添加一个任务
*
* @author sunyx
* @param request
* @return
* @since JDK 1.8
*/
@RequestMapping("/add")
public String add(HttpServletRequest request){
try {
jobService.addJob();
} catch (final SchedulerException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return "redirect:/show";
}
}