spring-quartz定時任務整合

1.增加quartz的jar包

quartz-2.2.1.jar

quartz-jobs-2.2.1.jar

2.web.xml中增加使用quartz的配置文件

  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml,classpath:spring-task.xml</param-value>
	</context-param>

3.spring-task.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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 啓動觸發器的配置開始 -->
    <bean name="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cleanUpThumbnailTrigger" />
                <!-- <ref bean="licenseTrigger" /> -->
            </list>
        </property>
    </bean>
    <!-- 啓動觸發器的配置結束 -->
    <bean id="cleanUpThumbnailTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="cleanUpThumbnailDetail" />
        </property>
        <property name="cronExpression">
            <!--middle night 24. do it  -->
            <value>0 0 0 */7 * ?</value>
        </property>
    </bean>
    <!-- <bean id="licenseTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="licenseJobDetail"/>  
	    10 seconds
	    <property name="startDelay" value="0" />
	    repeat every 5 seconds
	    <property name="repeatInterval" value="5000" />
    </bean> -->
    <!-- 調度的配置結束 -->

    <!-- job的配置開始 -->
    <bean id="cleanUpThumbnailDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="cleanUpThumbnailJob" />
        </property>
        <property name="targetMethod">
            <value>execute</value>
        </property>
    </bean>
    <!-- job的配置結束 -->

    <!-- 工作的bean -->
    <bean id="cleanUpThumbnailJob" class="com.hytera.eems.job.cleanUpThumbnailJob" />
</beans>

4.class文件

package com.hytera.eems.job;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.hytera.eems.util.ice.log.IceLogService;
/**
 * 清理縮略圖作業
 * @author y15212
 *
 */
public class cleanUpThumbnailJob{
	/**
	 * 執行縮略圖文件刪除任務
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(cleanUpThumbnailJob.class);

	public void execute() {  
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
        ServletContext servletContext = webApplicationContext.getServletContext();
        String aString = servletContext.getRealPath("").replace("\\", "/")+"images";
        delImageCache(aString);
		LOGGER.debug("七天了,要開始執行刪除圖片緩存的任務了");
		IceLogService.writeIceLog("Seven days, to start the task of deleting the image cache", 0);
    }
	
	public void  delImageCache(String path){
	        File f = new File(path);
	        if (!f.exists()) {
	            System.out.println(path + " not exists");
	            return;
	        }

	        File fa[] = f.listFiles();
	        for (int i = 0; i < fa.length; i++) {
	            File fs = fa[i];
	            if (fs.isDirectory()) {
	                String filedirname = fs.getName();
	                SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd");
	                String nowdate = formart.format(new Date());
	                if(!nowdate.equals(filedirname)){//不是今天創建的文件,則全部刪除
	                    File[] children = fs.listFiles();
                        //遞歸刪除目錄中的子目錄下
                        for (int j=0; j<children.length; j++) {
                             boolean success =children[j].delete();
                             if (!success) {
                                 return ;
                             }
                         }
                         // 目錄此時爲空,可以刪除
                        fs.delete();
	                }
	            } 
	        }
	    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章