spring 调度器的使用

一、使用调度器需要的jar包(不包括使用spring的包)

commons-collections-3.1.jar

org.springframework.context.support-3.1.1.RELEASE.jar

org.springframework.transaction-3.1.1.RELEASE.jar

org.springframework.web-3.1.1.RELEASE.jar

quartz-all-1.8.6.jar

二、编写一个Trigger类

package com.bdqn.triggerTime.service;


import java.util.Date;


import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;



//继承QuartzJobBean类
public class TimerTigger extends QuartzJobBean{
public TimerTigger(){
System.out.println("this is tigger");
}


@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("执行调度"+new Date());

}


}


三、配置applicationContext.xml信息

<!-- <context:component-scan base-package="com.bdqn.triggerTime"></context:component-scan> -->
<!-- 
1.在bean文件中配置实例化TimerTigger的代码
注意下面并没有直接声明一个TimerTigger而是声明了一个jobDetailBean
这是使用quartz的特点,jobDetailBean是Quartz的org.quarts.JobDetail的一个子类
他要求通过jobClass属性来设置一个job对象
-->
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean" >
<property name="jobClass">
<value>
com.bdqn.triggerTime.service.TimerTigger
</value>
</property>
</bean>
<!--
2.调度工作Quartz的org.quarts.Trigger类描述了合适及以怎样的频度运行一个Quartz工作,Spring提供了两个出发器:
SimpleTriggerBean和CronTriggerBean
A.SompleTriggerBean用来指定一个工作应该以怎样的频度运行,以及(可选的)在第一次运行工作之前应该等待多久
例如:要生成报表工作每24小时运行一次,第一次在1小时之后开始运行,该触发器只能指定工作执行的频度
B、CronTriggerBean 能更精确的控制任务的运行时间,例如:每天早晨6点发送邮件等
 -->
<bean id="simpleTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetailBean"></property>
<property name="startDelay" value="5000"></property>
<property name="repeatInterval" value="3000"></property>
</bean>
<!--  
4.启动工作
使用Spring的SchedulerFatoryBean类
按照如下方式在Spring配置文件中声明它
-->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTriggerBean"></ref>
</list>
</property>
</bean>

</beans>


四、配置web.xml信息

//自动加载applicationContext.xml配置文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
//配置监听
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值