Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Java项目中定时任务之Quartz的应用

Quartz 是个开放源码项目,提供了丰富的作业调度集。我目前接触到的quartz只是在我做的java项目中定时执行任务,我的项目因为是在spring的基础上搭建的想要整合quartz非常的简单。对于非spring项目的应用,它也很强大因为我没有实际测试过不做介绍。如果有需要的可以查看软件工程师 Michael Lipton 和 IT 架构师 Soobaek Jang 对 Quartz API 进行的介绍。链接地址:用 Quartz 进行作业调度下面主要说一个quartz在spring项目中的应用首先添加所需要的jar包:quartz-1.5.2.jar 和spring框架所需要的架包首先写个一个定时执行任务的类public class QuartzJob {
  public void work()
   {
 Date d = new Date();
 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");//时:分:秒:毫秒
 System.out.println("Quartz的任务调度"+sdf.format(d));
   }
  //手动启动测试是否定时任务是否编写成功
 public static void main(String[] args) {
 String[] configs={"file:D:/Workspaces/MyEclipse 8.5/.metadata/.me_tcat/webapps/zcz_test/WEB-INF/classes/applicationContext-quartz.xml"};
 ApplicationContext ac=new ClassPathXmlApplicationContext(configs);
 }
}在写一个spring的配置文件 applicationContext-quartz.xml(名字随便起,但要在web.xml引用)<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> 
 
      <bean id="testJob" class="quartz.QuartzJob" />
     
      <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="testJobDetail" /> 
        <property name="cronExpression" value="9,18,28,29,39,38,46,43,52,57 * * * * ?" /> 
   </bean> 
 
   <bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
        <property name="targetObject" ref="testJob" /> 
        <property name="targetMethod" value="work" /> 
        <property name="concurrent" value="false" />
   </bean> 
     
   <bean id="localQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"  lazy-init="false"> 
     <property name="triggers"> 
         <list>
         <ref bean="testTrigger"/>
         <!--
              <ref bean="iptvCheckTrigger"/>
              <ref bean="iptvParseTrigger"/>
              <ref bean="IptvUploadTrigger"/>
              -->
         </list> 
     </property> 
    </bean>   
</beans>推荐阅读:Spring集成Quartz定时任务框架介绍和Cron表达式详解 http://www.linuxidc.com/Linux/2013-03/81947.htmSpring整合Quartz http://www.linuxidc.com/Linux/2012-12/75284.htmSpring的Quartz定时器同一时刻重复执行二次的问题解决 http://www.linuxidc.com/Linux/2012-11/73443.htmSpring 定时器Quartz的用法 http://www.linuxidc.com/Linux/2012-11/73442.htmSpring联姻Quartz实现作业调度 http://www.linuxidc.com/Linux/2012-08/69215.htm