quartz定时任务

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="InitJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="temFileTask" />
        </property>
        <property name="targetMethod">
            <value>deleteFile</value>
        </property>
        <property name="concurrent" value="false" />
    </bean>
    <bean id="InitTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="InitJobDetail" />
        </property>
        <property name="cronExpression">
            <value>0 0/30 0/1 * * ? </value>
        </property>
    </bean>
    <bean id="InitJobDetailLogin"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="loginTemFileTask" />
        </property>
        <property name="targetMethod">
            <value>deleteFile</value>
        </property>
        <property name="concurrent" value="false" />
    </bean>
    <bean id="InitTriggerLogin" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="InitJobDetailLogin" />
        </property>
        <property name="cronExpression">
            <value>0 0/20 0/1 * * ? </value>
        </property>
    </bean>
    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local="InitTrigger" />
                <ref local="InitTriggerLogin" />
            </list>
        </property>
    </bean>

    <bean id="temFileTask" class="net.totosea.task.TemFileTask"></bean>
    <bean id="loginTemFileTask" class="net.totosea.task.LoginTemFileTask"></bean>
</beans>

删除临时文件的任务类:

package net.totosea.task;

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

import net.totosea.actionImpl.PropertyManager;
import net.totosea.entity.TemFile;
import net.totosea.other.GenPath;
import net.totosea.other.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class LoginTemFileTask implements Job {

    private final static Log LOG = LogFactory.getLog(LoginTemFileTask.class);

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            deleteFile();
            if (LOG.isInfoEnabled()) {
                LOG.info("删除临时文件!" + new Date());
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (LOG.isDebugEnabled()) {
                LOG.debug("删除临时文件出错:debug----" + e.getMessage());
            }
        }
    }

    @SuppressWarnings("unchecked")
    private void deleteFile() throws IOException {
        ApplicationContext ctx = new FileSystemXmlApplicationContext(
                "classpath:applicationContext.xml");
        HibernateTemplate hibernateTemplate = (HibernateTemplate) ctx
                .getBean("hibernateTemplate");
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        tx = session.beginTransaction();

        List<TemFile> list = hibernateTemplate.find("from TemFile");
        if (list.size() > 0) {
            for (TemFile f : list) {
                long mins = 60L * 1000L;
                if ((new Date().getTime() - f.getCreateDate().getTime()) / mins > Integer
                        .parseInt(PropertyManager
                                .getParagraphContextByProperties("temminute"))) {
                    String filePath = f.getFilepath();
                    String zipPath = f.getZippath();
                    if (filePath != null && !filePath.equals("")) {
                        File file = new File(GenPath.getFile("gen") + filePath);
                        file.delete();
                    }
                    if (zipPath != null && !"".equals(zipPath)) {
                        File zip = new File(GenPath.getFile("gen") + zipPath);
                        zip.delete();
                    }
                    Timer timer = new Timer();
                    hibernateTemplate.delete(f);
                    if (LOG.isInfoEnabled()) {
                        LOG.info("删除的临时文件是:" + GenPath.getFile("gen")
                                + filePath + "," + GenPath.getFile("gen")
                                + zipPath + ",删除文件所需要的时间为:" + timer.getTotal());
                    }
                }
            }
        }

        tx.commit();
        session.flush();
        session.clear();
        session.close();
        sessionFactory.close();
    }

}
package net.totosea.task;

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

import net.totosea.actionImpl.PropertyManager;
import net.totosea.entity.FileN;
import net.totosea.other.GenPath;
import net.totosea.other.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class TemFileTask implements Job {

    private final static Log LOG = LogFactory.getLog(TemFileTask.class);

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            deleteFile();
            if (LOG.isInfoEnabled()) {
                LOG.info("删除临时文件!" + new Date());
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (LOG.isDebugEnabled()) {
                LOG.debug("删除临时文件出错:debug----" + e.getMessage());
            }
        }
    }

    @SuppressWarnings("unchecked")
    private void deleteFile() throws IOException {
        ApplicationContext ctx = new FileSystemXmlApplicationContext(
                "classpath:applicationContext.xml");
        HibernateTemplate hibernateTemplate = (HibernateTemplate) ctx
                .getBean("hibernateTemplate");
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        tx = session.beginTransaction();

        List<FileN> list = hibernateTemplate.find("from FileN");
        if (list.size() > 0) {
            for (FileN f : list) {
                long mins = 60L * 1000L;
                if ((new Date().getTime() - f.getCreateDate().getTime()) / mins > Integer
                        .parseInt(PropertyManager
                                .getParagraphContextByProperties("fileminute"))) {
                    String filePath = f.getFilepath();
                    String zipPath = f.getZippath();
                    if (filePath != null && !filePath.equals("")) {
                        File file = new File(GenPath.getFile("gen") + filePath);
                        file.delete();
                    }
                    if (zipPath != null && !"".equals(zipPath)) {
                        File zip = new File(GenPath.getFile("gen") + zipPath);
                        zip.delete();
                    }
                    Timer timer = new Timer();
                    if (LOG.isInfoEnabled()) {
                        LOG.info("删除的临时文件是:" + GenPath.getFile("gen")
                                + filePath + "," + GenPath.getFile("gen")
                                + zipPath + ",删除文件所需要的时间为:" + timer.getTotal());
                    }
                    hibernateTemplate.delete(f);
                }
            }
        }

        tx.commit();
        session.flush();
        session.clear();
        session.close();
        sessionFactory.close();
    }

}
原文地址:https://www.cnblogs.com/tatame/p/2442699.html