`

quartz配置文件详解

    博客分类:
  • JAVA
阅读更多
quartz学习总结:
一、关于job:
   用Quartz的行话讲,作业是一个执行任务的简单Java类。任务可以是任何Java代码。只需你实现org.quartz.Job接口并且在出现严重 错误情况下抛出JobExecutionException异常即可。Job接口包含唯一的一个方法execute(),作业从这里开始执行。一旦实现了 Job接口和execute()方法,当Quartz确定该是作业运行的时候,它将调用你的作业。Execute()方法内就完全是你要做的事情。要注 意,自己实现job时必须有一个public 的无参数的构造方法.对于job,大多数情况下都要依赖于某些具体的条件,这时,就要用到JobDataMap了。JobDataMap是Map的一个子 类,获取时很简单,直接用get方法就ok了,基于参数,我们就可以定制不同的job任务了。下面是一个简单的job,用来列举出所有的参数并且获得参数 名为name的值.
   public class HelloJob implements Job {
private static Log _log = LogFactory.getLog(HelloJob.class);
public HelloJob() {
}
public void execute(JobExecutionContext context)
   throws JobExecutionException {
  JobDataMap jobDataMap =  context.getJobDetail().getJobDataMap();
  _log.info("要执行的参数如下:");
  Iterator i = jobDataMap.entrySet().iterator(); 
  while(i.hasNext()) {
  Map.Entry me = (Map.Entry)i.next();
  _log.info(me.getKey() + ": "+me.getValue());
  } 
  _log.info("U Are Welcome:"+jobDataMap.get("name"));
}
     }

二、关于jobdetail:
    Quartz并不存储一个真正的Job实例,相反的,它通过jobdetail来定义job,并指定job的name和group,在一个调度器 (Scheduler)中,name和group是唯一被定义的,一个触发器(trigger)只能指定一个job,但多个触发器可以指定同一个job.

    Scheduler的作用就是调用任务,在指定的时间执行指定的任务。主要方法如下:
    scheduleJob方法:
    scheduleJob(JobDetail jobDetail, Trigger trigger):把jobDetail添加到调度器中,并指定触发器trigger.在这里要注意,在同一个调度器中,jobDetail的name和 group是唯一的;Trigger的name和group也必须是唯一的。如果在trigger中指定job的name,则该name必须和 jobDetail的name保持一致,否则会抛出异常。
    scheduleJob(Trigger trigger):指定的trigger中必须包含jobdetai的name.以便于让quartz知道要执行的任务,如果指定的jobdetail的 name不在调度器中的任务列表中,则会抛出JobPersistenceException异常。
    deleteJob(String jobName,String groupName)方法:
    删除指定的job,并且删除所有相关联的触发器。(Delete the identified Job from the Scheduler - and any associated Triggers.)
四、关于作业存储
    Quartz提供两种基本作业存储类型。
  
    第一种类型叫做RAMJobStore,它利用通常的内存来持久化调度程序信息。这种作业存储类型最容易配置、构造和运行。对许多应用来说,这种作业存储 已经足够了。然而,因为调度程序信息是存储在被分配给JVM的内存里面,所以,当应用程序停止运行时,所有调度信息将被丢失。
  
    第二种类型称为JDBC作业存储。Quartz提供两种不同的实现,但两种实现一般都需要JDBC驱动程序和后台数据库来持久化调度程序信息。这两种类型 的不同在于你是否想要控制数据库事务或这释放控制给应用服务器例如BEA's WebLogic或Jboss。(这类似于J2EE领域中,Bean管理的事务和和容器管理事务之间的区别)这两种JDBC作业存储是:
    · JobStoreTX:当你想要控制事务或工作在非应用服务器环境中是使用 (注:自己控制事务)。
    · JobStoreCMT:当你工作在应用服务器环境中和想要容器控制事务时使用 (web服务器控制事务)。
五、关于触发器
      Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器类型,但两个最常用的是SimpleTrigger和CronTrigger。SimpleTrigger为需要简单打火调度而设计。
      典型地,如果你需要在给定的时间和重复次数或者两次打火之间等待的秒数打火一个作业,那么SimpleTrigger适合你。
    
      另一方面,如果你有许多复杂的作业调度,那么或许需要CronTrigger。
   
      CronTrigger很强大,使用复杂的时间判断来使用,效果很好。
六、关于Quartz中的几个表:
    QRTZ_TRIGGERS                   存放Trigger(包括SIMPLE_TRIGGERS和CRON_TRIGGERS)和jobDetail的对应关系
    QRTZ_TRIGGER_LISTENERS
    QRTZ_SIMPLE_TRIGGERS            存储简单触发器
    QRTZ_SCHEDULER_STATE
    QRTZ_PAUSED_TRIGGER_GRPS
    QRTZ_LOCKS
    QRTZ_JOB_LISTENERS
    QRTZ_JOB_DETAILS                 存储jobDetail
    QRTZ_FIRED_TRIGGERS
    QRTZ_CRON_TRIGGERS               存储复杂触发器CRON_TRIGGERS
    QRTZ_CALENDARS
    QRTZ_BLOB_TRIGGERS 

七、把quartz集成到web应用中
    1、根据quartz中提供的建表文档,建立数据库.
    2、把如下quartz.properties文件放置到classes目录下.文件内容如下:
  
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
      
#调度器名,无关紧要,名字任意定
org.quartz.scheduler.instanceName = ZXScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool   配置数据库连接池
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 12
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore  配置做业存储方式
#============================================================================
      
#相当于扫描频率,如果系统基于秒级,应培植成1000,quartz默认为分级(60000)
org.quartz.jobStore.misfireThreshold = 1000
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#如果设置内存,就不要设置clusterCheckinInterval等属性     
#在这里自己控制事务
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties = false
#配置dataSource名
org.quartz.jobStore.dataSource = myDS
#表前缀
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
#============================================================================
# Configure Datasources  配置数据库的连接,不用解释
#============================================================================
org.quartz.dataSource.myDS.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=quartzTest
org.quartz.dataSource.myDS.user = sa
org.quartz.dataSource.myDS.password = sa
org.quartz.dataSource.myDS.maxConnections = 5
    3、配置web.xml,启动quartz的初试化类,添加初始化servlet
   <servlet>
    <servlet-name>QuartzInitializer</servlet-name>
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
    <init-param>
      <param-name>shutdown-on-unload</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
     <param-name>config-file</param-name>
     <param-value>quartz.properties</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>  

     4、系统配置完毕。
八、构造cron触发器(个人翻译,英文不好,莫见笑)
    cron 触发器规范:
    Seconds Minutes Hours Day-of-month Month Day-of-Week Year
    秒       分      时     天           月    周          年 
  
    Seconds            0-59    , - * /
    Minutes            0-59    , - * /
    Hours              0-23    , - * /
    Day-of-month       1-31    , - * ? / L C
    Month              1-12 or JAN-DEC    , - * /
    Day-of-Week        1-7 or SUN-SAT    , - * ? / L C #
    Year (Optional)    empty, 1970-2099    , - * /
    关于字符串的设置(在cron expression中所有字符不区分大小写):

    The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
    "*"字符被用来指定所有的值,例如,"*"在分钟字段时表示每一分钟
    The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
    "?"字符在天和周字段中使用。表示没有指定值,天和周字段指定一个,但不能两个都指定为"?"
    The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
    '-'字符被用来设置范围,比如"10-12"在小时字段的意义为"10点,11点,12点"
    The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
    ','字符被用来设置添加的值,例如在周字段设置"MON,WED,FRI"的意义即为:在周一、周三、周五激活
    The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". You can also specify '/' after the '*' character - in this case '*' is equivalent to having '0' before the '/'.
    '/'字符被用来设置增量。例如秒字段设置"0/15"的意思为从0开始,每15秒触发,即在0、15、30、45秒触发,秒字段设置"5/15"的意思 为,从5开始,第15秒触发,即在5、20、35、50秒触发.你还可以在'*'后面使用'/'字符,在这种情况下'*'与字符'0'意义相同。

    The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.
    '#'被用在周字段。它用来指定第几个周几中激活。如:"6#3"-->月的第三个周五;"2#1"-->月的第一个周一;"4#5"-- >月的第五个周三。要注意,如果要使用#后面跟5,但当月并没有第五周相应的周天,那么job将不被执行(激活);
    The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".
    Support for the features described for the 'C' character is not complete
    'C'被用在天和周字段中,'C'是'calendar'的缩写.(不太明白,关于日历的支持还不完善)
    Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
    同时在周、天中使用'?'还不完善,目前只在两者中使用一个。
    Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!
    要注意'?'和'*'在周和天字段带来的影响。
    注意以下例子:
    1、"0 15 10 * * 6L 2002-2005"   在2002至2005年的每月每天的10:15触发。
    2、"0 15 10 ? * 6L 2002-2005"   在2002至2005年的每月的最后一个周五触发。
    1中*表示每天,覆盖了6L(最后一个周五)
分享到:
评论

相关推荐

    完整quartz配置大全

    OpenSymphony 的Quartz提供了一个比较完美的任务调度解决方案。  Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。  Quartz中有两个基本概念:作业和触发器。作业是...

    Quartz.NET 配置文件详解1

    1. 首先,说说 quartz.config 文件1) 先将上一篇中的配置贴出来# You can configure your scheduler i

    springboot整合quartz定时任务yml文件配置方式

    spring-cloud-alibaba+dubbo+nacos+quartz以yml配置方式实现

    spring jar 包详解

    (2) spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,...

    Spring.3.x企业应用开发实战(完整版).part2

    2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在Spring中装配DAO 2.4 业务层 2.4.1 UserService 2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 ...

    Spring3.x企业应用开发实战(完整版) part1

    2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在Spring中装配DAO 2.4 业务层 2.4.1 UserService 2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 ...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及...

    Spring-Reference_zh_CN(Spring中文参考手册)

    配置子报表文件 14.7.4.2. 配置子报表数据源 14.7.5. 配置Exporter的参数 15. 集成其它Web框架 15.1. 简介 15.2. 通用配置 15.3. JavaServer Faces 15.3.1. DelegatingVariableResolver 15.3.2. FacesContextUtils ...

    Spring中文帮助文档

    3.3.2. 依赖配置详解 3.3.3. 使用depends-on 3.3.4. 延迟初始化bean 3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用...

    Spring API

    3.3.2. 依赖配置详解 3.3.3. 使用depends-on 3.3.4. 延迟初始化bean 3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用...

    spring chm文档

    3.3.3. bean属性及构造器参数详解 3.3.4. 使用depends-on 3.3.5. 延迟初始化bean 3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. ...

    Spring 2.0 开发参考手册

    3.3.3. bean属性及构造器参数详解 3.3.4. 使用depends-on 3.3.5. 延迟初始化bean 3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. ...

Global site tag (gtag.js) - Google Analytics