Spring + Quartz 定时任务

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容