此文章是基于  搭建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!",重置后每天按时间定时输出

spring实现可重置时间定时器的更多相关文章

  1. [置顶] ios 时间定时器 NSTimer应用demo

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:ios NSTimer应用demo .ipho ...

  2. 2018.7.6 js实现点击事件---点击小图出现大图---时间定时器----注册表单验证

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  3. Libev源码分析04:Libev中的相对时间定时器

    Libev中的超时监视器ev_timer,就是简单的相对时间定时器,它会在给定的时间点触发超时事件,还可以在固定的时间间隔之后再次触发超时事件. 所谓的相对时间,指的是如果你注册了一个1小时的超时事件 ...

  4. clion2020最新安装破解版教程 内含激活码、破解补丁、无限重置时间插件

    clion是一款开发C.C++等相关程序的利器,到目前为止已经更新到2020.3版本啦!还没有升级的小伙伴们赶紧升级啦,本文教大家如何安装clion2020.3版本并且破解,此方法亲测100%可以永久 ...

  5. Libev——ev_timer 相对时间定时器

    Libev中的超时监视器ev_timer,是简单的相对时间定时器,它会在给定的时间点触发超时事件,还可以在固定的时间间隔之后再次触发超时事件. 1.超时监视器ev_timer结构 typedef st ...

  6. spring实现固定时间定时器

    此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. spring-framework-4.3.4.RELEASE 的 lib ...

  7. spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象

    最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...

  8. Spring整合Quartz实现动态定时器

    一.版本说明 spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然会出错. 原因:spring对于quartz的支持实现,org.springf ...

  9. Spring整合Quartz实现动态定时器,相关api,定时器添加,删除,修改

    一.版本说明 spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然会出错. 原因:spring对于quartz的支持实现,org.springf ...

随机推荐

  1. SSO单点登录入门

    1,SSO简介 SSO(Single Sign-On,单点登录)是身份管理中的一部分.SSO 的一种较为通俗的定义是:SSO 是指访问同一服务器不同应用中的受保护资源的同一用户,只需要登录一次,即通过 ...

  2. Jmeter后置处理器之JSON Extractor

    一.使用场景 json extractor后置处理器用在返回格式为json的HTTP请求中,用来获取返回的json中的某个值.并保存成变量供后面的请求进行调用或断言等. 二.使用方法 步骤一:选择HT ...

  3. 谷歌支付服务端详细讲解(PHP)

    前不久公司拓展海外市场,要接入google支付.刚开始一头雾水,相关的文档实在太少.而且很多东西都需要FQ,不过好在摸索几天后,总算调试通了. 前提:FQ 1.注册账号google账号 https:/ ...

  4. 阿里VS华为-开源镜像站体验及评测

    最近对阿里和华为的开源镜像站做了深度体验,并将评测结果分享给大家: 一.评测产品: 华为开源镜像站(https://mirrors.huaweicloud.com/)以下简称 华为 阿里开源镜像站(h ...

  5. git 常用口令

    版本管理 svn git   cd d 切换目录 cd www cd git git clone 一个地址 git status 获取修改的内容 git add * 上传修改的内容 git commi ...

  6. QRCode简介(收藏)

      一.什么是二维码:二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常 ...

  7. BT网站-IBMID.COM

    最近把網站改版了,主要是更改了搜索引擎. 大家可以訪問 什么是磁力链接(IBMID.COM)(Magnet URI)? 简单的说:类似下面这样以“magnet:?”开头的字符串,就是一条“磁力链接” ...

  8. 剑指offer——面试题17:打印从1到最大的n位数

    用字符串模拟加法: #include"iostream" #include"string.h" using namespace std; bool AddOne ...

  9. mongo嵌套查询

    db.getCollection('TradeBookingRepresentation').find({uitid:'168282:20190214010009224', tradeVersion: ...

  10. 经典C面试真题精讲

    第一章 灵魂--指针 101. p++ 请分析下述代码的输出结果 int main() { char* p1 = "China"; char* p2, * p3; p2 = (); ...