`
yimeng528
  • 浏览: 191265 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Quartz之QuartzInitializerListener

阅读更多

问题:我想在WEB容器启动时就执行任务怎么办呢 
Quartz:使用QuartzInitializerListener就可办到了
 
请注意它的优先级别比QuartzInitializerServlet要高 
在web.xml中可配置的参数如下: 
如: 

Java代码   收藏代码
  1. <context-param>  
  2.          <param-name>quartz:config-file</param-name>  
  3.          <param-value>/quartz.properties</param-value>  
  4. </context-param>  


以下二者参数可代表都是同一个意思 
quartz:config-file                                                       或者 config-file 
quartz:shutdown-on-unload                                       或者  shutdown-on-unload 
quartz:wait-on-shutdown 
quartz:start-on-load                                                  或者   start-scheduler-on-load 
quartz:start-delay-seconds                                        或者   start-delay-seconds 
quartz:servlet-context-factory-key                              或者   servlet-context-factory-key 
默认值为:org.quartz.impl.StdSchedulerFactory.KEY 
quartz:scheduler-context-servlet-context-key              或者 scheduler-context-servlet-context-key
 
以上参数都是根据QuartzInitializerListener源码得来的 
QuartzInitializerListener源码如下: 

Java代码   收藏代码
  1. package org.quartz.ee.servlet;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletContextEvent;  
  5. import javax.servlet.ServletContextListener;  
  6. import org.quartz.Scheduler;  
  7. import org.quartz.impl.StdSchedulerFactory;  
  8. import org.slf4j.Logger;  
  9. import org.slf4j.LoggerFactory;  
  10.   
  11. public class QuartzInitializerListener implements ServletContextListener {  
  12.       
  13.     public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";  
  14.     private boolean performShutdown;  
  15.     private boolean waitOnShutdown;  
  16.     private Scheduler scheduler;  
  17.     private final Logger log;  
  18.   
  19.     public QuartzInitializerListener() {  
  20.         this.performShutdown = true;  
  21.         this.waitOnShutdown = false;  
  22.   
  23.         this.scheduler = null;  
  24.   
  25.         this.log = LoggerFactory.getLogger(super.getClass());  
  26.     }  
  27.   
  28.     public void contextInitialized(ServletContextEvent sce) {  
  29.         this.log  
  30.                 .info("Quartz Initializer Servlet loaded, initializing Scheduler...");  
  31.   
  32.         ServletContext servletContext = sce.getServletContext();  
  33.         try {  
  34.             String configFile = servletContext  
  35.                     .getInitParameter("quartz:config-file");  
  36.             if (configFile == null)  
  37.                 configFile = servletContext.getInitParameter("config-file");  
  38.             String shutdownPref = servletContext  
  39.                     .getInitParameter("quartz:shutdown-on-unload");  
  40.             if (shutdownPref == null)  
  41.                 shutdownPref = servletContext  
  42.                         .getInitParameter("shutdown-on-unload");  
  43.             if (shutdownPref != null) {  
  44.                 this.performShutdown = Boolean.valueOf(shutdownPref)  
  45.                         .booleanValue();  
  46.             }  
  47.             String shutdownWaitPref = servletContext  
  48.                     .getInitParameter("quartz:wait-on-shutdown");  
  49.             if (shutdownPref != null)  
  50.                 this.waitOnShutdown = Boolean.valueOf(shutdownWaitPref)  
  51.                         .booleanValue();  
  52.   
  53.             StdSchedulerFactory factory;  
  54.   
  55.             if (configFile != null)  
  56.                 factory = new StdSchedulerFactory(configFile);  
  57.             else {  
  58.                 factory = new StdSchedulerFactory();  
  59.             }  
  60.   
  61.             this.scheduler = factory.getScheduler();  
  62.   
  63.             String startOnLoad = servletContext  
  64.                     .getInitParameter("quartz:start-on-load");  
  65.             if (startOnLoad == null) {  
  66.                 startOnLoad = servletContext  
  67.                         .getInitParameter("start-scheduler-on-load");  
  68.             }  
  69.             int startDelay = 0;  
  70.             String startDelayS = servletContext  
  71.                     .getInitParameter("quartz:start-delay-seconds");  
  72.             if (startDelayS == null)  
  73.                 startDelayS = servletContext  
  74.                         .getInitParameter("start-delay-seconds");  
  75.             try {  
  76.                 if ((startDelayS != null) && (startDelayS.trim().length() > 0))  
  77.                     startDelay = Integer.parseInt(startDelayS);  
  78.             } catch (Exception e) {  
  79.                 this.log  
  80.                         .error("Cannot parse value of 'start-delay-seconds' to an integer: "  
  81.                                 + startDelayS + ", defaulting to 5 seconds.");  
  82.                 startDelay = 5;  
  83.             }  
  84.   
  85.             if ((startOnLoad == null)  
  86.                     || (Boolean.valueOf(startOnLoad).booleanValue()))  
  87.                 if (startDelay <= 0) {  
  88.                     this.scheduler.start();  
  89.                     this.log.info("Scheduler has been started...");  
  90.                 } else {  
  91.                     this.scheduler.startDelayed(startDelay);  
  92.                     this.log.info("Scheduler will start in " + startDelay  
  93.                             + " seconds.");  
  94.                 }  
  95.             else {  
  96.                 this.log  
  97.                         .info("Scheduler has not been started. Use scheduler.start()");  
  98.             }  
  99.   
  100.             String factoryKey = servletContext  
  101.                     .getInitParameter("quartz:servlet-context-factory-key");  
  102.             if (factoryKey == null)  
  103.                 factoryKey = servletContext  
  104.                         .getInitParameter("servlet-context-factory-key");  
  105.             if (factoryKey == null) {  
  106.                 factoryKey = "org.quartz.impl.StdSchedulerFactory.KEY";  
  107.             }  
  108.   
  109.             this.log  
  110.                     .info("Storing the Quartz Scheduler Factory in the servlet context at key: "  
  111.                             + factoryKey);  
  112.   
  113.             servletContext.setAttribute(factoryKey, factory);  
  114.   
  115.             String servletCtxtKey = servletContext  
  116.                     .getInitParameter("quartz:scheduler-context-servlet-context-key");  
  117.             if (servletCtxtKey == null)  
  118.                 servletCtxtKey = servletContext  
  119.                         .getInitParameter("scheduler-context-servlet-context-key");  
  120.             if (servletCtxtKey != null) {  
  121.                 this.log  
  122.                         .info("Storing the ServletContext in the scheduler context at key: "  
  123.                                 + servletCtxtKey);  
  124.   
  125.                 this.scheduler.getContext().put(servletCtxtKey, servletContext);  
  126.             }  
  127.         } catch (Exception e) {  
  128.             this.log.error("Quartz Scheduler failed to initialize: "  
  129.                     + e.toString());  
  130.             e.printStackTrace();  
  131.         }  
  132.     }  
  133.   
  134.     public void contextDestroyed(ServletContextEvent sce) {  
  135.         if (!this.performShutdown) {  
  136.             return;  
  137.         }  
  138.         try {  
  139.             if (this.scheduler != null)  
  140.                 this.scheduler.shutdown(this.waitOnShutdown);  
  141.         } catch (Exception e) {  
  142.             this.log.error("Quartz Scheduler failed to shutdown cleanly: "  
  143.                     + e.toString());  
  144.             e.printStackTrace();  
  145.         }  
  146.   
  147.         this.log.info("Quartz Scheduler successful shutdown.");  
  148.     }  
  149. }  
分享到:
评论

相关推荐

    使用Quartz实现定时功能

    1. **Job(作业)**:这是Quartz中的核心概念之一,它代表了要执行的任务。一个`Job`实例必须实现`org.quartz.Job`接口,并且重写`execute`方法来定义具体的任务逻辑。如果任务需要保存状态,则需要实现`StatefulJob...

    quartz1.8 范例

    1. **Web初始化**:可能有一个名为`QuartzInitializerListener`的监听器,它在Web应用启动时被调用,负责设置Scheduler,并将Job和Trigger绑定到Web环境。 2. **Servlet或Filter**:可能包含一个Servlet或Filter,...

    Quartz-Job-Scheduling-Framework-中文版-V0.9.1.zip

    内容提要:借助于 QuartzInitializerServlet 或 QuartzInitializerListener 在 J2EE 容器上运行 Quartz,并使用容器的相关资源。 第十一章. Quartz 集群 (第一部分) 内容提要:Quartz 应用也能进行集群。及 Quartz ...

    kernel-devel-4.18.0-553.45.1.el8-10.x86-64.rpm

    Rocky Linux 8.10内核包

    Simulink中三阶单环多位量化Σ-Δ调制器的设计与实现-音频带ADC的应用(复现论文或解答问题,含详细可运行代码及解释)

    内容概要:本文档详细介绍了如何在Simulink中设计一个满足特定规格的音频带ADC(模数转换器)。首先选择了三阶单环多位量化Σ-Δ调制器作为设计方案,因为这种结构能在音频带宽内提供高噪声整形效果,并且多位量化可以降低量化噪声。接着,文档展示了具体的Simulink建模步骤,包括创建模型、添加各个组件如积分器、量化器、DAC反馈以及连接它们。此外,还进行了参数设计与计算,特别是过采样率和信噪比的估算,并引入了动态元件匹配技术来减少DAC的非线性误差。性能验证部分则通过理想和非理想的仿真实验评估了系统的稳定性和各项指标,最终证明所设计的ADC能够达到预期的技术标准。 适用人群:电子工程专业学生、从事数据转换器研究或开发的技术人员。 使用场景及目标:适用于希望深入了解Σ-Δ调制器的工作原理及其在音频带ADC应用中的具体实现方法的人群。目标是掌握如何利用MATLAB/Simulink工具进行复杂电路的设计与仿真。 其他说明:文中提供了详细的Matlab代码片段用于指导读者完成整个设计流程,同时附带了一些辅助函数帮助分析仿真结果。

    计算机课后习题.docx### 【计算机科学】研究生入学考试计算机组成原理专项题库设计:考研复习资源集成与优化

    内容概要:该题库专为研究生入学考试计算机组成原理科目设计,涵盖名校考研真题、经典教材课后习题、章节题库和模拟试题四大核心模块。名校考研真题精选多所知名高校的计算机组成原理科目及计算机联考真题,并提供详尽解析,帮助考生把握考研命题趋势与难度。经典教材课后习题包括白中英《计算机组成原理》(第5版)和唐朔飞《计算机组成原理》(第2版)的全部课后习题解答,这两部教材被众多名校列为考研指定参考书目。章节题库精选代表性考题,注重基础知识与重难点内容,帮助考生全面掌握考试大纲要求的知识点。模拟试题依据历年考研真题命题规律和热门考点,精心编制两套全真模拟试题,并附标准答案,帮助考生检验学习成果,评估应试能力。 适用人群:计划参加研究生入学考试并报考计算机组成原理科目的考生,尤其是需要系统复习和强化训练的学生。 使用场景及目标:①通过研读名校考研真题,考生可以准确把握考研命题趋势与难度,有效评估复习成效;②通过经典教材课后习题的练习,考生可以巩固基础知识,掌握解题技巧;③通过章节题库的系统练习,考生可以全面掌握考试大纲要求的各个知识点,为备考打下坚实基础;④通过模拟试题的测试,考生可以检验学习成果,评估应试能力,为正式考试做好充分准备。 其他说明:该题库不仅提供详细的题目解析,还涵盖了计算机组成原理的各个方面,包括计算机系统概述、数据表示与运算、存储器分层、指令系统、中央处理器、总线系统和输入输出系统等。考生在使用过程中应结合理论学习与实践操作,注重理解与应用,以提高应试能力和专业知识水平。

    __UNI__DB9970A__20250328141034.apk.1

    __UNI__DB9970A__20250328141034.apk.1

    minio-rsc-Rust资源

    rust for minio

    4-4-台区智能融合终端功能模块型式规范(试行).pdf

    国网台区终端最新规范

    《基于YOLOv8的化工管道焊缝缺陷检测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计.zip

    资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。

    python源码-1个机器学习相关资源

    一个简单的机器学习代码示例,使用的是经典的鸢尾花(Iris)数据集,通过 Scikit-learn 库实现了一个简单的分类模型。这个代码可以帮助你入门机器学习中的分类任务。

    pyqt离线包,pyqt-tools离线包

    pyqt离线包,pyqt-tools离线包

    《基于YOLOv8的船舶机舱灭火系统状态监测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计.zip

    资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。

    SQL常用日期和时间函数整理及使用示例

    SQL常用日期和时间函数整理及在sqlserver测试示例 主要包括 1.查询当前日期GETDATE 2.日期时间加减函数DATEADD 3 返回两个日期中指定的日期部分之间的差值DATEDIFF 4.日期格式转换CONVERT(VARCHAR(10),GETDATE(),120) 5.返回指定日期的年份数值 6.返回指定日期的月份数值 7.返回指定日期的天数数值

    GSDML-V2.3-Turck-BL20-E-GW-EN-20160524-010300.xml

    GSDML-V2.3-Turck-BL20_E_GW_EN-20160524-010300.xml

    T_CPCIF 0225-2022 多聚甲醛.docx

    T_CPCIF 0225-2022 多聚甲醛.docx

    《基于YOLOv8的智能仓储货物堆码倾斜预警系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计.zip

    《基于YOLOv8的智能仓储货物堆码倾斜预警系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计

    蚕豆脱壳机设计.zip

    蚕豆脱壳机设计.zip

    附件2-2:台区智能融合终端入网专业检测单位授权委托书.docx

    台区终端电科院送检文档

    Y6一39一No23.6D离心通风机 CAD().zip

    Y6一39一No23.6D离心通风机 CAD().zip

Global site tag (gtag.js) - Google Analytics