Quartz入门

简介:

    Quartz是一个完全由Java编写的开源作业调度框架,当你想实现定时做些事情的时候,它就派上用场啦!目前Quartz比较稳定的版本是2.2.1,所以我这里就以这个版本为例,如果你使用Quartz2.x系列,那你的spring版本必须3.1版本及以上(假如你需要将Quartz跟Spring整合的话),Quartz并不一定需要跟Spring整合哈,它完全可以脱离Spring单独工作,只是Spring目前太流行,所以大家都喜欢将其他框架往Spring里整合。

      下面是Quartz的一个入门级别的简单示例:

       applicationContext-quartz.xml配置如下:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  7.              http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     
  8.              http://www.springframework.org/schema/context      
  9.              http://www.springframework.org/schema/context/spring-context-3.1.xsd     
  10.              http://www.springframework.org/schema/aop      
  11.              http://www.springframework.org/schema/aop/spring-aop-3.1.xsd     
  12.              http://www.springframework.org/schema/tx      
  13.              http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
  14.              http://www.springframework.org/schema/mvc      
  15.              http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd     
  16.              http://www.springframework.org/schema/context      
  17.              http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
  18.     default-autowire="byName" default-lazy-init="true">  
  19.   
  20.     <bean id="taskJob" class="com.yida.framework.test.FirstJob" />  
  21.     <bean id="jobDetail"  
  22.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  23.         <property name="group" value="job_work" />  
  24.         <property name="name" value="job_work_name" />  
  25.         <!--false表示等上一个任务执行完后再开启新的任务 -->  
  26.         <property name="concurrent" value="false" />  
  27.         <property name="targetObject">  
  28.             <ref bean="taskJob" />  
  29.         </property>  
  30.         <property name="targetMethod">  
  31.             <value>run</value>  
  32.         </property>  
  33.     </bean>  
  34.     <!-- 调度触发器 -->  
  35.     <bean id="myTrigger"  
  36.         class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
  37.         <property name="name" value="work_default_name" />  
  38.         <property name="group" value="work_default" />  
  39.         <property name="jobDetail">  
  40.             <ref bean="jobDetail" />  
  41.         </property>  
  42.         <property name="cronExpression">  
  43.             <value>0/3 * * * * ?</value>  
  44.         </property>  
  45.     </bean>  
  46.     <!-- 调度工厂 -->  
  47.     <bean id="scheduler" autowire="no"  
  48.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  49.         <property name="triggers">  
  50.             <list>  
  51.                 <ref bean="myTrigger" />  
  52.             </list>  
  53.         </property>  
  54.     </bean>  
  55. </beans>  

   

    web.xml里配置加载spring的相关配置文件:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  4.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  5.       
  6.     <context-param>  
  7.         <param-name>contextConfigLocation</param-name>  
  8.         <param-value>  
  9.             classpath:/com/yida/framework/base/config/spring/application*.xml  
  10.         </param-value>  
  11.     </context-param>  

    编写一个简单的任务测试类:

Java代码   收藏代码
  1. package com.yida.framework.test;  
  2.   
  3. public class FirstJob {  
  4.     public void run(String[] args) {  
  5.         System.out.println("我的第一个测试任务。");  
  6.     }  
  7. }  

    最后部署你的项目,启动tomcat,任务就会自动执行啦!

转载:http://iamyida.iteye.com/blog/2263133

目录
相关文章
|
8月前
|
Java 调度
quartz(一)基础篇
quartz(一)基础篇
40 0
|
11月前
|
开发框架 Java Linux
Quartz-任务调度概述及Quartz(2.2.X)快速入门
Quartz-任务调度概述及Quartz(2.2.X)快速入门
102 0
quartz学习笔记7:trading
quartz学习笔记7:trading
50 0
Quartz - 入门案例
Quartz - 入门案例
63 0
Quartz - 基础篇(上)
Quartz - 基础篇(上)
92 0
Quartz - 基础篇(上)
|
Java 应用服务中间件
Quartz - 基础篇(下)
Quartz - 基础篇(下)
135 0
Quartz - 基础篇(下)