Spring定时器的两种实现方式

51 篇文章 0 订阅

一.Spring定时器的两种实现方式:

1.     继承java.util.TimerTask

2.     继承org.springframework.scheduling.quartz.QuartzJobBean

 

下面详细说明两种方式实现的过程

在开始说明前先建立一个web工程SpringTiming然后导入需要用到的包


a.    TimerTask定时(用TimerTask来实现定时只能设置定时器以怎样一个频率(也就是多久运行一次)运行而不能设定某个时间点来运行定时器)

1.    建一个类TestTimerTask继承TimerTask类实现run方法,这个类的run方法里的代码在定时器触发时执行


package com.SpringTiming;

import java.util.TimerTask;

publicclass TestTimerTaskextends TimerTask {

    @Override

    publicvoid run() {

        System.out.println("=====执行TimerTask定时成功=====");

    }

}

 

2. 建一个xml文件applicationContext.xml用来配置定时器


<?xmlversion="1.0"encoding="UTF-8"?>

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

</beans>

 

新增一个bean

<beanid="testScheduledTimerTask"class="org.springframework.scheduling.timer.ScheduledTimerTask">

<!--此处必须是注入方式,不允许直接写类路径,否则会出现转换类型错误-->

    <propertyname="timerTask"value="com.SpringTiming.TestTimerTask"/>

        <property name="period">

            <value>10000</value>

        </property>

</bean>

其中timerTask属性是用来告知ScheduledTimerTask执行那个类

,period属性是用来设置定时器运行的间隔时间(也就是调用TestTimerTask.run()方法的间隔时间),其中的1000代表1000毫秒。

 

再添加一个负责启动定时器的bean

<beanid="testTimerFactoryBean"class="org.springframework.scheduling.timer.TimerFactoryBean">

        <property name="scheduledTimerTasks">

            <list>

                <ref bean="testScheduledTimerTask"/>

            </list>

        </property>

</bean>

其中的scheduledTimerTask属性值用来引入需要定时触发的执行的bean

 

3.再添加一个类(TestApp.java)用来调用applicationContext.xml文件

TestApp.java

package com.TimingRun;

import org.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclass TestApp {

    publicstaticvoid main(String[] args) {

        // TODO Auto-generated method stub

        ApplicationContext context =newClassPathXmlApplicationContext("applicationContext.xml");

    }

}

 

b. quartz定时(可以用SimpleTriggerBean设置一个频率时间来运行定时器,类似于TimerTask;还可以用CronTriggerBean来设置一个固定的时间点来运行定时器)

1.新建一个类TestQuartzJobBean.java继承QuartzJobBean抽象类

TestQuartzJobBean.java

package com.SpringTiming;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

publicclass TestQuartzJobBeanextends QuartzJobBean {

    @Override

    protectedvoid executeInternal(JobExecutionContext arg0)

            throws JobExecutionException {

        System.out.println("=============执行quartz定时任务成功===========");

    }  

}

其中executeInternal这个方法里的代码是定时器触发时用来执行的

 

2. 在xml文件中配置定时器

<beanid="testJobDetailBean"class="org.springframework.scheduling.quartz.JobDetailBean">

    <propertyname="jobClass"value="com.SpringTiming.TestQuartzJobBean"/>

    <!--

    <propertyname="jobDataAsMap">

        <map>

            <entrykey="courseService">

                <ref bean="courseService"/>

            </entry>

        </map>

    </property>

     -->

</bean>

其中的jobClass属性是用来设置定时器调用的类;jobDataAsMap属性用来设置定时器调用的类的属性,当JobDetailBean实例化时,它会将courseService Bean注入到TestQuartzJobBean的courseService属性中。

 

Spring提供了两个触发器一个用来设置定时器何时运行另一个用来设置以怎样的频率运行分别是:SimpleTriggerBean和CronTriggerBean。

SimpleTriggerBean与ScheduledTimerTask类似都可以用来设置定时器以怎样的频率运行,配置如下:

<beanid="testSimpleTriggerBean"class="org.springframework.scheduling.quartz.SimpleTriggerBean">

    <propertyname="jobDetail"ref="testJobDetailBean"/>

    <propertyname="repeatInterval"value="10000"/>

</bean>

其中的jobDetail属性用来设置定时器调用的Bean;repeatInterval属性用来设置定时器触发的频率(也就是多久触发一次定时器),1000代表1000毫秒。

 

再用SchedulerFactoryBean来启动定时器:

<beanid="simpleSchedulerFactoryBean"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

    <propertyname="triggers">

        <list>

            <ref bean="testSimpleTriggerBean"/>

        </list>

    </property>

</bean>

其中的triggers属性用来引用需要执行怎样频率的定时器bean。

 

CronTriggerBean可以用来设置定时器在某个时间点来运行,配置如下:

<beanid="testCronTriggerBean"class="org.springframework.scheduling.quartz.CronTriggerBean">

    <propertyname="jobDetail"ref="testJobDetailBean"/>

    <propertyname="cronExpression"value="0 2217 * * ?"/>

</bean>

其中的jobDetail属性用来设置定时器调用的Bean;cronExpression属性用来设置定时器运行的时间点,设置的规则为:

一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。从左到右:1.秒2.分3.小时4.月份中的日期(1-31)5.月份(1-12或JAN-DEC)6.星期中的日期(1-7或SUN-SAT)7.年份(1970-2099)
每个元素都显示的规定一个值(如6),一个区间(9-12),一个列表(9,11,13)或一个通配符(*)。因为4和6这两个元素是互斥的,因此应该通 过设置一个问号(?)来表明不想设置的那个字段,“/”如果值组合就表示重复次数(10/6表示每10秒重复6次)。

 

然后启动定时器:

<beanid="simpleSchedulerFactoryBean"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

    <propertyname="triggers">

        <list>

            <ref bean="testCronTriggerBean"/>

        </list>

    </property>

</bean>

其中的triggers属性用来引用需要执行怎样间隔时间的定时器bean。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值