此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

一. jar包介绍

  1. spring-framework-4.3.4.RELEASE 的 libs 文件夹下得到:

spring-context-support-4.3.4.RELEASE.jar

  2. quartz-2.2.3.jar

二. 相关文件介绍

  1. applicationInfrastructure.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"> <!-- 定时器bean类定义 -->
<bean id="flexTimeJob" class="com.ims.infrastructure.quartz.FlexTimeJob">
<!-- 指定定时器调度工厂 -->
<property name="scheduler" ref="flexTimeSchedulerFactory" />
<!-- 向定时器注入bean -->
</bean> <!-- 任务定义 -->
<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> <!-- 任务触发器 -->
<bean id="flexTimeJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="flexTimeJobDetail"/>
<property name="cronExpression" value="0 0 0 ? * *"/> <!-- 默认每天凌晨0点整打印 -->
<property name="startDelay" value="10000"/> <!-- 延迟10秒(10000毫秒)启动 -->
</bean> <!-- 调度工厂,触发器集合 -->
<bean id="flexTimeSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="flexTimeJobTrigger"/>
</list>
</property>
</bean> </beans>

  2. FlexTimeJob.java,定时器任务类

package com.ims.infrastructure.quartz;

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("hello!");
} /**
* 重置定时时间任务的方法
*/
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. flexTime.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
<%@ include file="/common/basePath.jsp"%>
</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 type="text/javascript" src="content/js/jquery/jquery-1.8.1.min.js"></script>
<script type="text/javascript"> function resetTime(){
$.ajax({
url:rootPath+"/test/flexTimeJob!reset.do?time="+$('#time').val(),
async:false
});
} </script>
</body>
</html>

三. 测试

  访问:http://localhost:8080/ims/test/flexTime.do,默认每天凌晨0时,后台会输出"hello!",重置后每天按时间定时输出

最新文章

  1. NOSDK--SDK一键打包及统一接入的实现(前言)
  2. ucenter 通信成功后 apps.php无误后 该做的事
  3. dom core,html dom,css dom,jquery 中的dom操作
  4. 10.10 dos试验
  5. 《算法导论》习题解答 Chapter 22.1-5(求平方图)
  6. 集合框架学习之Guava Collection
  7. Vim 保存和退出命令
  8. 安装WordPress详细教程指南
  9. JPA的介绍
  10. php 10.1总
  11. maven私服nexus搭建(windows)
  12. Entitas Learning Document
  13. simple shell
  14. angular2.0 官网架构文档
  15. 安装Anaconda3进行python版本管理
  16. hdu 1754 线段树(单点替换 区间最值)
  17. 不存数据库生成验证码(totp算法)
  18. angular-resource版本差异问题
  19. XML_CPP_资料_libXml2_01_Code
  20. mysql 慢查询记录方法

热门文章

  1. [译文]casperjs 的API-casper模块
  2. 获取Android状态栏的高度
  3. 读《3M 利率分析新框架》
  4. 洛谷 P4307 [JSOI2009]球队收益 / 球队预算(最小费用最大流)
  5. AXI协议(一)
  6. Tomcat中查看JVM内存使用情况
  7. maven 可执行jar maven-shade-plugin
  8. thinkphp5.0升级
  9. 当Appium中遇到alert(python篇)
  10. V1-bug Alpha阶段测试报告