zoukankan      html  css  js  c++  java
  • 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();
        }
    
    }
  • 相关阅读:
    如何实现基于消息/传输安全验证机制下的windows身份验证过程、无任何验证
    利用WCF的callback机制开发一个简单的多人游戏模型
    WCF常见预绑定协议中各种安全模式(Message Security|Transport Security)所支持的客户端凭证验证类型汇总
    tornado+peeweeasync+peewee+mysql(一)
    IIS 内部运行机制
    IIS 7 托管管道模式 经典模式(Classic) 集成模式(Integrated) 分析与理解
    斐波那契查找算法
    进制间转换
    严版数据结构题集3.15
    (TOJ 4413)IP address
  • 原文地址:https://www.cnblogs.com/tatame/p/2442699.html
Copyright © 2011-2022 走看看