@EnableScheduling和@Scheduled、@EnableAsync和@Async(1)

5 篇文章 0 订阅
4 篇文章 0 订阅

今天在浏览公司项目的时候看到了两个以前没有接触到的注解分别是@EnableScheduling和EnableAsync。先简单说一下我们公司的项目框架吧,我们团队的服务端项目框架使用的是SpringBoot框架,基本上利用了SpringBoot的大部分组件来进行开发的。今天看到的这两个注解就是标注在SpringBoot的主程序入口类上,类似如下:

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

@EnableScheduling
@EnableAsync
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {

        SpringApplication.run(TestApplication.class, args);

    }
}

那么这两个注解分别表示什么意思呢?在经过一番查阅之后,总结如下:

1、@EnableScheduling和@Scheduled

@EnableScheduling:该注解标注在配置类上,用于开启对定时任务的支持。

其中Scheduled注解中有以下几个参数:

cron:是设置定时执行的表达式,如 0/5 * * * ?每隔五分钟执行一次;里面的参数(秒 分 小时 日期 月份 星期 年 )

zone:表示执行时间的时区;

fixedDelay和fixedDelayString:表示一个固定延迟时间执行,上个任务完成后,延迟多长时间执行;

fixedRate和fixedRateString:表示一个固定频率执行,上个任务开始后,多长时间后开始执行;

initialDelay和initialDelayString:表示一个初始延迟时间,第一次被调用前延迟的时间。

具体表达式详细见:https://blog.csdn.net/supingemail/article/details/22274279

@Scheduled:该注解标注在具体的方法上,用于声明具体要执行的定时任务。

配置类:

package com.schedule.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * 配置类
 */
@Configuration
@Component
@EnableScheduling
public class ScheduledConfig {

}

具体业务类:

package com.schedule.service;

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

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledService {

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

    //每5秒一次执行一次该方法
    @Scheduled(cron = "0/5 * * * * ?")
    public void cronTest(){
        System.out.println("cron,当前时间:"+ format.format(new Date()));
    }

    //初始延迟1秒钟,每隔2秒钟执行一次该方法
    @Scheduled(fixedRateString = "2000", initialDelay = 1000)
    public void fixedRateTest(){
        System.out.println("fixedRateString,当前时间:" +format.format(new Date()));
    }

    //每次执行完该方法便延迟两秒钟
    @Scheduled(fixedDelayString = "2000")
    public void fixedDelayTest(){
        System.out.println("fixedDelayString,当前时间:" +format.format(new Date()));
    }
}

测试:

package com.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ScheduledApplication {
    public static void main(String[] args) {

        SpringApplication.run(ScheduledApplication.class, args);

    }
}

结果:

fixedRateString,当前时间:23:51:22
fixedDelayString,当前时间:23:51:23
fixedRateString,当前时间:23:51:24
cron,当前时间:23:51:25
fixedDelayString,当前时间:23:51:25
fixedRateString,当前时间:23:51:26
fixedDelayString,当前时间:23:51:27
fixedRateString,当前时间:23:51:28
fixedDelayString,当前时间:23:51:29
cron,当前时间:23:51:30
fixedRateString,当前时间:23:51:30
。。。。

本文暂且说到这里,下一节,我将讨论第二组注解@EnableAsync和@Async。

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值