当前位置:编程学习 > JAVA >>

spring整合quartz

1.定时执行的类

[java]
package thb.quartz; 
 
public class QuartzJob { 
     
    /**
     * 定时执行的方法
     */ 
    public void work() { 
        System.out.println(System.currentTimeMillis() + ">>>执行定时任务。。。"); 
    } 
 

2.quartz配置文件
[java]
<?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:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" 
    default-lazy-init="true"> 
     
    <!-- 要调用执行定时任务的类 --> 
    <bean id="quartzJob" class="thb.quartz.QuartzJob"></bean> 
     
    <!-- 定义调用对象和调用对象的方法(触发器) --> 
    <bean id="jobTask" class="org.springframework.sche易做图ng.quartz.MethodInvokingJobDetailFactoryBean"> 
        <!-- 调用的类 --> 
        <property name="targetObject"> 
            <ref bean="quartzJob"/> 
        </property> 
        <!-- 调用的方法 --> 
        <property name="targetMethod"> 
            <value>work</value> 
        </property> 
    </bean> 
     
    <!-- 定义触发事件(调度器) --> 
    <bean id="doTime" class="org.springframework.sche易做图ng.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="jobTask"></property> 
        <!-- 10点开始,每隔1分钟 --> 
        <property name="cronExpression" value="0 * 10 * * ?"></property> 
    </bean> 
     
    <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.sche易做图ng.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> 
            <list> 
                <ref bean="doTime"/> 
            </list> 
        </property> 
    </bean> 
</beans> 

3.测试代码
[java] www.zzzyk.com
package thb.quartz; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.FileSystemXmlApplicationContext; 
 
public class QuartzTest { 
 
    /**
     * 定时任务测试
     */ 
    public static void main(String[] args) { 
        ApplicationContext ctx = new FileSystemXmlApplicationContext("/resource/spring/quartz.xml"); 
        System.out.println("定时任务开始执行。。。"); 
    } 
 

 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,