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

spring4.0+quartz配置定时任务

2016-05-10 16:22 507 查看
作用简介:

作用我就不说了。这个写给我自己看的,方便以后忘记了进行翻阅,这样的代码网上一搜一堆。

jar架包:

spring4.2.4系列包,quartz2.1.7,slf4j,commons-logging。

(1)web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>quartz1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 加载xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*quartz.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>


(2)quartz.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- - Application context definition for JPetStore's business layer. - Contains
bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml
(see web.xml's "contextConfigLocation"). -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
<!-- 要执行的工作类 -->
<bean id="quartzJob" class="com.test.quartz.JobClass"></bean><!-- 可以有多个,工作类可以继续添加 -->

<!-- 调用工作类中的方法 -->
<bean id="firstJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 要调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 要调用类中的哪个方法 -->
<property name="targetMethod">
<value>firstJob</value>
</property>
</bean>
<!-- 定义上面这个方法的执行周期 -->
<bean id="execTime1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 调用firstJob -->
<property name="jobDetail">
<ref bean="firstJob" />
</property>
<!-- cron表达式  每分钟的03秒时执行 -->
<property name="cronExpression">
<value>3 * * * * ? </value>
</property>
</bean>

<!-- 启动所有的定时任务 -->
<bean id="startAllJob" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="execTime1"/>
<ref bean="execTime2"/>
<!-- 此处继续添加要执行的定时任务 -->
</list>
</property>
</bean>

<!-- 第二个任务 -->
<bean id="twoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 要调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 要调用类中的哪个方法 -->
<property name="targetMethod">
<value>twoJob</value>
</property>
</bean>
<bean id="execTime2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 调用firstJob -->
<property name="jobDetail">
<ref bean="twoJob" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>3 * * * * ? </value>
</property>
</bean>

</beans>
(3)工作类JobClass.java

package com.test.quartz;

import java.util.Date;

public class JobClass {
/**
* 第一个定时任务
*/
public void firstJob(){
System.out.println("执行第一个定时任务,时间是::"+new Date());
System.out.println("当前线程id:"+Thread.currentThread().getId());
}
/**
* 第二个定时任务
*/
public void twoJob(){
System.out.println("执行第二个定时任务,时间是:"+new Date());
System.out.println("当前线程id:"+Thread.currentThread().getId());
}
}


(4)执行结果。

执行第一个定时任务,时间是::Tue May 10 16:31:03 CST 2016

当前线程id:38

执行第二个定时任务,时间是:Tue May 10 16:31:03 CST 2016

当前线程id:39

执行第二个定时任务,时间是:Tue May 10 16:32:03 CST 2016

当前线程id:41

执行第一个定时任务,时间是::Tue May 10 16:32:03 CST 2016

当前线程id:40

结语:没什么含量的代码,仅仅是方便自己以后忘记了可以来看看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: