SpringBoot3入门图文实例之18-整合定时任务

本文介绍了如何在SpringBoot中使用默认定时任务和Quartz库实现更灵活的定时任务,包括在Spring应用中启用定时任务、创建并配置QuartzJob和Trigger。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 使用SpringBoot默认的定时任务

1.1. 新建SpringBoot项目

1.2. 选择版本与依赖

1.3. 在Applicaiton上添加开启定时任务的注解


package com.example._18quartz;

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

@SpringBootApplication
// 开启定时任务
@EnableScheduling
public class Application {

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

}

1.4. 编写一个定时任务的类

package com.example._18quartz.task;

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

@Component
public class ScheduleTask {

    private int count = 0;
    private int count2 = 0;

    // cron 表达式		(每隔15秒执行一次)
    @Scheduled(cron = "0/15 * * * * ?")
    public void task1()
    {
        count++;
        System.out.println("定时任务1,运行第"+count+"次");
    }

    // (每隔6秒执行一次)
    @Scheduled(fixedRate = 6000)
    public void task2()
    {
        count2++;
        System.out.println("定时任务2,运行第"+count2+"次");
    }
}

1.5. 运行测试

2. 使用Quartz

2.1. 注释掉1中的代码

2.2. 新建一个定时任务

SampleJob.java

package com.example._18quartz.quartz;

import lombok.Data;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

@Data
public class SampleJob extends QuartzJobBean {

    protected String name;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println(name+"定时执行代码");
    }
}

2.3. 新建一个定时任务配置类

package com.example._18quartz.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

// @Configuration 配置类
// 里面的方法都是会被自动执行,并且会返回对象 放入到Spring容器中
@Configuration
public class SampleScheduler
{
    // 定时任务详情对象
    @Bean
    public JobDetail jobDetail()
    {
        return JobBuilder.newJob(SampleJob.class)
                .withIdentity("job1", "group1")
                .usingJobData("name","张三")
                .storeDurably()
                .build();
    }

    // 触发器
    @Bean
    public Trigger trigger()
    {

        // 调度器
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5).repeatForever();

        return TriggerBuilder.newTrigger().forJob(jobDetail())
                .withIdentity("sampleTrigger").withSchedule(scheduleBuilder).build();

    }


}

2.4. 测试执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值