1.需求说明


主要负责项目任务调度。使用Quartz。以Spring为辅助。

如今有这样一个需求:我们不知道管理员想设定过多厂时间运行主任务,须要在配置文件定义。在配置好后须要马上运行。实现热部署。



2.设计


在主调度加一个方法,此方法先检測配置文件是否更改。若更改则将调度时间又一次设置,又一次启用调度任务

3.实现


在PropertiesUtil定义一一个初始时间,改时间在这个类初始化之前运行。也就是在静态代码块运行,代码例如以下:
private static long curModifiedTime;
static {
try {
curModifiedTime = new File(filePath).lastModified();
System.out.println("当前文件改动时间为 " +curModifiedTime);
} catch (Exception e) {
e.printStackTrace();
} }

此时获取到文件本来的时间


写一个方法来推断配置文件是否改动:
public static boolean isModifiedValue()
{
boolean flag = false;
long lastModifiedTime = new File(filePath).lastModified();
if(lastModifiedTime > curModifiedTime) {
curModifiedTime= lastModifiedTime;
flag = true;
}
return flag;
}

读取文件最后改动地址。若大于初始时间。则将最后改动时间赋给初始时间

public boolean reSetTaskSchedulerzIntervalTime(){
boolean flag = false;
//监听到配置文件改动
if(PropertiesUtil.isModifiedValue()) {
long time = 0L;
try {
time = Integer.parseInt(PropertiesUtil.readValue(TaskSchedulerzIntervalTime));
simpleTrigger.setRepeatInterval(time);
Scheduler scheduler = quartzScheduler.getScheduler();
String jobName = simpleTrigger.getName();
String group = simpleTrigger.getGroup();
try {
scheduler.pauseJob(jobName, group);
scheduler.unscheduleJob(jobName,group);
scheduler.scheduleJob(simpleTrigger);
scheduler.resumeJob(jobName, group);
flag = true;
} catch (SchedulerException e1) {
e1.printStackTrace();
}
} catch (NumberFormatException e) {
System.out.println("定时任务非数字! ");
e.printStackTrace();
}
}
return flag;
}

当中,quartzScheduler和simpleTrigger是注入进去的,在Spring中整合Quartz时已经定义好,配置文件例如以下

<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="jobDetail" />
</property>
<property name="startDelay">
<value>0</value>
</property>
<property name="repeatInterval" value="${taskSchedulerzIntervalTime}" />
</bean> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="taskScheduler" />
<property name="targetMethod" value="run" />
</bean>

之前的调度时间是在里面写死的。如今是通过配置文件来读取。例如以下


<context:property-placeholder location="classpath:config.properties,classpath:jdbc.properties" />

測试例如以下


watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFja2Uy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">


实际工程Quartz与Spring设计与实现一体化的热部署的更多相关文章

  1. spring boot (三): 热部署

    介绍了Spring boot实现热部署的两种方式,这两种方法分别是使用 Spring Loaded和使用spring-boot-devtools进行热部署. 热部署是什么 大家都知道在项目开发过程中, ...

  2. spring boot 启动报错(spring-boot-devtools热部署后):The elements [spring.resources.cache-period] were left unbound. Update your application's configuration

    详细错误代码: *************************** APPLICATION FAILED TO START *************************** Descript ...

  3. Spring Boot 要点--启动类和热部署

    spring boot需要一个启动类 比如 package com.tianmaying; import org.springframework.boot.SpringApplication; imp ...

  4. Spring Boot在开发时实现热部署(开发时修改文件保存后自动重启应用)(spring-boot-devtools)

    热部署是什么 大家都知道在项目开发过程中,常常会改动页面数据或者修改数据结构,为了显示改动效果,往往需要重启应用查看改变效果,其实就是重新编译生成了新的Class文件,这个文件里记录着和代码等对应的各 ...

  5. spring boot+ Intellj idea devtools 设置热部署

    POM文件 <!--添加依赖--> <dependency> <groupId>org.springframework.boot</groupId> & ...

  6. 使用eclipse,对spring boot 进行springloader或者devtool热部署失败处理方法

    确定配置进行依赖和配置没有错误后. 调整spring boot 的版本,因为新版本对老版本的spring boot 不能使用. 改为: <groupId>org.springframewo ...

  7. 使用Spring Loader或者Jrebel实现java 热部署

    .其实JRebel和Spring-Loaded就是一个开发环境下的利器,skip build and redeploy process,大大提升了工作效率!而非生产环境的利器...因为线上reload ...

  8. Spring Boot学习笔记(三)实现热部署

    pom文件中添加如下依赖即可 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  9. spring boot 在idea中实现热部署

    1)在pom中直接引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

随机推荐

  1. 设置Maven默认的JDK为1.7,解决Update Maven Project默认为1.5和Maven打包报错2个问题

    1.之前,一直遇到这个问题. Update Maven Project的时候,JDK变成了1.5的.    如果项目中有使用"@overdide",程序就会报错,需要手动修改JRE ...

  2. Stack overflow 编译能通过,运行时出现Stack overflow

    Stack overflow 编译能通过,运行时出现Stack overflow 大家都知道,Windows程序的内存机制大概是这样的,全局变量(局部的静态变量本质也属于此范围)存储于堆内存,该段内存 ...

  3. 00092_字符输出流Writer

    1.字符输出流Writer (1)既然有专门用于读取字符的流对象,那么肯定也有写的字符流对象: (2)查阅API,发现有一个Writer类,Writer是写入字符流的抽象类.其中描述了相应的写的动作. ...

  4. android问题及其解决-优化listView卡顿和怎样禁用ListView的fling

    问题解决-优化listView卡顿和怎样禁用ListView的fling 前戏非常长,转载请保留出处:http://blog.csdn.net/u012123160/article/details/4 ...

  5. 解决Keystore was tampered with, or password was incorrect

    使用签名文件keystore查看生成的数字签名中报错解决 Keystore was tampered with, or password was incorrect 这是由于android规定自己定义 ...

  6. (转)Windows Server 2012 R2虚拟机自激活(AVMA)技术

    转自: 老丁的技术博客 相信Hyper-v管理员都有这样的经历,安装多台虚拟机后,都要一台一台手工激活,如果虚拟机足够多的话,这是一项很繁琐的工作,但从Windows Server 2012 R2开始 ...

  7. 2、Python基本数据类型

    1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即- ...

  8. 超链接a的download属性 实现文件下载功能

    今天做项目遇到一个要点击按钮下载文件的功能. 百度之 知道了a的download属性.这是HTML5的新特性.主要功能是实现下载功能.主要语法是 <a href="url" ...

  9. [AngularFire2] Auth with Firebase auth -- email

    First, you need to enable the email auth in Firebase console. Then implement the auth service: login ...

  10. java 封装解析 Json数据。

    import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; im ...