spring 定时任务例子

时间:2023-02-15 07:49:55
作业类
package com.cn.task;

import java.text.SimpleDateFormat;
import java.util.Date;


public class TaskService {
public void taskTestInterface(){
System.out.println("定时任务开启。。。。。"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}


web.xml最简单的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<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>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--配置调度工厂  --> <bean name="testScheduler"  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   <!-- 在SchedulerFactoryBean初始化完成后,延迟10秒启动testScheduler,以便让Spring能够更快初始化容器中剩余的Bean。-->    <property name="startupDelay" value="10"/> <!-- scheduler by QuartzAdjust --> <property name="triggers"><list><ref bean="taskTest"/></list></property></bean>  <!-- CronTriggerBean调度器:每到指定时间则触发一次。与之相对的还有SimpleTriggerBean调度器,每隔指定时间则触发一次。--> <!-- <bean id="taskTest" class="org.springframework.scheduling.quartz.CronTriggerBean"> --><bean id="taskTest" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  <!-- 对应的JobDetail -->  <property name="jobDetail" ref="taskTestIn"></property><!-- Cron表达式  每隔一分钟执行一次-->   <property name="cronExpression">      <value>0 * * * * ? </value>   </property></bean><!-- 要调用的工作类 -->  <!-- Spring提供了一个MethodInvokingJobDetailFactoryBean,通过这个FactoryBean可以将Spring容器中Bean的方法包装成Quartz任务,这样开发者就不必为Job创建对应的类。 --><bean id="taskTestIn" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="TaskService" /><property name="targetMethod" value="taskTestInterface" /><property name="concurrent" value="false"></property></bean>  <bean id="TaskService" class="com.cn.task.TaskService"/></beans>


SchedulerFactoryBean是Spring继承Quartz而提供的类。
Scheduler和Spring容器的生命周期相关联。
Spring容器启动后,Scheduler自动开始工作,而在Spring容器关闭前,自动关闭Scheduler。
Scheduler在Spring容器的生命周期完成自动启动和关闭的操作。
SchedulerFactoryBean功能:
 1.以更具Bean风格的方式为Scheduler提供配置信息。
 2.让Scheduler和Spring容器的生命周期建立关联,相生相息。
 3.通过属性配置部分或全部代替Quartz自身的配置文件。
参数解释:
 1.dataSource: 当需要使用数据库来持久化任务调度数据时,你可以在Quartz中配置数据源,
也可以直接在Spring中通过dataSource指定一个Spring管理的数据源。如果指定了该属性,即使quartz.properties中
已经定义了数据源,也会被此dataSource覆盖。
 2.startupDelay:在SchedulerFactoryBean初始化完成后,
延迟启动Scheduler,以便让Spring能够更快初始化容器中剩余的Bean。
 3.quartzProperties:类型为Properties,允许你在Spring中定义Quartz的属性。
其值将覆盖quartz.properties配置文件中的设置,这些属性必须是Quartz能够识别的合法属性。在配置时你可以需要查看Quartz的相关文档。
 4.transactionManager:可以通过该属性设置一个Spring事务管理器。
在设置dataSource时,Spring强烈推荐你使用一个事务管理器,否则数据表锁定可能不能正常工作

 
配置作业调度的触发方式(触发器)  Quartz的作业触发器有两种,分别是org.springframework.scheduling.quartz.SimpleTriggerBeanorg.springframework.scheduling.quartz.CronTriggerBean

 
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>

CronTriggerBean  是spring3.0内置Quartz版本是<2.0的  如果使用>2.0的Quartz版本  可能会有如下异常
Caused by: java.lang.IncompatibleClassChangeError: 
  class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
 因为这个导致异常时的解决方法:
1.降低Quartz版本,降到1.X去。
2.升级Spring版本到3.1+,根据Spring的建议,将原来的**TriggerBean替换成**TriggerFactoryBean,
例如CronTriggerBean 就可以替换成 CronTriggerFactoryBean。替换之后问题解决。



spring 定时任务例子
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false"
>
<context:annotation-config />
<!--spring扫描注解的配置 -->
<context:component-scan base-package="com.cn.task" />

<!--开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>


配置作业调度的触发方式(触发器)使用一种即可  
spring task 方式  采用注解更方便
在<pre name="code" class="java">applicationContext.xml中添加 引用

task任务类

package com.cn.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("taskJob")
public class AnnotateTask {
@Scheduled(cron="15/30 * * * * ?")
public void run(){
System.out.println("task定时任务启动:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}