spring boot 调度——迹忆客-ag捕鱼王app官网

调度是在特定时间段内执行任务的过程。 spring boot 为在 spring 应用程序上编写调度程序提供了很好的支持。

java cron 表达式

java cron 表达式用于配置 crontrigger 的实例,它是 org.quartz.trigger 的子类。 有关 java cron 表达式的更多信息,我们可以参考此链接 -

@enablescheduling 注解用于为我们的应用程序启用调度程序。 此注解应添加到主 spring boot 应用程序类文件中。

package com.study;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.scheduling.annotation.enablescheduling;
/**
 * @author jiyik.com
 */
@springbootapplication
@enablescheduling
public class myapplication {
    public static void main(string[] args) {
        springapplication.run(myapplication.class, args);
    }
}

@scheduled 注解用于触发特定时间段的调度程序。

@scheduled(cron = "0 * 9 * * ?")
public void cronjobsch() throws exception {
}

下面是一个示例代码,展示了如何从每天上午 9:00 开始到上午 9:59 结束的每分钟执行一次任务

package com.study.scheduler;
import org.springframework.scheduling.annotation.scheduled;
import org.springframework.stereotype.component;
import java.text.simpledateformat;
import java.util.date;
/**
 * @author jiyik.com
 */
@component
public class scheduler {
    @scheduled(cron = "0 * 9 * *  ?")
    public void cronjobsch() {
        simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss");
        date now = new date();
        string strdate = sdf.format(now);
        system.out.println("java cron job expression:: "   strdate);
    }
}

下面的截图显示了应用程序是如何在 09:04:00 启动的,并且从该时间开始每隔一分钟,cron 作业调度程序任务就会执行。

spring boot 调度


固定速率 fixed rate

固定速率调度程序用于在特定时间执行任务。 它不等待上一个任务的完成。 这些值应该以毫秒为单位。 下面是示例代码

@scheduled(fixedrate = 1000)
public void fixedratesch() { 
}

此处显示了从应用程序启动后每秒执行任务的示例代码

package com.study.scheduler;
import org.springframework.scheduling.annotation.scheduled;
import org.springframework.stereotype.component;
import java.text.simpledateformat;
import java.util.date;
/**
 * @author jiyik.com
 */
@component
public class scheduler {
    @scheduled(fixedrate = 1000)
    public void fixedratesch() {
        simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss");
        date now = new date();
        string strdate = sdf.format(now);
        system.out.println("fixed rate scheduler:: "   strdate);
    }
}

请注意以下截图,该截图显示了在 09:14:52 开始的应用程序,之后每隔一个固定速率调度程序任务执行一次。

spring boot fixed rate


固定延迟 fxied delay

固定延迟调度程序用于在特定时间执行任务。 它应该等待上一个任务完成。 这些值应该以毫秒为单位。 下面是示例代码

@scheduled(fixeddelay = 1000, initialdelay = 1000)
public void fixeddelaysch() {
}

这里,initialdelay 是在初始延迟值之后第一次执行任务的时间。

应用程序启动后 3 秒后每秒执行任务的示例如下所示

package com.study.scheduler;
import org.springframework.scheduling.annotation.scheduled;
import org.springframework.stereotype.component;
import java.text.simpledateformat;
import java.util.date;
/**
 * @author jiyik.com
 */
@component
public class scheduler {
    @scheduled(fixeddelay = 1000, initialdelay = 3000)
    public void fixeddelaysch() {
        simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss");
        date now = new date();
        string strdate = sdf.format(now);
        system.out.println("fixed delay scheduler:: "   strdate);
    }
}

观察下面的屏幕截图,它显示了在 09:18:39 开始的应用程序,每 3 秒后,固定延迟调度程序任务每秒执行一次。

spring boot fxied delay

查看笔记

扫码一下
查看教程更方便
网站地图