- 代碼地址(包含相關jar包) :http://pan.baidu.com/s/1c20EtAK 密碼:7ut1
主要jar包
quartz-2.2.3.jar相關文件介紹(基於兩個定時器)
3.1applicationContext-Infrastructure.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 測試job -->
<!-- 配置bean的順序不能換,否則可能報錯 FactoryBean which is currently in creation returned null from getObject-->
<!-- 任務觸發器 -->
<!--
cronExpression知識:
1格式:秒(0-59) 分(0-59) 時(0-23) 日(1-31) 月(1-12) 星期(1-7) 年(空值 1970-2009)
2*:用於所有字段中,表示每秒每分鐘每小時每天...
3?:用於日、星期,表示沒有意義佔位符
4-:用於所有字段中,表示一個範圍,如小時中10-12表示10,11,12
5,:用於所有字段中,表示列表值,小時中10,11,12表示10時、11時、12時
6x/y:用於所有字段中,秒中10/5 表示第10秒起每隔 5秒執行一次
-->
<!-- 1定時器bean類定義 -->
<bean id="flexTimeJob" class="cn.com.timer.infrastructure.quartz.test.FlexTimeJob">
<!-- 指定定時器調度工廠 -->
<property name="scheduler" ref="flexTimeSchedulerFactory" />
<!-- 向定時器注入bean -->
</bean>
<!-- 2任務定義 -->
<bean id="flexTimeJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 任務所在的類 -->
<property name="targetObject" ref="flexTimeJob" />
<!-- 任務所對應的方法名 -->
<property name="targetMethod" value="executeInternal" />
<property name="concurrent" value="false" />
</bean>
<!-- 3任務觸發器 -->
<bean id="flexTimeJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="flexTimeJobDetail"/>
<property name="cronExpression" value="0 50 8 ? * *"/> <!-- 默認每天凌晨0點整打印 -->
<!-- <property name="cronExpression" value="0/5 * * ? * *"/> --> <!-- 默認每隔五秒執行一次FlexTimeJob的executeInternal方法 -->
<property name="startDelay" value="0"/> <!-- 延遲10秒(10000毫秒)啓動 -->
</bean>
<!-- 4調度工廠,觸發器集合 -->
<bean id="flexTimeSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="flexTimeJobTrigger"/>
</list>
</property>
</bean>
<!-- 正式每天23:59:59執行從mongodb查詢到數據並寫入數據庫的job -->
<!-- 1定時器bean類定義 -->
<bean id="timerJob" class="cn.com.timer.infrastructure.quartz.test.TimerJob">
<!-- 指定定時器調度工廠 -->
<property name="scheduler" ref="timerSchedulerFactory" />
<!-- 向定時器注入bean -->
</bean>
<!-- 2任務定義 -->
<bean id="timerJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 任務所在的類 -->
<property name="targetObject" ref="timerJob" />
<!-- 任務所對應的方法名 -->
<property name="targetMethod" value="executeInternal" />
<property name="concurrent" value="false" />
</bean>
<!-- 3任務觸發器 -->
<bean id="timerJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="timerJobDetail"/>
<property name="cronExpression" value="30 48 8 ? * *"/> <!-- 默認每天凌晨0點整打印 -->
<!-- <property name="cronExpression" value="0/5 * * ? * *"/> --> <!-- 默認每隔五秒執行一次FlexTimeJob的executeInternal方法 -->
<property name="startDelay" value="0"/> <!-- 延遲10秒(10000毫秒)啓動 -->
</bean>
<!-- 4調度工廠,觸發器集合 -->
<bean id="timerSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="timerJobTrigger"/>
</list>
</property>
</bean>
</beans>
3.2定時器FlexTimeJob(實現重置定時時間)
package cn.com.timer.infrastructure.quartz.test;
import java.util.Date;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.TriggerKey;
public class FlexTimeJob {
/**
* 調度工廠,可取得所屬的觸發器
*/
private Scheduler scheduler;
/**
* 定時任務執行的方法
*/
protected void executeInternal(){
System.out.println(new Date());
System.out.println("hello FlexTimeJob!");
}
/**
* 重置定時時間任務的方法
*/
public void resetTime(String time)throws Exception{
String cronExpression = transformTime(time);// 時間格式如:13:30
TriggerKey triggerKey = TriggerKey.triggerKey("flexTimeJobTrigger",Scheduler.DEFAULT_GROUP);
CronTrigger trigger = (CronTrigger)scheduler.getTrigger(triggerKey);
String originConExpression = trigger.getCronExpression();
if(!originConExpression.equalsIgnoreCase(cronExpression)){
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//按新的cronExpression表達式重新構建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
.withSchedule(scheduleBuilder).build();
//按新的trigger重新設置job執行
scheduler.rescheduleJob(triggerKey, trigger);
}
}
/**
* 將時間轉換爲cron表達式
* @param time 時間字符串,格式如:13:30
* @return
*/
private String transformTime(String time){
StringBuffer result = new StringBuffer();
String[] arr = time.split(" ");
String[] timeArr = arr[0].split(":");
result.append("0 "+timeArr[1]+" "+timeArr[0]);
result.append(" ? * *");
return result.toString();
}
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
3.2定時器TimerJob
package cn.com.timer.infrastructure.quartz.test;
import java.util.Date;
import org.quartz.Scheduler;
public class TimerJob {
/**
* 調度工廠,可取得的觸發器
*/
private Scheduler scheduler;
/**
* 定時任務執行的方
*/
protected void executeInternal(){
System.out.println(new Date());
System.out.println("hello TimerJob!");
}
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
3.3重置定時執行時間的jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>測試</title>
</head>
<body>
~~~~~~~~~~~~~~~~~~~~~~可重置時間定時器~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<br><br>
<input type="text" name="time" id="time"> 時間格式如:13:30
<br><br>
<button type="button" onclick="resetTime();">設置</button>
<br><br><br>
<script src="<%=basePath%>Scripts/jqueryV2.2.3.min.js"></script>
<script type="text/javascript">
function resetTime(){
$.ajax({
url:"/police_system/test1/flexTimeJob!reset.action?time="+$('#time').val(),
async:true,
success:function(data){
alert(data)
}
});
}
</script>
</body>
</html>
4.訪問地址:http://localhost:8081/timer_test/test1/flexTime.action