java 获取scheduler_springboot成神之——Scheduler定时任务

本文介绍spring的Scheduler定时任务

目录结构

1d30e511329ee0b0775bb23172647149.png

config

// @EnableScheduling 开启后台任务

package com.springlearn.learn.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration

@EnableScheduling

public class SchedulerConfig {

}

scheduler

// 获取当前时间

package com.springlearn.learn.scheduler;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component

public class GetCurrentTimeSchedule {

private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss sss");

@Scheduled(initialDelay=3*1000, fixedDelay=1*1000)

public void GetCurrentTime() {

Date now = new Date();

System.out.println("Now is:" + df.format(now));

}

}

@Scheduled配置参数

cron

second, minute, hour, day of month, month, day(s) of week

@Scheduled(cron="0 * * * * *" ) 每分钟执行一次

@Scheduled(cron="*/10 * * * * *") 每10秒执行一次

@Scheduled(cron="*0 0 8-10 * * *") 8点,9点,10点各执行一次

@Scheduled(cron="0 0/30 8-10 * * *") 8点,8点半,9点,9点半,10点,10点半各执行一次

@Scheduled(cron="0 0 9-17 * * MON-FRI") 周一到周五的每天的9点到17点各执行一次

@Scheduled(cron="0 0 0 1 1 ?") 元旦节午夜执行一次

zone

时区

@Scheduled(cron="0 * * * * *", zone="Asia/Shanghai")

fixedDelay

当上一个任务完成,才会执行下一个

fixedDelayString

fixedRate

不管上一个任务完没完成,时间一到,都会执行下一个

fixedRateString

initialDelay

initialDelayString

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值