目录
    • 3、springboot整合

    一、定时任务

    1、cron表达式

    语法:秒 分 时 日 月 周 年

    (其中“年”spring不支持,也就是说在spring定时任务中只能设置:秒 分 时 日 月 周)

    2、cron示例

    3、springboot整合

    @enablescheduling

    @scheduled

    实例:

    package com.xunqi.gulimall.seckill.scheduled;
    import lombok.extern.slf4j.slf4j;
    import org.springframework.scheduling.annotation.async;
    import org.springframework.scheduling.annotation.scheduled;
    import org.springframework.stereotype.component;
    import java.util.concurrent.timeunit;
    /**
     * @description:
     * @created: with intellij idea.
     * @author: 夏沫止水
     * @createtime: 2020-07-09 18:49
     **/
    /**
     * 定时任务
     *      1、@enablescheduling 开启定时任务
     *      2、@scheduled开启一个定时任务
     *
     * 异步任务
     *      1、@enableasync:开启异步任务
     *      2、@async:给希望异步执行的方法标注
     */
    @slf4j
    @component
    @enablescheduling
    public class helloscheduled {
        /**
         * 1、在spring中表达式是6位组成,不允许第七位的年份
         * 2、在周几的的位置,1-7代表周一到周日
         * 3、定时任务不该阻塞。默认是阻塞的
         *      1)、可以让业务以异步的方式,自己提交到线程池
         *              completablefuture.runasync(() -> {
         *         },execute);
         *
         *      2)、支持定时任务线程池;设置 taskschedulingproperties
         *        spring.task.scheduling.pool.size: 5
         *
         *      3)、让定时任务异步执行
         *          异步任务
         *
         *      解决:使用异步任务 + 定时任务来完成定时任务不阻塞的功能
         *
         */
         @scheduled(cron = "*/1 * * * * ?")
         public void hello() {
             log.info("hello...");
             try { timeunit.seconds.sleep(3); } catch (interruptedexception e) { e.printstacktrace(); }
         }
    }
    

    定时任务默认是阻塞的线程,也就是说即使你设置成每一秒执行一次,但是方法内部的业务时间需要5秒才能执行完,也会造成定时任务每6秒才能执行一次。

    当然我们可以开启异步线程:

    @enableasync

    @async

    实例:

    package com.xunqi.gulimall.seckill.scheduled;
    import lombok.extern.slf4j.slf4j;
    import org.springframework.scheduling.annotation.async;
    import org.springframework.scheduling.annotation.scheduled;
    import org.springframework.stereotype.component;
    import java.util.concurrent.timeunit;
    /**
     * @description:
     * @created: with intellij idea.
     * @author: 夏沫止水
     * @createtime: 2020-07-09 18:49
     **/
    /**
     * 定时任务
     *      1、@enablescheduling 开启定时任务
     *      2、@scheduled开启一个定时任务
     *
     * 异步任务
     *      1、@enableasync:开启异步任务
     *      2、@async:给希望异步执行的方法标注
     */
    @slf4j
    @component
    @enableasync
    @enablescheduling
    public class helloscheduled {
        /**
         * 1、在spring中表达式是6位组成,不允许第七位的年份
         * 2、在周几的的位置,1-7代表周一到周日
         * 3、定时任务不该阻塞。默认是阻塞的
         *      1)、可以让业务以异步的方式,自己提交到线程池
         *              completablefuture.runasync(() -> {
         *         },execute);
         *
         *      2)、支持定时任务线程池;设置 taskschedulingproperties
         *        spring.task.scheduling.pool.size: 5
         *
         *      3)、让定时任务异步执行
         *          异步任务
         *
         *      解决:使用异步任务 + 定时任务来完成定时任务不阻塞的功能
         *
         */
         @async
         @scheduled(cron = "*/1 * * * * ?")
         public void hello() {
             log.info("hello...");
             try { timeunit.seconds.sleep(3); } catch (interruptedexception e) { e.printstacktrace(); }
         }
    }
    

    这样就会开启异步线程,并且是非阻塞线程,因为每次都会开启一个线程来执行,我们可以看一下源码配置的截图,这个就是异步执行的默认配置,核心线程数是8,最大线程数是无限大,这时如果一直每秒执行一次,则会造成服务器资源耗尽。

    当然,我们可以在配置文件中进行定时任务线程池的设定:

    #核心线程数

    spring.task.execution.pool.core-size=20

    #最大线程数

    spring.task.execution.pool.max-size=50

    #队列大小

    spring.task.execution.pool.queue-capacity=10000

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!