Quartz与spring2.0集成备忘录

经过一段时间的查找资料和试验,发现Quartz与spring2.0集成比较简单,在此做下记录,方便以后查找。

新建web工程如图所示:

第一步:新建个类TaskService并添加个方法execute,只是个普通的JavaBean;

TaskService.java

package com.go.task;

import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;

public class TaskService {
 Logger logger = Logger.getLogger(TaskService.class);

 public void execute() {
  String command = "xcopy D://work//mms_web//WebRoot//XMLData c://Media /s";
  try {
   Runtime.getRuntime().exec(command);
  } catch (IOException e) {
   logger.error(e);
  }
  logger.info(new Date() + " 任务执行中 " + command);
 }
}

第二步:导入Quartz和spring2.0的jar包,以及log4j的包;

第三步:在web.xml中增加初始化sping的配置信息;

web.xml


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 
  contextConfigLocation
  >/WEB-INF/classes/applicationContext.xml
 

 
  org.springframework.web.context.ContextLoaderListener
 

 
  index.jsp
 

第四步:在src下添加sping和log4j的配置文件,并增加相应配置信息;

applicationContext.xml


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/jee
  http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

 
 

 
   class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  
   
    
   

  

 

 
   class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  
   
  

  
   execute
  

 

 
 
  
   
  

  
   0 * * * * ?
  

 

log4j.properties

log4j.rootCategory=INFO,stdout,fileout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d LV:%5p CL:%c%t] %m%n

#log4j.appender.fileout.encoding=UTF-8
log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=D/://workarea//log.log
log4j.appender.fileout.MaxFileSize=5MB
log4j.appender.fileout.MaxBackupIndex=100
log4j.appender.fileout.Append=true
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%C.%M():%L] %m%n

你可能感兴趣的:(java)