任务调度 Quartz 尽量做到可配置?

下面想总结下项目中完全只使用 任务调度Quartz来 组织定时任务 :

 

1.首先我们需要Quartz的jar包 ,见附件

2.我们来创建一个 ServletContextListener  实现这个接口的一个监听器,主要用来启动任务调度

 

public class SchedulerListener implements ServletContextListener {

 public void contextInitialized (ServletContextEvent sce) {

   //TODO : something

}

public void contextDestroyed (ServletContextEvent sce) {

 //TODO : something

}

 

  在web.xml配置这个监听器

 

<context-param>
         <param-name>scheduler-config-file</param-name>
         <param-value>/config/quartz.properties</param-value>
     </context-param>
<listener>
         <listener-class>
             com.test.quartz.SchedulerListener
         </listener-class>
     </listener>

 

3. 属性文件的编写  quartz.properties

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
org.quartz.scheduler.instanceName = DefaultQuartzScheduler

org.quartz.scheduler.rmi.export = false

org.quartz.scheduler.rmi.proxy = false

org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount = 10

org.quartz.threadPool.threadPriority = 5

org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

#============================================================================
# Configure Plugins 
#============================================================================

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = /config/quartz-jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

 

由于具体的东西了解不是很深,所有没有注释这些配置是做什么的 ,以后补充 

 可以参考下面的超链 好像讲了很多 quartz.properties配置说明  http://kdisk-sina-com.iteye.com/blog/436109 

 

4.quartz-jobs.xml  的配置

 

<?xml version='1.0' encoding='utf-8'?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData
  http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd"
  version="1.5">
  
  <job>
    <job-detail>
      <name>myJob</name>
      <group>myJobs</group>
      <job-class>com.test.quartz.MyJob</job-class>
    </job-detail>
    <trigger>
      <cron>
        <name>myTrigger</name>
        <group>testTriggers</group>
        <job-name>myJob</job-name>
        <job-group>test</job-group>
        <cron-expression>0 0 2 * * ?</cron-expression>
        <!-- Fire at 02:00 am every day -->
      </cron>
    </trigger>
  </job>

</quartz>

 
配置说明 : 其中 com.test.quartz.MyJob 类是我们需要自己实现自己需要的东西

 

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job {

     public void execute (JobExecutionContext jobCtx) throws JobExecutionException {
        
     //TODO :something  
   }
}

 

这里,我们可以把多个相同性质的任务 都放在execute方法里面 . 例如 有多个不同的任务都是要在 下午3点定时执行的

话,我们就都把他放在这里执行,

 

 

在上面的 quartz-jobs.xml  的配置中 ,我们可以配置多个 job节点 ,完成我们对不同任务的调度时间的不同需求

 

这样就达到可以配置的目的了

 

 

 

 

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值