haidaoxianzi 2018-07-15
<?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