环境
最近项目(springmvc+jboss(tomcat)+db2)需要用到定时执行,java 定时任务
java.util.Timer
- Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
Quartz
- 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,兼容性比较好。(quartz-all-1.6.1.jar),
需要继承 org.springframework.scheduling.quartz.QuartzJobBean类,配置触发器(SimpleTriggerBean或CronTriggerBean)每隔指定时间则触发一次
Spring-task
- Spring3.0以后自带的task :scheduledTask,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。(spring-support.jar),必须spring3.0以后的版本。
总结
- 如果用spring的话,其自带的task比较好用。这些应用的配置,gogle百度一大把、这里贡献一个 scheduledTask的配置文件(清晰简单)
http://gong1208.iteye.com/blog/1773177 (参考)<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ss="http://www.springframework.org/schema/security" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- 注册相关后置处理器,扫描包路径下的注解配置 --> <context:component-scan base-package="com.suning.ebuy.ott" /> <span style="white-space:pre"> </span>...这里到<span style="font-family: Helvetica, Tahoma, Arial, sans-serif;">myJob1之间其实还有</span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif;">很多bean,不好扫描所以在这里面配置了,这几个文件没有删除是想表达这个项目很庞大...</span> <bean id="memberAccountMgmt" class="com.suning.ebuy.ott.impl.service.esb.member.MemberAccountMgmt" /> <bean id="customerLevelMgmt" class="com.suning.ebuy.ott.impl.service.esb.member.CustomerLevelMgmt" /> <bean id="userService" class="com.suning.ebuy.ott.impl.user.UserServiceImpl" /> <bean id="rscServerService" class="com.suning.ebuy.ott.impl.rscserver.RscServerServiceImpl" /> <bean id="act" class="com.suning.ebuy.ott.rscserver.ApplicationContextInit" /> <bean id="myJob" class="com.suning.ebuy.ott.impl.rscserver.MyJob"> <property name="msgType" value="abc_2" /> </bean> <bean id="myJob1" class="com.suning.ebuy.ott.impl.rscserver.MyJob1"> <property name="msgType" value="abc_1" /> </bean> <bean id="headPortraitService" class="com.suning.ebuy.ott.impl.service.esb.member.HeadPortraitServiceImpl"></bean> <bean id="goodsPriceService" class="com.suning.ebuy.ott.impl.service.esb.tv3.GoodsPriceServiceImpl"></bean> <bean id="chanColumnService" class="com.suning.ebuy.ott.impl.service.esb.bigfavor.ChanColumnServiceImpl"></bean> <bean id="chanGPService" class="com.suning.ebuy.ott.impl.service.esb.bigfavor.ChanGPServiceImpl"></bean> <bean id="gPActivityInfoService" class="com.suning.ebuy.ott.impl.service.esb.bigfavor.GPActivityInfoServiceImpl"></bean> <!-- 初始化logback配置文件 --> <bean id="loggingInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="com.suning.framework.log.util.LogbackConfigurer" /> <property name="targetMethod" value="initLogging" /> <property name="arguments"> <list> <value>classpath:conf/log/main_${envName}_logging.xml</value> </list> </property> </bean> <!-- target service and auto run method --> <!-- 媒讯数据导入 --> <bean id="mediaSpeedAutoTask" class="com.suning.ebuy.ott.task.SystemAutoTask" /> <bean id="searchDayStatTask" class="com.suning.ebuy.ott.reportTask.StatExecutor"> <property name="dmlSqlIds"> <map> <entry key="delete"><value>shotStatSearchDay.delete</value></entry> <entry key="insert"><value>shotStatSearchDay.insert</value></entry> <entry key="update"><value>shotStatSearchDay.update</value></entry> </map> </property> <property name="statSqlIds"> <list> <value>shotStatSearchDay.stat</value> </list> </property> </bean> <bean id="searchStatTask" class="com.suning.ebuy.ott.reportTask.StatExecutor"> <property name="dmlSqlIds"> <map> <entry key="delete"><value>shotStatSearch.delete</value></entry> <entry key="insert"><value>shotStatSearch.insert</value></entry> <entry key="update"><value>shotStatSearch.update</value></entry> </map> </property> <property name="statSqlIds"> <list> <value>shotStatSearch.stat</value> </list> </property> </bean> <bean id="channelStatTask" class="com.suning.ebuy.ott.reportTask.StatExecutor"> <property name="dmlSqlIds"> <map> <entry key="delete"><value>channelStat.delete</value></entry> <entry key="insert"><value>channelStat.insert</value></entry> <entry key="update"><value>channelStat.update</value></entry> </map> </property> <property name="statSqlIds"> <list> <value>channelStat.stat1</value> <value>channelStat.stat2</value> <value>channelStat.stat3</value> <value>channelStat.stat4</value> <value>channelStat.stat5</value> </list> </property> </bean> ...为了节省空间,其它task删除... <bean id="cpaStatTask" class="com.suning.ebuy.ott.reportTask.StatExecutor"> <property name="dmlSqlIds"> <map> <entry key="delete"><value>statCpa.delete</value></entry> <entry key="insert"><value>statCpa.insert</value></entry> <entry key="update"><value>statCpa.update</value></entry> </map> </property> <property name="statSqlIds"> <list> <value>statCpa.stat1</value> <value>statCpa.stat2</value> <value>statCpa.stat3</value> </list> </property> </bean> <bean id="flowDayStatTask" class="com.suning.ebuy.ott.reportTask.StatExecutor"> <property name="dmlSqlIds"> <map> <entry key="delete"><value>flowStat.delete</value></entry> <entry key="insert"><value>flowStat.insert</value></entry> <entry key="update"><value>flowStat.update</value></entry> </map> </property> <property name="statSqlIds"> <list> <value>flowStat.stat1</value> <value>flowStat.stat2</value> <value>flowStat.stat3</value> <value>flowStat.stat4</value> <value>flowStat.stat5</value> <value>flowStat.stat6</value> <value>flowStat.stat7</value> </list> </property> </bean> <task:scheduled-tasks scheduler="myScheduler"> <!-- 每天早上9点运行 --> <task:scheduled ref="searchDayStatTask" method="stat" cron="0 0 9 * * ?" /> <task:scheduled ref="searchStatTask" method="stat" cron="0 0 9 * * ?" /> <task:scheduled ref="channelStatTask" method="stat" cron="0 0 9 * * ?" /> <task:scheduled ref="cpaStatTask" method="stat" cron="0 0 9 * * ?" /> <task:scheduled ref="flowDayStatTask" method="stat" cron="0 0 9 * * ?" /> <task:scheduled ref="flowHourStatTask" method="stat" cron="0 0 9 * * ?" /> <task:scheduled ref="pageClickStatTask" method="stat" cron="0 0 9 * * ?" /> <!-- 2:00点到18:点每隔半小时 --> <task:scheduled ref="mediaSpeedAutoTask" method="doTask" cron="0 0/30 2-18 * * ?" /> </task:scheduled-tasks> <task:scheduler id="myScheduler" pool-size="10"/> </beans>