Quartzタスクスケジューリング0 0 17*?
プロジェクトディレクトリ構造
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 >
< property name = "cronExpression" >
< value > 0 0 17 * * ? value >
property >
bean >
< 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" >
< 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 >
classpath*:beans.xml
classpath*:quartz.xml
param-value >
context-param >