Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 262|回复: 0

[默认分类] spring定时任务(方便轻巧)

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-13 10:00:05 | 显示全部楼层 |阅读模式


    spring提供了定时任务功能,不需要第三方jar包支持,spring足以。
    代码:
      
    1. package com.inth.product.web.task;
    2. import java.util.Date;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.scheduling.annotation.Scheduled;
    5. import org.springframework.stereotype.Component;
    6. import com.inth.product.service.impl.ContractServiceImpl;
    7. @Component("changeStateTask")
    8. public class ChangeStateTask{
    9.        
    10.         @Autowired
    11.         private ContractServiceImpl contractServiceImpl;
    12.         /**
    13.          * 定时更改合同状态
    14.          * 0 0 * * * ?  表示每次秒和分为0时触发一次(每小时一次)
    15.          * "@Scheduled"时spring提供的定时任务标签
    16.          * 需要在applicationContext.xml中添加
    17.          * xmlns:task="http://www.springframework.org/schema/task"
    18.          * xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "
    19.          *         <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    20.          *  <task:scheduler id="qbScheduler" pool-size="10"/
    21.          * ChangeStateTask.doJob()<BR>
    22.          * <P>Author : DingYinLong </P>  
    23.          * <P>Date : 2014年7月28日 </P>
    24.          */
    25.         @Scheduled(cron = "0 0 * * * ?")
    26.         public void doJob(){
    27.                 this.contractServiceImpl.executeStateChange();
    28.         }
    29.         /**
    30.          * 固定每分钟执行一次
    31.          * ChangeStateTask.doJob1()<BR>
    32.          * <P>Author : DingYinLong </P>  
    33.          * <P>Date : 2014年8月1日 </P>
    34.          */
    35.         @Scheduled(fixedRate = 60*1000)
    36.         public void doJob1(){
    37.                 System.out.println(new Date() + "-----------------doJob1");
    38.         }
    39.         /**
    40.          * 上次任务结束后一分钟后再次执行
    41.          * ChangeStateTask.doJob2()<BR>
    42.          * <P>Author : DingYinLong </P>  
    43.          * <P>Date : 2014年8月1日 </P>
    44.          */
    45.         @Scheduled(fixedDelay = 60*1000)
    46.         public void doJob2(){
    47.                 System.out.println(new Date() + "-----------------doJob2");
    48.         }
    49. }
    复制代码


      
    applicationContext.xml配置文件:


    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.         xmlns:context="http://www.springframework.org/schema/context"  
    4.     xmlns:task="http://www.springframework.org/schema/task"
    5.         xsi:schemaLocation="
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    8.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
    9.         <!-- 扫描包基础目录 -->
    10.     <context:component-scan base-package="com.inth" />
    11.     <!-- 识别@Scheduled注解 -->
    12.         <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    13.     <task:scheduler id="qbScheduler" pool-size="10"/>
    14. </beans>
    复制代码

    注意事项:
      
    1,beans 属性加上xmlns:task="http://www.springframework.org/schema/task"以及xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "
    2,fixedRate和fixedDelay的区别写在注释上了。




    以上情况不基于注解纯配置如下:
    代码:
      
    1. package com.inth.product.web.task;
    2. import java.util.Date;
    3. import com.inth.product.service.impl.ContractServiceImpl;
    4. public class ChangeStateTask{
    5.         private ContractServiceImpl contractServiceImpl;
    6.         public void doJob(){
    7.                 System.out.println(new Date() + "-----------------doJob");
    8. //                this.contractServiceImpl.executeStateChange();
    9.         }
    10.         public void doJob1(){
    11.                 System.out.println(new Date() + "-----------------doJob1");
    12.         }
    13.         public void doJob2(){
    14.                 System.out.println(new Date() + "-----------------doJob2");
    15.         }
    16. }
    复制代码
    applicationContext.xml配置:
      
      
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.         xmlns:context="http://www.springframework.org/schema/context"  
    4.     xmlns:task="http://www.springframework.org/schema/task"
    5.         xsi:schemaLocation="
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    8.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
    9.         <!-- 扫描包基础目录 -->
    10.     <context:component-scan base-package="com.inth" />
    11.     <bean name="taskJob" class="com.inth.product.web.task.ChangeStateTask"></bean>
    12.     <task:scheduled-tasks>   
    13.         <task:scheduled ref="taskJob" method="doJob" cron="0/5 * * * * ?"/>  
    14.         <task:scheduled ref="taskJob" method="doJob1" fixed-rate="5000"/>  
    15.         <task:scheduled ref="taskJob" method="doJob2" fixed-delay="5000"/>   
    16.         </task:scheduled-tasks>
    17. </beans>
    复制代码

    附:cron表达式配置说明





    字段 允许值 允许的特殊字符

    秒 0-59 , - * /

    分 0-59 , - * /

    小时 0-23 , - * /

    日期 1-31 , - * ? / L W C

    月份 1-12 或者 JAN-DEC , - * /

    星期 1-7 或者 SUN-SAT , - * ? / L C #

    年(可选) 留空, 1970-2099 , - * /

    表达式 意义

    "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期间的每1分钟触发

    "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

    "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

    "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

    "0 10,44 14 ? 3 WED" 每年三月的星期三的下午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" 每月的第三个星期五上午10:15触发

    特殊字符 意义

    * 表示所有值;

    ? 表示未说明的值,即不关心它为何值;

    - 表示一个指定的范围;

    , 表示附加一个可能值;

    / 符号前表示开始时间,符号后表示每次递增的值;




      
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 09:56 , Processed in 0.443888 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表