通过spring 中的@Scheduled来执行定时任务

  以前开发定时任务的功能的时候,是框架里面写好的quartz配置方式,由于功力尚浅,感觉定时跑披定时任务什么的云里雾里,很高大上,每次都不知道怎么修改配置,后来几次接触定时任务发现,还是比较好掌握的,特别是@Scheduled形式的,很容易上手。

  

直接上代码applicationContext-task.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:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/fex   
        http://www.springframework.org/schema/fex/spring-fex-1.5.xsd   
        http://www.springframework.org/schema/task    
        http://www.springframework.org/schema/task/spring-task-3.0.xsd    
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config /> 
    <context:component-scan base-package="com.minzhou.test" />  
    <task:annotation-driven proxy-target-class="true" />
</beans>

对应定时类为

package com.minzhou.test;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Test {
    
    @Scheduled(cron = "0/5 * * * * ?")
    public void testTask(){
        System.out.println("定时执行任务");
    }
}

对应的web.xml为

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-task.xml</param-value>
    </context-param>


    <listener>
        <description>spring监听器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



        
    


    
</web-app>

这个示例很只是一个检测定时功能的示例,过程为——创建一个web project ,然后编辑web.xml 引用到applicationContext-task.xml,在applicationContext-task.xml里面编辑定时配置,然后写个定时类,就改三个文件,运行起来就可以了。

对应cron示例

0 0 12 * * ? 每天12点触发
0 15 10 ? * * 每天10点15分触发
0 15 10 * * ? 每天10点15分触发
0 15 10 * * ? * 每天10点15分触发
0 15 10 * * ? 2005 2005年每天10点15分触发
0 * 14 * * ? 每天下午的 2点到2点59分每分触发
0 0/5 14 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发)
0 0/5 14,18 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发) 每天下午的 18点到18点59分(整点开始,每隔5分触发)
0 0-5 14 * * ? 每天下午的 2点到2点05分每分触发
0 10,44 14 ? 3 WED 3月分每周三下午的 2点10分和2点44分触发
0 15 10 ? * MON-FRI 从周一到周五每天上午的10点15分触发
0 15 10 15 * ? 每月15号上午10点15分触发
0 15 10 L * ? 每月最后一天的10点15分触发
0 15 10 ? * 6L 每月最后一周的星期五的10点15分触发
0 15 10 ? * 6L 2002-2005 从2002年到2005年每月最后一周的星期五的10点15分触发
0 15 10 ? * 6#3 每月的第三周的星期五开始触发
0 0 12 1/5 * ? 每月的第一个中午开始每隔5天触发一次
0 11 11 11 11 ? 每年的11月11号 11点11分触发
原文地址:https://www.cnblogs.com/minzhousblogs/p/5626522.html