spring定时器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
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文件


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ":/spring-beans.dtd">
-->
<beans>
<bean id="businessReport" class="task.BusinessReport" />

<bean name="reportTask"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="task.BusinessReport" />
</bean>


<!-- 触发器 -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">


<!-- 指向我们的任务 -->
<property name="jobDetail" ref="reportTask" />


<!--  每天下午16点50分到55分,每分钟运行一次 -->
<property name="cronExpression" value="0 50-55 16 * * ?" />
</bean>




<!-- 调度器  -->
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--  触发器列表 -->
<ref bean="cronTrigger" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties" /> 
</bean> 
</beans>




三 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时触发.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值