Quartz之入门实例

Posted zengnansheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz之入门实例相关的知识,希望对你有一定的参考价值。

http://www.quartz-scheduler.org/

项目中导入quartz包

 

新建2个job类

package com.zns.test.quartz;

public class Job1 {
    public void work() {
        System.out.println("job1 work...");
    }
}
package com.zns.test.quartz;

public class Job2 {
    public void work() {
        System.out.println("job2 work...");
    }
}

 

 

spring xml配置

<!-- quartz job -->
    <bean id="job1" class="com.zns.test.quartz.Job1"></bean>
    <bean id="job2" class="com.zns.test.quartz.Job2"></bean>
    <!-- quartz job detail -->
    <bean id="jobdetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="job1"></property>
        <property name="targetMethod" value="work"></property>
        <property name="concurrent" value="false"></property>
    </bean>
    <bean id="jobdetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="job2"></property>
        <property name="targetMethod" value="work"></property>
        <property name="concurrent" value="false"></property>
    </bean>
    <!-- quartz trigger -->
    <bean id="trigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobdetail1"></property>
        <!-- 每5秒执行一次 -->
        <property name="cronExpression" value="*/5 * * * * ?"></property>
    </bean>
    <bean id="trigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobdetail2"></property> 
         <!-- 每5秒执行一次 -->
        <property name="cronExpression" value="*/5 * * * * ?"></property>
    </bean>
    <!-- quartz schedulerFactory -->
    <bean id="quartzscheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="trigger1"></ref>
                <ref bean="trigger2"></ref>
            </list>
        </property>
        <property name="autoStartup" value="true"></property>
    </bean>

 

 

启动运行项目即可看到每隔一段时间执行了方法。

 

以上是关于Quartz之入门实例的主要内容,如果未能解决你的问题,请参考以下文章

Quartz的基本使用之入门(2.3.0版本)

quartz定时任务框架之实例

Spring Boot 入门:集成Quartz定时任务

Quartz 之 JobExecutionContext 解读

Quartz任务调度概念例析快速入门

quartz入门