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/archive/2012/04/11/2442699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值