美文网首页
Spring + Quartz 定时任务

Spring + Quartz 定时任务

作者: Made0107 | 来源:发表于2018-03-20 15:22 被阅读0次

1. JAVA代码

package quartz.task;

public class TaskTest {

    private int count = 0;
    
    public void execute() {
        
        count++;
        
        System.out.println("TaskTest : " + count);
        System.out.println();
    }
}

2. 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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">


    <!-- 定时任务类 -->
    <bean id="taskTest" class="quartz.task.TaskTest"></bean>

    <!-- 执行的方法 -->
    <bean id="jobTest"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 将定时任务类 注入其中 -->
        <property name="targetObject" ref="taskTest"></property>
         <!-- 要执行的方法名称 -->  
        <property name="targetMethod">  
            <value>execute</value>
        </property>
    </bean>

    <!-- 调度 触发器 -->
    <bean id="testJobTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobTest"></property>
        <property name="cronExpression">
            <value>* */1 * * * ?</value>
        </property>
    </bean>

    <!-- 调度  工厂 --> 
    <bean id="SpringJobSchedulerFactoryBean"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="testJobTrigger" />
            </list>
        </property>
    </bean>

</beans>

2. 运行结果

执行结果.PNG

相关文章

网友评论

      本文标题:Spring + Quartz 定时任务

      本文链接:https://www.haomeiwen.com/subject/nnlbqftx.html