JAVA—咖啡馆

——欢迎访问rogerfan的博客,常来《JAVA——咖啡馆》坐坐,喝杯浓香的咖啡,彼此探讨一下JAVA技术,交流工作经验,分享JAVA带来的快乐!本网站部分转载文章,如果有版权问题请与我联系。

BlogJava 首页 新随笔 联系 聚合 管理
  447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
在Spring中,使用JDK的Timer类库来做任务调度功能不是很方便,关键它不可以象cron服务那样可以指定具体年、月、日、时和分的时间。你只能将时间通过换算成微秒后传给它。如任务是每天执行一次,则需要在spring中如下配置:
­
<bean id="scheduledTask" class= "org.springframework.scheduling.timer.ScheduledTimerTask">
<!--程序启动后开始执行任务的延迟时间 -->
<property name="delay" value="0" />
<!--每隔一天【一天=24×60×60×1000微秒】执行一次-->
<property name="period" value="86400000" />
<!--业务统计报表bean -->
<property name="timerTask" ref="businessReport" />
</bean>
­
其中period就是一天的微秒数。如果每月1日运行一次,那就复杂了,不知如何配置。因为月份有大、小月之分,每月的微秒数都不一样。
­
而Quartz类库不但有着上述JDK的Timer类库类似的配置,更重要的,它还有着类似于unix的cron服务的配置。因此,在迁移中我们采用了Quartz类库的接口。

具体的步骤如下:
1 编写业务类,该类继承了org.quartz.Job,主要的逻辑在execute方法中编写

2 配置spring的applicationContext.xml文件
    2.1 配置任务JobDetailBean
    2.2配置触发器 CronTriggerBean
    2.3配置调度器  SchedulerFactoryBean

3 所需要的jar包:
         spring.jar,quartz.jar,commons-logging-1.0.4.jar,commons-dbcp-1.2.2.jar,commons-pool-1.3.jar

4 把quartz.properties放到类路径下

以下为一个demo

业务类:

Java代码

 

package task;   
  
import java.util.Date;   
  
import org.quartz.JobExecutionContext;   
import org.quartz.JobExecutionException;   
  
public class BusinessReport implements org.quartz.Job{   
      
public void perform()//执行报表统计入口函数   
            
//业务逻辑   
          System.out.println("开始执行报表的业务逻辑了----现在的时间是--"+new Date());   
             
        }
   
  
    
public void execute(JobExecutionContext arg0) throws JobExecutionException {   
        perform();   
           
    }
   
  
}
   

applicationContext.xml文件

 三 quartz.properties文件的内容(默认放在类路径下)
#============================================================================
# Configure Main Scheduler Properties 
#============================================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

#============================================================================
# Configure ThreadPool 
#============================================================================
#org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
#org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

#============================================================================
# Configure JobStore 
#============================================================================
#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.misfireThreshold = 60000
#org.quartz.jobStore.useProperties = false
#org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.dataSource = myDS

#org.quartz.jobStore.isClustered = true
#org.quartz.jobStore.clusterCheckinInterval = 15000

#============================================================================
# Configure DataSource
#============================================================================
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost/test
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = root
org.quartz.dataSource.myDS.maxConnections = 10


附:cronExpression表达式解释:
0 0 12 * * ?---------------在每天中午12:00触发
0 15 10 ? * *---------------每天上午10:15 触发
0 15 10 * * ?---------------每天上午10:15 触发
0 15 10 * * ? *---------------每天上午10:15 触发
0 15 10 * * ? 2005---------------在2005年中的每天上午10:15 触发
0 * 14 * * ?---------------每天在下午2:00至2:59之间每分钟触发一次
0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和2:44时触发
0 15 10 ? * MON-FRI---------------从星期一至星期五的每天上午10:15触发
0 15 10 15 * ?---------------在每个月的每15天的上午10:15触发
0 15 10 L * ?---------------在每个月的最后一天的上午10:15触发
0 15 10 ? * 6L---------------在每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6L 2002-2005---------------在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ?---------------在每个11月11日的上午11:11时触发.­



Cron 表达式包括以下 7 个字段:

  ·秒

  ·分

  ·小时

  ·月内日期

  ·月

  ·周内日期

  ·年(可选字段)

  Cron 触发器利用一系列特殊字符,如下所示:

  ·反斜线(/)字符表示增量值。例如,在秒字段中“5/15”代表从第 5 秒开始,每 15 秒一次。

  ·问号(?)字符和字母 L 字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。所以,如果指定月内日期,可以在周内日期字段中插入“?”,表示周内日期值无关紧要。字母 L 字符是 last 的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果“L”单独存在,就等于“7”,否则代表当月内周内日期的最后一个实例。所以“0L”表示安排在当月的最后一个星期日执行。

  ·在月内日期字段中的字母(W)字符把执行安排在最靠近指定值的工作日。把“1W”放在月内日期字段中,表示把执行安排在当月的第一个工作日内。

  ·井号(#)字符为给定月份指定具体的工作日实例。把“MON#2”放在周内日期字段中,表示把任务安排在当月的第二个星期一。

  ·星号(*)字符是通配字符,表示该字段可以接受任何可能的值。

posted on 2010-07-14 10:03 rogerfan 阅读(814) 评论(1)  编辑  收藏 所属分类: 【开源技术】

Feedback

# re: 【转】spring定时任务之quartz 2014-11-25 12:15 zuidaima
java quartz定时任务demo教程源代码下载:http://zuidaima.com/share_topic/k%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6-p1-s1.htm  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: