@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。

 

在 @Scheduled 注解中添加 @Async 注解可以实现定时任务的异步执行。@Async 注解可以用于标记一个方法为异步方法,表示该方法将在一个独立的线程中执行,而不会阻塞当前线程。 下面是一个示例: ```java import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyScheduledTask { @Async @Scheduled(fixedRate = 5000) // 每隔5秒执行一次 public void myAsyncTask() { // 异步执行的任务逻辑 System.out.println("异步任务开始执行..."); // ... System.out.println("异步任务执行完成!"); } } ``` 在上述示例中,我们在定时任务方法上同时添加了 @Async 和 @Scheduled 注解。@Scheduled 注解用于配置定时任务的执行频率,这里使用 fixedRate 表示每隔5秒执行一次。@Async 注解表示该方法将异步执行。 需要注意的是,为了使 @Async 注解生效,还需要在 Spring Boot 的主类上添加 @EnableAsync 注解。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 这样配置之后,定时任务将在独立的线程中异步执行,不会阻塞当前线程。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值