Spring4+Springmvc+quartz实现多线程动态定时调度

scheduler定时调度系统是大多行业项目都需要的,传统的spring-job模式,个人感觉已经out了,因为存在很多的问题,特别是定时调度的追加、修改、删除等,需要修改xml,xml的配置生效无非是热部署灰度发布方案或者直接停止、重启服务器,完全不能做到自动启动、修复方式。

提醒:可以对应用进行集群部署,在对定时调度配置时可以使用集群方式或者单边配置应用方式,今天讲解的是使用spring4+scheduler实现定时调度,闲话少说,直接把步骤记录下来:

 

1. 在项目的pom.xml文件中引入quartz的jar包,如下:

Java代码   收藏代码
  1.              <!-- quartz定时调度 -->  
  2. lt;dependency>  
  3. <groupId>org.quartz-scheduler</groupId>  
  4. <artifactId>quartz</artifactId>  
  5. <version>1.8.5</version>  
  6. lt;/dependency>  

 

2. 定义quartz的配置文件spring-context-quartz.xml:

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="  
  4.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  5.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"  
  6.     default-lazy-init="false">  
  7.     <!-- 调度器 -->  
  8.     <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   
  9.        <!-- 通过applicationContextSchedulerContextKey属性配置spring上下文 -->      
  10.         <property name="applicationContextSchedulerContextKey" value="applicationContext" />  
  11.     </bean>    
  12.     <!--加载数据库任务-->  
  13.     <bean id="jobService" class="com.ml.honghu.job.service.JobService" init-method="loadJob" />  
  14. </beans>  

  

3.  在项目的web.xml文件中引入spring-context-quartz.xml配置文件

Java代码   收藏代码
  1. classpath*:spring-context-quartz.xml  

 

4. 定义job实体对象

Java代码   收藏代码
  1. public class Job{  
  2.       
  3.     private static final long serialVersionUID = 1L;  
  4.       
  5.     /** 
  6.      * 任务执行周期cron表达式 
  7.      */  
  8.     public static int EXECYCLE_CRON = 2;  
  9.     /** 
  10.      * 任务执行周期自定义 
  11.      */  
  12.     public static int EXECYCLE_DEFINE = 1;  
  13.     /** 
  14.      * 执行周期-分钟 
  15.      */  
  16.     public static int EXECYCLE_MINUTE = 1;  
  17.     /** 
  18.      * 执行周期-小时 
  19.      */  
  20.     public static int EXECYCLE_HOUR = 2;  
  21.     /** 
  22.      * 执行周期-日 
  23.      */  
  24.     public static int EXECYCLE_DAY = 3;  
  25.     /** 
  26.      * 执行周期-月 
  27.      */  
  28.     public static int EXECYCLE_WEEK = 4;  
  29.     /** 
  30.      * 执行周期-月 
  31.      */  
  32.     public static int EXECYCLE_MONTH = 5;  
  33.       
  34.   
  35.     private String jobType;     // 任务类型(1首页静态化、2栏目页静态化、3内容页静态化、4采集、5分发)  
  36.     private String jobName;     // 任务名称  
  37.     private String jobClass;        // 任务类  
  38.     private String execycle;        // 执行周期分类(1非表达式 2 cron表达式)  
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值