每天一个注解之@EnableScheduling

@EnableScheduling

@EnableScheduling是 Spring Framework 提供的一个注解,用于启用 Spring 的定时任务(Scheduling)功能。通过使用这个注解,可以在 Spring 应用程序中创建定时任务,使得指定的方法可以按照设定的时间间隔或固定的时间点自动执行。使用定时任务可以实现周期性地执行一些任务,比如数据清理、报表生成、数据同步等等。
以下是一个简单的示例,展示了如何在 Spring Boot 项目中使用 @EnableScheduling 注解来开启定时任务功能:

在 Spring Boot 项目的入口类上使用 @EnableScheduling 注解:

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

@SpringBootApplication
@EnableScheduling  // 启用定时任务功能
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

创建一个定时任务方法,通过 @Scheduled 注解来指定执行的时间间隔或时间点:

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

@Component
public class MyScheduledTasks {

    @Scheduled(fixedRate = 5000)  // 每隔5秒执行一次
    public void performTask() {
        // 执行定时任务的逻辑
        System.out.println("定时任务执行中...");
    }
}

在上面的示例中,我们在 MyScheduledTasks 类中创建了一个定时任务方法 performTask(),通过 @Scheduled 注解指定了该方法的执行时间间隔为每隔5秒执行一次。

总之,通过在 Spring Boot 项目中使用 @EnableScheduling 注解,可以启用定时任务功能,然后通过 @Scheduled 注解来标记需要定时执行的方法。这些方法会被 Spring 自动调度并按照设定的时间规则执行。

定时任务列表 添加、删除,执行的 实现

实现定时任务列表的添加、删除和执行:
动态添加和删除定时任务,可以借助额外的库,如 Quartz 或者自定义定时任务管理类。
执行定时任务: 定时任务将会由 Spring 定时执行,无需手动触发。
以下是一个使用 Quartz 实现动态添加、删除和执行定时任务的简单示例:

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class QuartzConfig {

    @Autowired
    private Scheduler scheduler;

    @Bean
    public JobDetail sampleJobDetail() {
        return JobBuilder.newJob(SampleJob.class)
                .withIdentity("sampleJob")
                .storeDurably()
                .build();
    }

    @Scheduled(fixedRate = 5000)  // 每隔5秒添加一个定时任务
    public void addSampleJob() throws SchedulerException {
        JobDetail jobDetail = sampleJobDetail();
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(jobDetail)
                .withIdentity(jobDetail.getKey().getName())
                .withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))  // 每10秒执行一次
                .build();

        scheduler.scheduleJob(jobDetail, trigger);
    }

    // 可以添加删除定时任务的方法
}

上述代码中,使用 Quartz 库来实现动态添加和删除定时任务。addSampleJob() 方法会每隔5秒添加一个新的定时任务,该任务会每隔10秒执行一次。可以根据需求扩展添加、删除和执行定时任务的逻辑。

需要注意的是,上述示例只是一个简单的使用 Quartz 实现的动态定时任务的示例,实际应用中需要考虑更多的细节和异常处理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羱滒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值