spring-quartz学习

1.配置文件 applicationContext-quartz.xml

 

<?xml version="1.0" encoding="GBK"?>
<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="clearImgTimerManager" class="com.mypro.manage.impl.ClearImgTimerManagerImpl">
    </bean>
    
       
    
    <!-- 创建一个Quartz工作,即配置ClearImgJob类,注意是用org.springframework.scheduling.quartz.JobDetailBean进行配置 -->
 <bean id="clearImgJob" class="org.springframework.scheduling.quartz.JobDetailBean">
   <property name="jobClass" value="com.mypro.util.quartz.ClearImgJob">
   </property>
   <property name="jobDataAsMap">
     <map>
       <entry key="clearImgTimerManager" value-ref="clearImgTimerManager"></entry>
     </map>
   </property>
 </bean>
 
 <!-- 调度工作 -->
 <bean id="cronClearImgTrigger"
  class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <ref bean="clearImgJob" />
  </property>
  <property name="cronExpression">
   <value>0 58 23 * * ?</value>
  </property>
 </bean>
 
 <!-- 将记录 定时插入数据库 begin-->
 <bean id="recordTimerJob" class="org.springframework.scheduling.quartz.JobDetailBean">
   <property name="jobClass" value="com.mypro.util.quartz.RecordTimerJob">
   </property>
   <property name="jobDataAsMap">
     <map>
       <entry key="recordTimerManager" value-ref="recordTimerManager"></entry>
     </map>
   </property>
 </bean>
 
 <!-- 简单调度 -->
 <bean id="simpleRecordTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
   <property name="jobDetail"> 
     <ref bean="recordTimerJob"/>  
   </property>
   <!-- 在5秒后启动5000 -->
   <property name="startDelay" value="60000"></property>
   <!-- 每一小时执行一次 36000000 24小时86400000-->
   <property name="repeatInterval" value="120000"></property>
 </bean>
 
 <!--  //复杂的调度
 <bean id="cronRecordTrigger"
  class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <ref bean="recordTimerJob" />
  </property>
  <property name="cronExpression">
   <value>0 1-59 10 * * ?</value>
  </property>
 </bean>
 -->
 
 
 <!-- 启动工作 -->
 <bean id="schedulerFactoryService" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref local="cronClearImgTrigger" />
    <ref local="simpleRecordTrigger" />    
   </list>
  </property>
 </bean>
 
 
</beans>

2.java文件

 

/**
 *
 */
package com.mypro.manage.impl;

import java.io.File;
import java.net.URL;

import org.apache.log4j.Logger;

import com.mypro.manage.ClearImgTimerManager;

/**
 * @author 
 * 清除temp文件夹上传的没有用的图片,
 * 防止项目大小不断扩大
 */
public class ClearImgTimerManagerImpl implements ClearImgTimerManager{

 private Logger log = Logger.getLogger(this.getClass());
 /**
     * 清除WebRoot/temp文件夹下的图片
     * @throws Exception
     */
 public void clearImg() throws Exception{
  //baseUrl=file:/E:/workspace/survey/WebRoot/WEB-INF/classes/
  URL baseUrl = Thread.currentThread().getContextClassLoader().getResource("");
  URL tempDirUrl = new URL(baseUrl, "../../temp");
  File file = new File(tempDirUrl.toURI());    
  int k = tempDirUrl.toString().indexOf("file:/");
  String tempDirStr = tempDirUrl.toString().substring(k+6);
  //System.out.println("tempDirStr="+tempDirStr);
  
  String[] tempList = file.list();
  File temp = null;
  if(tempList!=null){
   for (int i = 0; i < tempList.length; i++) {
    temp = new File(tempDirStr, tempList[i]);
    if(temp.isFile()){
     //判断时间
     String fileName = temp.getName();
     //获得扩展名
     int j = fileName.lastIndexOf(".");
     //String prefix = fileName.substring(j);
     String fileNameWithOutPrefix = fileName.substring(0,j);
     long interval = 24*60*60;
     //删除
     try{
      if(Long.parseLong(fileNameWithOutPrefix)<(System.currentTimeMillis()-interval)){
       temp.delete();
       System.out.println(fileName+"被定时删除了");
      }
     }catch(Exception e){
      log.info("定时删除temp文件夹中的文件:"+fileName+"删除失败");
     }
     
    }
   }
  }
  
  
  
 }
 
 public static void main(String[] args) {
  String path = "D://Java//Tomcat5.5.23//webapps//survey//temp";
  File file = new File(path);
  String[] tempList = file.list();
  File temp = null;
  if(tempList!=null){
   for (int i = 0; i < tempList.length; i++) {
    temp = new File(path+File.separator + tempList[i]);
    if(temp.isFile()){
     //判断时间
     String fileName = temp.getName();
     //获得扩展名
     int j = fileName.lastIndexOf(".");
     //String prefix = fileName.substring(j);
     String fileNameWithOutPrefix = fileName.substring(0,j);
     System.out.println("fileNameWithOutPrefix="+fileNameWithOutPrefix);
     
    }
   }
    }
 }
}

 

3.java文件

/**
 *
 */
package com.mypro.util.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.mypro.manage.ClearImgTimerManager;

/**
 * @author wangjb
 * 定时清除项目temp文件夹的暂时图片,以防项目不断扩大
 * 相当于TimerTask的等价类
 */
public class ClearImgJob extends QuartzJobBean{

 private ClearImgTimerManager clearImgTimerManager;
 
 @Override
 protected void executeInternal(JobExecutionContext arg0)
   throws JobExecutionException {
  try{
     clearImgTimerManager.clearImg();  //清除图片
  }catch(Exception e){   
  }
 }

 public ClearImgTimerManager getClearImgTimerManager() {
  return clearImgTimerManager;
 }

 public void setClearImgTimerManager(ClearImgTimerManager clearImgTimerManager) {
  this.clearImgTimerManager = clearImgTimerManager;
 }
 
 

 
}

4.//测试

/**
 *
 */
package com.hc360.test.manager.quartz;

import org.quartz.JobDetail;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.JobDetailBean;

import com.hc360.test.BaseTestCase;

/**
 * @author myhello
 * 参考:http://dolphin-ygj.javaeye.com/blog/368874
 */
public class ClearImgTimerManagerTest extends BaseTestCase{

 //测试,看定时有没有启动,查看方式 在JobDetail处加断点,进行调试查看
 public void testTimer() throws Exception{
  CronTriggerBean triggerBean = (CronTriggerBean)this.applicationContext.getBean("cronClearImgTrigger");
  
  org.quartz.Scheduler scheduler = (org.quartz.impl.StdScheduler)this.applicationContext.getBean("schedulerFactoryService");
  JobDetail job = triggerBean.getJobDetail();
   
  scheduler.triggerJob(job.getName(),job.getGroup());
  System.out.println(triggerBean.getNextFireTime()); 
  
 }

}

你可能感兴趣的:(exception,bean,String,File,Class,triggers)