Spring也提供了对Quartz的支持,它对Quzrtz的核心类进行了封装,使开发人员更便捷地实现任务调度。另外,整合Spring和Quartz,可以使用声明的方式配置计划任务,大大简化了操作步骤,而且也降低了代码耦合。
下面我们通过Spring框架来重构【http://blog.csdn.net/wx5040257/article/details/78780400】提出的问题。主要通过3步来完成。
1.使用JobDetaiIBean创建Quartz工作
在Quartz框架中,开发人员使用JobDetail创建任务对象,通过JobExecutionContext从JobDataMap中读取任务数据。Spring框架为了简化操作,提供了QuartzjobBean类,它实现了Quartz中的org.quartz.Job接口。该接口中的executelntemal()方法,定义了在预定的时刻到来时应该执行的计划任务。因此,我们需要从该类中派生子类来创建任务。然后通过扩展QuartzjobBean来创建Remindjob,并实现它的executelnternal()方法。
2.使用SimpleTriggerBean调度定时任务
接下来,需要创建触发器来定义时间规则。类似于Quartz中的SimpleTrigger,在Spring中可以使用SimpleTriggerBean来完成同样的功能。只需要在Spring的Bean配置文件中配置一个SimpleTriggerBean对象,并添加对JobDetail对象的引用。
3.使用SchedulerFactoryBean注册JobDetail和Trigger
最后,就是要配置调度器。在Spring中的Bean配置文件中可以配置一个SchedulerFactoryBean实例,可以在这个工厂Bean中指定多个触发器。最后将触发器添加到SchedulerFactoryBean中。
完整案例==============TaskEntity.java================
package com.obtk.entitys;
public class TaskEntity {
private String dateStr;
private String task;
public TaskEntity() {
}
public TaskEntity(String dateStr, String task) {
super();
this.dateStr = dateStr;
this.task = task;
}
public String getDateStr() {
return dateStr;
}
public void setDateStr(String dateStr) {
this.dateStr = dateStr;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
public void showInfo(){
System.out.println(this.dateStr+"===>"+this.task);
}
}
================TaskDao.java=====================
package com.obtk.dao;
import java.util.ArrayList;
import java.util.List;
import com.obtk.entitys.TaskEntity;
public class TaskDao {
public List<TaskEntity> queryAllTask(){
List<TaskEntity> taskList=new ArrayList<TaskEntity>();
TaskEntity task1=new TaskEntity("2018-03-15 08:00:00", "起床执行打假任务");
TaskEntity task2=new TaskEntity("2018-03-15 20:30:00", "到打假办公室315进行研讨");
taskList.add(task1);
taskList.add(task2);
return taskList;
}
}
==================SpringDaJiaJob.java====================
package com.obtk.jobs;
import java.util.List;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.obtk.dao.TaskDao;
import com.obtk.entitys.TaskEntity;
public class SpringDaJiaJob extends QuartzJobBean{
private String userName;
private TaskDao taskDao;
public void setUserName(String userName) {
this.userName = userName;
}
public void setTaskDao(TaskDao taskDao) {
this.taskDao = taskDao;
}
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
List<TaskEntity> taskList=taskDao.queryAllTask();
System.out.println("请注意,这是"+userName+"发的消息==>");
for(TaskEntity task : taskList){
//输出打假任务通知的内容
task.showInfo();
}
}
}
spring配置如下
<?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-3.0.xsd">
<bean id="taskDao" class="com.obtk.dao.TaskDao"></bean>
<!-- job -->
<bean id="remindJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.obtk.jobs.SpringDaJiaJob"></property>
<property name="jobDataAsMap">
<map>
<entry key="taskDao" value-ref="taskDao"></entry>
<entry key="userName" value="孙主任"></entry>
</map>
</property>
</bean>
<!-- 触发器 -->
<bean id="myTrig" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="remindJob"></property>
<property name="startDelay" value="1000"></property>
<property name="repeatInterval" value="3000"></property>
<property name="repeatCount" value="4"></property>
</bean>
<!-- cron触发器 -->
<bean id="cronTrig" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="remindJob"></property>
<property name="cronExpression" value="5/5 * * 13 * ? 2017"></property>
</bean>
<!-- 调度器 -->
<bean id="mySched" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="myTrig"/> -->
<ref bean="cronTrig"/>
</list>
</property>
<property name="waitForJobsToCompleteOnShutdown" value="true"></property>
</bean>
</beans>
测试类
package com.obtk.test;
import org.quartz.Scheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring2 {
public static void main(String[] args) {
ApplicationContext context=
new ClassPathXmlApplicationContext("ApplicationContext.xml");
//这条语句后任务就开始执行了
Scheduler mySched=(Scheduler)context.getBean("mySched");
try {
Thread.sleep(5000);
//下面是强行停止
mySched.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}