不多说直接上代码
用的maven管理哈哈
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>
首先是配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="reloadTask" class="com.demo.schedule.SchedulerTask"
init-method="initial"></bean>
<bean id="jobDetail-reload"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="reloadTask" />
</property>
<property name="targetMethod">
<value>reload</value>
</property>
</bean>
<bean id="trigger-reload"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobDetail-reload" />
</property>
<property name="cronExpression">
<value>0 0 0 * * ? </value>
</property>
</bean>
<bean id="registerQuartz"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="trigger-reload" />
</list>
</property>
</bean>
</beans>
定时任务类
package com.demo.schedule;
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class SchedulerTask {
@Autowired
@Qualifier("trigger-reload")
private CronTrigger cronTrigger;
@Autowired
@Qualifier("registerQuartz")
private Scheduler scheduler;
public void initial() throws ParseException, SchedulerException {
checkScheduleTime();
}
private void checkScheduleTime() throws ParseException, SchedulerException {
// 5秒一次
String scheduleTime = "0/5 * * * * ?";
if (scheduleTime != null) {
String oriCronExpression = cronTrigger.getCronExpression();
if (!oriCronExpression.equals(scheduleTime)) {
cronTrigger.setCronExpression(scheduleTime);
scheduler.rescheduleJob("trigger-reload", Scheduler.DEFAULT_GROUP, cronTrigger);
}
}
}
public static int ORIGINAL_VERSION = 0;
public void reload() throws Exception {
int versionId = 0; // 可以从数据库取版本号比对更新,对吧 嘿嘿。。
if (ORIGINAL_VERSION == versionId) {
ORIGINAL_VERSION = versionId;
System.out.println("start ==============...");
}
}
}
ok 完事 复制到工程中绝对好使 。。。。。。。。。。。。。。。。。。。。。。。。