工程目录结构
spring + quartz JAR包
ApplicationContext.xml(beans.xml)
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" > < context:annotation-config /> < context:component-scan base-package = "com" /> < aop:aspectj-autoproxy /> <!-- 要调用的工作类 --> < bean id = "quartzJob" class = "com.kay.quartz.QuartzJob" ></ bean > <!-- 定义调用对象和调用对象的方法 --> < bean id = "jobtask" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <!-- 调用的类 --> < property name = "targetObject" > < ref bean = "quartzJob" /> </ property > <!-- 调用类中的方法 --> < property name = "targetMethod" > < value > work </ value > </ property > </ bean > <!-- 定义触发时间 --> < bean id = "doTime" class = "org.springframework.scheduling.quartz.CronTriggerBean" > < property name = "jobDetail" > < ref bean = "jobtask" /> </ property > <!-- cron 表达式 --> < property name = "cronExpression" > < value > 0 0 17 * * ? </ value > </ property > </ bean > <!-- 总管理类 如果将 lazy-init='false' 那么容器启动就会执行调度程序 --> < bean id = "startQuertz" lazy-init = "false" autowire = "no" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > < property name = "triggers" > < list > < ref bean = "doTime" /> </ list > </ property > </ bean > </ beans >
任务调度类
package com.kay.quartz; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Date; /** * quartz 任务调度 , 每天下午 17:00, 自动将 e:/ 下的 jpg 图片 , 拷贝到 e:/aa/ 下 * * 秒 分 时 月中天 月 周中天 [ 年 ] * * 0 0 17 * * ? * * @author leiwei 2011 - 11 - 29 * * */ public class QuartzJob { public void work() throws Exception { int b = 0; // Make sure the directory exists File dir = new File( "e:/" ); if (!dir.exists()){ throw new Exception( "Directory not configured" ); } // Use FileFilter to get only jpg files FileFilter filter = new FileExtensionFileFilter( ".jpg" ); File[] files = dir.listFiles(filter); // Return since there were no files if (files == null || files. length <= 0) { System. out .println( "No XML files found in " + dir); return ; } // The number of jpg files int size = files. length ; // Iterate through the files found for ( int i = 0; i < size; i++) { File file = files[i]; // Log something interesting about each file. File aFile = file.getAbsoluteFile(); FileInputStream ino = new FileInputStream(aFile); FileOutputStream fos = new FileOutputStream( "e:/aa/" +file.getName()); while ((b=ino.read())>-1){ fos.write(b); } } } } // 内部类,过滤文件 class FileExtensionFileFilter implementsFileFilter{ private String extension ; public FileExtensionFileFilter(String extension) { this . extension = extension; } public boolean accept(File file) { String lCaseFilename = file.getName().toLowerCase(); return (file.isFile() && (lCaseFilename.indexOf( extension ) > 0 )) ? true:false ; } }
测试
package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { System. out .println( " 调度开始 ..." ); ApplicationContext apc = new ClassPathXmlApplicationContext( "beans.xml" ); System. out .println( " 调度结束 ..." ); } }
参考: http://it.oyksoft.com/post/698/
续:上面只是quartz的独立应用,如何将它整合到实际的项目应用中了。
在原有的ssh基础上,加入quartz.xml(在这里面配置所需的任务调度),
再在web.xml文件contextConfigLocation下param-value中追加classpath*:quartz.xml
结构图、文件如下
quartz.xml
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > <!-- 对 testService 中的 testQuartz 方法进行定时任务调度 --> < bean id = "methodInvokingJobDetailForSms" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > < property name = "targetObject" > < ref bean = "testService" /> </ property > < property name = "targetMethod" > < value > testQuartz </ value > </ property > </ bean > < bean id = "cronTriggerForSms" class = "org.springframework.scheduling.quartz.CronTriggerBean" > < property name = "jobDetail" > < ref bean = "methodInvokingJobDetailForSms" /> </ property > < property name = "cronExpression" > < value > 0 * 12 * * ? </ value > </ property > </ bean > < bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > < property name = "triggers" > < list >< ref local = "cronTriggerForSms" /></ list > </ property > </ bean > </ beans >
web.xml
< context-param > < param-name > contextConfigLocation </ param-name > <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --> < param-value > classpath*:beans.xml classpath*:quartz.xml </ param-value > </ context-param >