SSM+Quartz定时器

版权声明:本文由博主原创文章,如需转载请注明出处... https://blog.csdn.net/zc157158/article/details/83824868
  1. 添加maven依赖
<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.2.0</version>
</dependency>
  1. 编写spring-quartz配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!-- 添加调度的任务bean 配置对应的类-->
    <bean id="myQuartz" class="com.zc.quarter.MyQuartz" />
    <!--配置调度具体执行的方法-->
    <bean id="myQuartzDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myQuartz" />
		<property name="targetMethod" value="quartzTest" />
		<!-- 使用Quartz做计划任务时,默认情况下,当前任务总会执行,无论前一个任务是否结束 -->
		<!-- 下面的concurrent属性默认为true,设置成false可以让任务按照顺序执行,上一个任务没有结束,下面的任务不会执行-->
		<property name="concurrent" value="false" />
    </bean>
    <!--配置调度执行的触发的时间-->
    <bean id="myQuartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myQuartzDetail" />
        <property name="cronExpression">
        	<!--每隔10秒执行一次-->
            <value>0/10 * * * * ?</value>
        </property>
    </bean>
    <!-- quartz的调度工厂只能有一个,多个调度任务在list中添加 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="myQuartzTrigger" />
            </list>
        </property>
    </bean>
</beans>

Cron表达式可以自己了解,这里推荐一个在线生成Cron表达式生成器
3. 在web.xml里加载shiro-quartz.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml,spring-quartz.xml</param-value>
</context-param>

或者在spring的主配置文件里加一下代码

<import resource="spring-quartz.xml" />

4.编写Java类

//注意这里的类名要跟spring-quartz.xml配置文件的目标类保持一致
public class MyQuartz{
	private Logger logger = Logger.getLogger(this.getClass());
	//注意这里的方法名要跟spring-quartz.xml配置文件的目标方法保持一致
	public void quartzTest(){
		//内容就是打印一句话
		System.out.println("执行定时任务...");
	}
}

结语:其中concurrent属性标识的解释为:对于相同的Detail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。

那么此处的第二个job到底是被推迟执行了,还是被取消了呢? 今天带着这个疑问做了一个小实验,具体做法如下:将任务的Trigger设置为每10秒钟执行一次,然后在执行的任务体内让Thread暂停15秒钟,这样不同的job之间肯定会有时间上的重叠。当启动程序之后,发现前一个job执行结束的时刻的秒数为15,而且第二个本来在10s就执行的job马上就启动了。

由此可见,concurrent属性实际是将job推迟执行了。
结语转载至:http://nesuk.iteye.com/blog/1582557

猜你喜欢

转载自blog.csdn.net/zc157158/article/details/83824868
今日推荐