chen_jianfei
2016-11-16 11:21:11
[学习笔记]Spring Timer计时器(一)
applicationContext
<!-- 数据清理--> <bean id="cleanJob" class="com.founder.servermonitor.job.CleanJob"> <property name="hardwareService" ref="hardwareService"/> <property name="softwareService" ref="softwareService"/> <property name="databaseService" ref="databaseService"/> </bean> <!-- 数据清理-->
<!--spring task 定时器开关 --> <task:scheduled-tasks> <!-- 每小时整点01分钟开始 --> <task:scheduled ref="cleanJob" method="cleanDatabase" cron="0 1 * * * ?"/> <!-- 每小时整点01分钟开始 --> <task:scheduled ref="cleanJob" method="cleanSoftware" cron="0 1 * * * ?"/> <!-- 每小时0、15、30、45分钟开始 --> <task:scheduled ref="cleanJob" method="cleanHardware" cron="* 0/15 * * * ?"/> </task:scheduled-tasks>
package com.founder.servermonitor.job; import org.apache.log4j.Logger; import com.founder.servermonitor.common.DateTool; import com.founder.servermonitor.service.DatabaseService; import com.founder.servermonitor.service.HardwareService; import com.founder.servermonitor.service.SoftwareService; /** * @ClassName: CleanJob * @Description: TODO定时清理日志数据 * @author lixiaohu * @date 2016年11月16日 上午10:30:22 */ public class CleanJob { private static final Logger logger = Logger.getLogger(DatabaseJob.class); private DatabaseService databaseService; private HardwareService hardwareService; private SoftwareService softwareService; public void cleanDatabase() { logger.debug("本轮数据库日志清理开始"); System.out.println("当前时间:"+DateTool.getDateTime()); logger.debug("本轮数据库日志清理完成"); } public void cleanSoftware() { logger.debug("本轮软件日志清理开始"); System.out.println("当前时间:"+DateTool.getDateTime()); logger.debug("本轮数据库日志完成"); } public void cleanHardware() { logger.debug("本轮硬件日志清理开始"); System.out.println("当前时间:"+DateTool.getDateTime()); logger.debug("本轮硬件日志清理完成"); } public DatabaseService getDatabaseService() { return databaseService; } public void setDatabaseService(DatabaseService databaseService) { this.databaseService = databaseService; } public HardwareService getHardwareService() { return hardwareService; } public void setHardwareService(HardwareService hardwareService) { this.hardwareService = hardwareService; } public SoftwareService getSoftwareService() { return softwareService; } public void setSoftwareService(SoftwareService softwareService) { this.softwareService = softwareService; } }
尚需要引入一些头文件
来自百度的内容
-
spring-2.0.6.jar Spring框架的核心包
-
jta.jar 会用到包中的usertransaction,具体什么用此处暂时不论
-
quartz-1.6.0.jar 会用到CronTrigger这个类,通过表达式实现精确的定时
-
commons-logging-1.1.1.jar 日志管理包
-
commons-collections-3.2.1.jar 集合工具包
评论
最近浏览
chen_jianfei LV2
2017年11月2日