Spring3的quartz定时任务

要使用定时任务,需要将quartz-1.5.2.jar加入lib,没有的话可以从下面地址下载:

quartz-1.5.2.jar

 

有了这个再做个配置文件appctx-quartz.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="schedulerFactory"    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local="cronTriggerTest" />
            </list>
        </property>
    </bean> 
    
    <bean id="cronTriggerTest" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobTest" />
        <property name="cronExpression">
                <value>0 17 21 ? * SAT</value> <!-- FORMAT:second minute hour ? * Weekday -->
        </property>
    </bean>    
    
    <bean id="jobTest"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="claimScheduler" />
        <property name="targetMethod" value="print" />
    </bean>
    
    <bean id="claimScheduler" class="com.ufo.unknown.quartz.Test">
    </bean>
</beans>
复制代码

被调用的类如下:

package com.ufo.unknown.quartz;


public class Test{
    public void print(){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}

然后在周六的21点17分0秒,Test类的print函数就被调用了,控制台输出如下:

DEBUG (JobRunShell.java:202) - Calling execute on job DEFAULT.jobTest
DEBUG (CachedIntrospectionResults.java:222) - Getting BeanInfo for class [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob]
DEBUG (CachedIntrospectionResults.java:238) - Caching PropertyDescriptors for class [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob]
DEBUG (CachedIntrospectionResults.java:250) - Found bean property 'class' of type [java.lang.Class]
DEBUG (CachedIntrospectionResults.java:250) - Found bean property 'methodInvoker' of type [org.springframework.util.MethodInvoker]
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

posted @   逆火狂飙  阅读(1654)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2013-11-29 【Canvas与艺术】蓝底金菊花桌面
2013-11-29 【Canvas与电脑桌面】用六角回旋镖铺满一个平面(1920*1080)
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示