spring quartz 定时任务示例
来源:互联网 发布:2016酒店行业数据分析 编辑:程序博客网 时间:2024/06/05 19:13
刚用spring quartz 写了一个定时清理文件夹中excel文件的功能(粗略代码,不经细琢)
在写此功能之前我查了一下,能够实现定时任务的几种方式,主要就是Timer 和 quartz,相比较quartz功能更强大,故选此法
好了,闲话不多说,上干货
具体步骤以及代码
1,导入必要的jar包(略)
2,写定时任务类以及方法(此类需要交于spring管理)
package com.cpic.itCloud.service.quartz;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.cpic.itCloud.service.context.ConfigService;
/**
* 计划任务服务类
*
* @author zhutashan
*
*/
@Service
public class ScheduleTaskService {
protected Logger logger = LoggerFactory.getLogger(getClass());
/**
* 清理导出产生的Excel文件
*/
public void cleanExcelFile() {
logger.info("start cleanExcelFile");
StopWatch sw = new StopWatch();
sw.start();
List<File> files = findNeedCleanExcelFile();
for (File f : files) {
if (f.exists()) {
f.delete();
logger.info("delete " + f.getName());
}
}
sw.stop();
logger.info("finish cleanExcelFile [waste time={} second]", sw.getTime() / 1000d);
}
private List<File> findNeedCleanExcelFile() {
List<File> list = new ArrayList<File>();
// 获取excel文件所在路径
File file = new File(this.getClass().getClassLoader().getResource("/").getPath());
String excelPath = file.getParentFile().getParentFile().getAbsolutePath();
File excelDir = new File(excelPath, ConfigService.EXCEL_DOWNLOAD_PATH.substring(0, ConfigService.EXCEL_DOWNLOAD_PATH.lastIndexOf("/")));
// 得到excel 文件
File[] excelFiles = excelDir.listFiles(new FilenameFilter() {
public boolean accept(File file, String name) {
// 这里的file是dir引用所指的对象,name就是需要过滤的内容
return name.endsWith(".xls");
}
});
// 得到此时 CLEAN_EXCEL_FILE_INTERVAL 以前未修改的excel,用来清除
for (File f : excelFiles) {
boolean needClean = (new Date().getTime() - f.lastModified()) / 1000 / 60 > ConfigService.CLEAN_EXCEL_FILE_INTERVAL ? true : false;
if (needClean) {
list.add(f);
}
}
return list;
}
}
3,在quartz.xml中配置此定时方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:core="http://cxf.apache.org/core"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" default-lazy-init="true">
<!-- 定时清理excel -->
<bean id="cleanExcelFileJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduleTaskService" />
<property name="targetMethod" value="cleanExcelFile" />
<property name="concurrent" value="false" />
</bean>
<bean id="cleanExcelFileTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="cleanExcelFileJob" />
<property name="cronExpression">
<value>0 0 */2 * * ?</value>
</property>
<!-- <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_DO_NOTHING"/> -->
</bean>
<bean id="DefaultQuartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cleanExcelFileTrigger" />
</list>
</property>
<!-- quartz配置文件路径, 指向配置 -->
<property name="configLocation" value="classpath:quartz/quartz.properties" />
</bean>
</beans>
- spring quartz 定时任务示例
- Quartz定时任务,Quartz Spring定时任务,spring多个定时任务Quartz配置,示例,详解
- spring+quartz定时任务示例【转】
- Spring+Quartz定时任务示例1
- Spring+Quartz定时任务示例2
- Spring+Quartz定时任务
- spring quartz 定时任务
- Spring-quartz定时任务
- spring 定时任务 quartz
- spring quartz 定时任务
- spring Quartz 定时任务
- spring+quartz定时任务
- Spring Quartz 定时任务
- spring quartz 定时任务
- Spring+quartz 定时任务
- spring quartz定时任务
- spring-quartz 定时任务
- spring quartz 定时任务
- 计算机初级阶段学习内容
- Oracle 自定义字符串转换数组函数
- JAVA MemCache 史无前例的详细讲解【转】
- Django admin的一些有用定制
- 移动开发大作业————随手记(主界面和编辑界面)
- spring quartz 定时任务示例
- java 重载与重写
- 秒杀多线程第十二篇 多线程同步内功心法——PV操作上
- 理解 Java 的三大特性之封装
- linux安装redis全过程
- QT多平台移植经验分享及问题解析
- Sublime、Webstorm等App快速开发工具盘点
- Android Touch事件分发和消费机制
- Redis学习笔记(三)---对象