SpringBoot整合定时任务和异步任务处理

SpringBoot定时任务schedule讲解
   

        1、常见定时任务 Java自带的java.util.Timer类
            timer:配置比较麻烦,时间延后问题
            timertask:不推荐

        2、Quartz框架
            配置更简单
            xml或者注解

        3、SpringBoot使用注解方式开启定时任务
            1)启动类里面 @EnableScheduling开启定时任务,自动扫描
            2)定时任务业务类 加注解 @Component被容器扫描
            3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次
 

2、SpringBoot常用定时任务配置实战
    简介:SpringBoot常用定时任务表达式配置和在线生成器

        1、cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
            1)crontab 工具  https://tool.lu/crontab/
            
                    Linux
        *    *    *    *    *    *
        -    -    -    -    -    -
        |    |    |    |    |    |
        |    |    |    |    |    + year [optional]
        |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
        |    |    |    +---------- month (1 - 12)
        |    |    +--------------- day of month (1 - 31)
        |    +-------------------- hour (0 - 23)
        +------------------------- min (0 - 59)

        Java(Spring)
        *    *    *    *    *    *    *
        -    -    -    -    -    -    -
        |    |    |    |    |    |    |
        |    |    |    |    |    |    + year [optional]
        |    |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
        |    |    |    |    +---------- month (1 - 12)
        |    |    |    +--------------- day of month (1 - 31)
        |    |    +-------------------- hour (0 - 23)
        |    +------------------------- min (0 - 59)
        +------------------------------ second (0 - 59)
        
        参考博客:https://www.cnblogs.com/mingyue1818/p/5764050.html
        举几个例子:

        0 0  2  1 *  ? *  表示在每月的1日的凌晨2点调度任务     
        0 15 10 ? *  MON-FRI 表示周一到周五每天上午10:15执行作业             
        0 15 10 ? 6L 2002-2006 表示200-2006年的每个月的最后一个星期五上午10:15执行作业 
        
        2、fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
        @Component
        public class TestTask {
  
            @Scheduled(fixedRateString="2000")
            public void test() throws InterruptedException {
                Thread.sleep(4000L);
                System.out.println(new Date());
            }
        }
        每4秒执行一次
        3、fixedDelay: 上一次执行结束时间点后xx秒再次执行
        @Component
        public class TestTask {
          
            @Scheduled(fixedDelay=2000)
            public void test() throws InterruptedException {
                Thread.sleep(4000L);
                System.out.println(new Date());
            }
        }
        每6秒执行一次
        4、fixedDelayString:  字符串形式,可以通过配置文件指定

=========================================================

另外一种方式:

再启动类上加注解:

@EnableScheduling  开启定时任务
@SpringBootApplication
@EnableDubboConfig
@EnableScheduling
@DubboComponentScan("com.xdclass.userapp.service.dubbo")
@MapperScan("com.xdclass.couponapp.mapper")
public class CouponAppApplication {

   public static void main(String[] args) {
      SpringApplication.run(CouponAppApplication.class, args);
   }

}

 

写一个需要执行定时任务的类:每5秒执行该方法

package com.xdclass.couponapp.service.shedule;

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

@Service
public class HelloShedule {

    @Scheduled(cron = "0/5 * * * * ?")
    public void hello(){
        System.out.println("enter say hello!");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值