在Spring框架中集成Quartz实现任务调度

  在各种企业应用中经常遇到任务调度的需求,Sun JDK 本身提供的java.util.TimerTimerTask提供了简单的调度功能,允许用户按照一个固定的时间间隔,周期运行任务。Quartz是开源任务调度运用最为广泛的框架,它提供了强大的任务调度机制,允许开发人员灵活地定义触发器的调度时间表,对触发器和任务进行关联映射,并且Quartz提供了调度运行环境的持久化机制,可以保存并恢复调度现场。

 

  1Quartz中的一些核心概念

  Job:一接口,只有一个void excute (JobExecutionContext context) 方法,开发者可以通过实现该接口来定义运行任务,context提供了上下文的各种信息。

  JobDetail:接收一个Job实现类,在运行时通过newInstance()的反射机制实例化Job

  Trigger:描述触发Job执行的时间触发规则。有SimpleTriggerCronTrigger两个子类,其中CronTrigger定义更为灵活,通过Cron表达式定义各种复杂时间规则的调度方案。

  Scheduler:代表一个Quartz的独立运行容器,TriggerJobDetailScheduler中注册,两者在Scheduler中拥有各自的组与名称,Trigger的组和名称必须唯一,JobDetail的组和名称也必须唯一。

 

  2)在Spring中集成Quartz

  Spring为创建QuartzSchedulerTriggerJobDetail提供了便利的FactoryBean类以便在Spring容器中使用依赖注入的优点,此外Spring提供工具类可以直接将Spring中的Bean包装成合法的任务(Spring提供了一个MethodInvokingJobDetailFactoryBean,通过这个FactoryBean类可以将Spring容器中的Bean包装成Quartz任务),Spring进一步降低了Quartz的使用难度。

 

  3)一个简单的Demo

  a. 新建一Web项目,带入所需的Quartz包和Spring包,项目目录如图-1所示:

 

           图-1

  b. 配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  5.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6.     <context-param>
  7.         <param-name>contextConfigLocation</param-name>
  8.         <param-value>classpath:/applicationContext.xml</param-value>
  9.     </context-param>
  10.     
  11.     <listener>
  12.         <listener-class>
  13.             org.springframework.web.context.ContextLoaderListener
  14.         </listener-class>
  15.     </listener>
  16.     <listener>
  17.         <listener-class>
  18.             org.springframework.web.util.IntrospectorCleanupListener
  19.         </listener-class>
  20.     </listener>
  21.     <listener>
  22.         <listener-class>
  23.             org.springframework.web.context.request.RequestContextListener
  24.         </listener-class>
  25.     </listener>
  26.     
  27.     <welcome-file-list>
  28.         <welcome-file>index.jsp</welcome-file>
  29.     </welcome-file-list>
  30. </web-app>

  c. 配置applicationContext.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"
  4.        xmlns:jee="http://www.springframework.org/schema/jee"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6.            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
  7.     
  8.     <bean id="MyJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
  9.         <property name="jobClass" value="com.demo.quartz.MyJob"/>
  10.         <property name="jobDataAsMap">
  11.             <map>
  12.                 <entry key="size" value="10"></entry>
  13.             </map>
  14.         </property>
  15.         <property name="applicationContextJobDataKey" value="applicationContext"/>
  16.     </bean>
  17.     
  18.     <bean id="MyJobScheduledTask" class="org.springframework.scheduling.quartz.CronTriggerBean">
  19.         <property name="jobDetail" ref="MyJobDetail"/>
  20.         <property name="cronExpression" value="0 19 10 6 * ? *"></property>
  21.     </bean>
  22.     
  23.     <bean id="HiJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
  24.         <property name="jobClass" value="com.demo.quartz.HiJob"/>
  25.         <property name="jobDataAsMap">
  26.             <map>
  27.                 <entry key="size" value="10"></entry>
  28.             </map>
  29.         </property>
  30.         <property name="applicationContextJobDataKey" value="applicationContext"/>
  31.     </bean>
  32.     
  33.     <bean id="HiJobScheduledTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  34.         <property name="startDelay" value="10000"/>
  35.         <property name="repeatInterval" value="2000"/>
  36.         <property name="jobDetail" ref="HiJobDetail"/>
  37.     </bean>
  38.     
  39.     <bean id="QuartzJobFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  40.         <property name="triggers">
  41.             <list>
  42.                 <ref bean="MyJobScheduledTask"/>
  43.                 <!--  <ref bean="HiJobScheduledTask"/> -->
  44.             </list>
  45.         </property>
  46.         <!-- 设置是否Spring容器初始化后马上启动Scheduler,默认为true。如果设置为false则需要手工启动Scheduler -->
  47.         <property name="autoStartup" value="true"/>
  48.     </bean>
  49.     
  50. </beans>

  d. HiJob

  1. package com.demo.quartz;
  2. import java.util.Date;
  3. import org.quartz.Job;
  4. import org.quartz.JobExecutionContext;
  5. import org.quartz.JobExecutionException;
  6. public class HiJob implements Job  {
  7.     public void execute(JobExecutionContext context)
  8.             throws JobExecutionException {
  9.         System.out.println("This is HiJob, Run time is " + new Date());
  10.     }
  11. }
  1. package com.demo.quartz;
  2. import java.util.Date;
  3. import org.quartz.Job;
  4. import org.quartz.JobExecutionContext;
  5. import org.quartz.JobExecutionException;
  6. public class MyJob implements Job {
  7.     public void execute(JobExecutionContext context)
  8.             throws JobExecutionException {
  9.         System.out.println("This is MyJob, Run time is " + new Date());
  10.     }
  11. }

  f. Client测试类

  1. package com.demo.quartz;
  2. import org.quartz.Scheduler;
  3. import org.quartz.SchedulerException;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class Client {
  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args) {
  11.         
  12.         ApplicationContext context = 
  13.             new ClassPathXmlApplicationContext("applicationContext.xml");
  14.         Scheduler a = (Scheduler) context.getBean("QuartzJobFactory");
  15.         try {
  16.             a.start();
  17.         } catch (SchedulerException e) {
  18.             e.printStackTrace();
  19.         }
  20.     }
  21. }

  在Spring中集成Quartz极大地方便了任务调度功能的实现,并且通过SchedulerFactoryBean能够和Spring容器的生命周期关联,在Spring容器启动时,启动调度器。

 

  e. MyJob

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值