您的位置:首页 > 编程语言 > Java开发

spring 调度器的使用

2015-11-12 15:42 197 查看
一、使用调度器需要的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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: