springboot之定时任务

摘要:
说到定时任务,JDK附带的定时线程通常被认为是执行定时任务。查看定时线程池。publicstaticScheduledExecutorServicenewScheduledThreadPool{returnnewScheduledTthreadPoolExecutor;}publicstaticScheduledExecutorServicenewScheduledThreadPool{returnnewScheduledTthreadPoolExecutor;}两种常用的方法:scheduleAtFixedRate:以固定频率执行任务。周期是指成功执行每个任务之间的间隔。

定时线程

说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务。
回顾一下定时线程池。

public static ScheduledExecutorService newScheduledThreadPool(int var0) {
        return new ScheduledThreadPoolExecutor(var0);
    }

    public static ScheduledExecutorService newScheduledThreadPool(int var0, ThreadFactory var1) {
        return new ScheduledThreadPoolExecutor(var0, var1);
    }

常用的两个方法:
scheduleAtFixedRate:是以固定的频率去执行任务,周期是指每次执行任务成功执行之间的间隔。

schedultWithFixedDelay:是以固定的延时去执行任务,延时是指上一次执行成功之后和下一次开始执行的之前的时间。

看一个DEMO:

public class ScheduledExecutorServiceDemo {
    public static void main(String args[]) {

        ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(4000);
                    System.out.println(Thread.currentThread().getId() + "执行了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 0, 2, TimeUnit.SECONDS);
    }
}

具体细节我就不再赘述了,有兴趣的可以查看我关于线程池的博客:链接

springboot的定时任务

pom的依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

启动类启用定时

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class StartApplication {
    public static void main(String args[]){
        SpringApplication application = new SpringApplication(StartApplication.class);
        application.run(args);
    }
}

定时任务业务类:

package com.schedule;

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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class ScheduleTask {

    private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

    private AtomicInteger count = new AtomicInteger();

    @Scheduled(fixedRate = 6000)
    public void  reportTime(){
        System.out.println("现在的时间是:"+format.format(new Date()));
    }

    /**
     * 以固定的频率去执行任务
     */
    @Scheduled(initialDelay = 10000,fixedRate = 3000)
    public void  reportNumber(){
        System.out.println(count.incrementAndGet());
    }

    /**
     * 以固定的延时去执行任务
     */
    @Scheduled(initialDelay = 10000,fixedDelay = 3000)
    public void  reportNumberDelay(){
        System.out.println(count.incrementAndGet());
    }
}

运行结果如下:

现在的时间是:09:59:57
1
2
现在的时间是:10:00:03
3
4
5
6
现在的时间是:10:00:09
7

使用说明:

  • @Scheduled(fixedRate = 1000) :上一次开始执行时间点之后1秒再执行
  • @Scheduled(fixedDelay = 1000) :上一次执行完毕时间点之后1秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
    @Scheduled(initialDelay=1000, fixedDelay=6000) :第一次延迟1秒后执行,之后按fixedDelay的规则每6秒执行一次

源码地址

免责声明:文章转载自《springboot之定时任务》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇图解ARP协议(二)ARP攻击原理与实践Android程序对不同手机屏幕分辨率自适应的总结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

IntelliJ IDEA 版本控制(svn、git) 修改文件后,所属目录的颜色也变化

IntelliJIDEA的版本控制默认文件已被修改,目录的颜色不会改变,这很不方便。例如,修改方法如下:文件---&gt;设置--&gt;版本控制--&gt;选中“显示更改内容的目录”,效果如下:...

git:将两个请求合并为一个请求

Gitrebase ihEAD~2解释:此命令可以以文本形式显示您提交的两次请求。如果数字2被4替换,则您最近四次提交的信息将显示如下:1 pick56a06efchange1:删除一个空白行2 pickedbeab5change2:addlogonMainActivity34#Rebase23198ba..Edbeab5onto23198ba5#6#命令:...

mini.DataGrid使用说明

√√√ ajaxOptionsObjectajax配置对象。√√√ idFieldString是行数据的唯一字段。设置为“client”之后,客户端将排序√√√√ totalCountNumber记录总数√√√ defaultColumnWidthNumber默认列宽100√√√√ showColumnsBoolean显示标头true√√√√ showPag...

MySQL锁详解

MySQL锁详解update语句执行流程MySQL的锁介绍按照锁的粒度来说,MySQL主要包含三种类型(级别)的锁定机制:全局锁:锁的是整个database。由MySQL的SQLlayer层实现的表级锁:锁的是某个table。由MySQL的SQLlayer层实现的行级锁:锁的是某行数据,也可能锁定行之间的间隙。...

ArcGIS Server服务状态正在停止。。。问题BUG解决

1、 ArcGISServer服务器存在服务问题:ArcGISServerManager的所有服务都显示为“正在停止…”。N个解决方案1)港口占用问题。端口4000-4002已被其他程序占用,导致服务无法正常启动。您可以通过CMD找到相关PID占用的端口,停止它,然后启动GIS服务。您也可以在安装目录中将其手动更改为D:ProgramFiles ArcGIS...

谷歌浏览器插件安装、VIP看视频、解除百度网盘限速

谷歌浏览器的插件主要由石油猴子获得。为了安装油猴,您需要先安装Google Access Assistant。utm_Source=chrome ntp图标建议使用几个视频下载插件https://jingyan.baidu.com/article/49711c61b19dd5fa441b7ccd.html两个插件“百度通用网盘助手”、“网盘直链下载助手”和一...