Spring定时器应用

来源:互联网 发布:python 量化交易平台 编辑:程序博客网 时间:2024/04/29 08:48
<bean id="systemScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <!-- 添加触发器 -->        <property name="triggers">            <list>            <!-- 处理ue上传文件 -->                <ref bean="cronTrigger4OptUeAttaFileJob" />            <!-- 处理form上传文件 -->                <ref bean="cronTrigger4OptFormAttaFileJob" />            </list>        </property>    </bean>


<!-- 清理附件的定时作业 --><!-- cronExpression结束   --><!-- 字段       允许值                  允许的特殊字符--><!-- 秒    0-59            , - * / --><!-- 分    0-59            , - * / --><!-- 小时   0-23            , - * / --><!-- 日期   1-31            , - * ? / L W C --><!-- 月份   1-12 或者 JAN-DEC   , - * / --><!-- 星期   1-7 或者 SUN-SAT   , - * ? / L C # --><!-- 年(可选)  留空, 1970-2099   , - * / --><!-- 处理ueditor上传附件作业 --><bean name="optUeAttaFileJob" class="org.springframework.scheduling.quartz.JobDetailBean"><property name="jobClass" value="com.ccxe.p2p.job.OptUeAttaJob"/></bean><!-- 处理form上传附件作业 --><bean name="optFormAttaFileJob" class="org.springframework.scheduling.quartz.JobDetailBean"><property name="jobClass" value="com.ccxe.p2p.job.OptFormAttaJob"/></bean><bean id="cronTrigger4OptUeAttaFileJob" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="optUeAttaFileJob"/><!-- 在每天凌晨2点开始执行 --><property name="cronExpression" value="0 0 2 * * ?" /></bean><bean id="cronTrigger4OptFormAttaFileJob" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="optFormAttaFileJob"/><!-- 在每天凌晨2点开始执行 --><property name="cronExpression" value="0 0 2 * * ?"/></bean>

public class OptUeAttaJob extends QuartzJobBean{Logger log = Logger.getLogger(OptUeAttaJob.class);@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {try {log.info("系统作业---处理ueditor上传附件开始");WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();P2pConfig p2pConfig = (P2pConfig)wac.getBean("p2pConfig");IIAttaService attaService = (IIAttaService)wac.getBean("attaService");attaService.optUeFile(p2pConfig);log.info("系统作业---处理ueditor上传附件结束");} catch (Exception e) {log.error("系统作业---处理ueditor上传附件出错", e);}}}


public void optUeFile(P2pConfig p2pConfig) throws Exception{//获取ueditor附件在容器下的保存路径String uploadPath = p2pConfig.getUeditorFilePath();String relPath = Thread.currentThread().getContextClassLoader().getResource("").getPath().replace("WEB-INF/classes/", ""); uploadPath = relPath + uploadPath;SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");String now = sdf.format(getPrevDay(new Date()));uploadPath += "/" +now;if(!new File(uploadPath).isDirectory()){try {new File(uploadPath).mkdirs(); //创建目录} catch (RuntimeException e) {e.printStackTrace();}}else{List<IAtta> ueAttaFile = attaDAO.queryAttaByDateAndSrc("1", new Date(), "");//获取所有上传文件绝对路径List<String> filePath = getFileList(uploadPath);for(IAtta atta:ueAttaFile){String attaPath = atta.getAttPath();for(String tempFileName:filePath){if(tempFileName.contains(attaPath)){continue;}else{//数据库中不包含服务器上的文件则说明当前文件为无效文件,须删除FileUtils.deleteFile(tempFileName);}}}}}//获取前一天的日期private static Date getPrevDay(Date date) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.DAY_OF_MONTH, -1);date = calendar.getTime();return date;}

/** * Desc 得到目录下所有文件绝对路径 * @author:dongliyuan * @date:2014-9-30 上午11:01:03 * @param filePath * @return * @throws UnsupportedEncodingException */private List<String> getFileList(String filePath)    throws UnsupportedEncodingException {List<String> fileList = new ArrayList<String>();//定义List用于装载当前目录下的所有文件File tempFile = new File(filePath);if (tempFile.exists() && tempFile.canRead()) {    if (tempFile.isDirectory()) {        for (File tmp : tempFile.listFiles()) { //遍历子文件或子文件夹            fileList.addAll(getFileList(tmp.getAbsolutePath()));        }    } else {        fileList.add(filePath);//保存当前文件的绝对路径    }}return fileList;}



0 0
原创粉丝点击