Quartz简易定时任务(定时输出一句话)——Spring及Spring Boot

一、Spring 用法

1、导入quartz依赖

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.3.2</version>
</dependency>

2、任务类 

import org.quartz.*;

/**
 * 创建一个MyJob类,实现org.quartz.Job接口
 * 在该类中定义要执行的任务逻辑。
 */
public class MyJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 编写要执行的任务逻辑
        System.out.println("定时任务执行了");
    }
}

3、测试类,每秒输出一句话"定时任务执行了" 

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * 创建一个SchedulerTest类,启动调度器
 */
public class SchedulerTest {
    public static void main(String[] args) throws InterruptedException {
        try {
            // 调用StdSchedulerFactory.getDefaultScheduler()方法创建一个调度器对象scheduler
            // 创建调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            // 使用TriggerBuilder.newTrigger()创建一个触发器对象trigger,并使用withIdentity()方法设置唯一标识符,
            // 通过withSchedule()方法设置触发的时间规则,这里使用了Cron表达式"0 0/1 * * * ?",表示每分钟执行一次
            // 方法一:Cron表达式格式:{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
            // 定义一个触发器,指定执行的时间规则
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")) // 每秒钟执行一次
                    //.withSchedule(CronScheduleBuilder.cronSchedule("01 14 11 15 8 ? 2023")) // 固定时间执行
                    .build();

            // 方法二:使用SimpleTrigger设置触发
//            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger01", "group01").startNow()// 一旦加入scheduler,立即生效
//                    .withSchedule(simpleSchedule() // 使用静态SimpleTrigger
//                            .withIntervalInSeconds(1) // 每隔1秒执行一次
//                            .repeatForever()) // 一直执行
//                    .build();

            // 使用JobBuilder.newJob()创建一个任务对象job,并使用withIdentity()方法设置唯一标识符
            // 定义一个JobDetail,绑定Job实现类
            JobDetail job = JobBuilder.newJob(MyJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            // 调用scheduler.scheduleJob()方法将触发器和任务绑定到调度器中,并调用scheduler.start()方法启动调度器,开始定时执行任务
            // 将触发器和任务绑定到调度器中
            scheduler.scheduleJob(job, trigger);
            System.out.println("-------- start ! ------------");
            // 启动调度器
            scheduler.start();

            //主程序休眠一段时间
            Thread.sleep(10000); // 10秒钟后调度器关闭
            // 关闭调度器
            scheduler.shutdown();
            System.out.println("-------- end ! ------------");

        } catch (SchedulerException e) {
            System.out.println("任务调度失败!");
            e.printStackTrace();
        }
    }
}

【首先定义了一个任务类,继承Job接口,实现该接口的execute()方法,在该方法中编写需要执行的任务,然后定义一个触发器,设置该任务的执行时间,最后定义一个调度器,将任务和触发器绑定在一起从而实现定时任务的执行。】 

二、Spring Boot 用法 

1、导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

【首先要确保有 Spring Boot 依赖】 

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.9</version>
</parent>

2、 配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;
import java.util.Map;

@Configuration
public class QuartzConfig {
    // cron表达式  每秒输出一次
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void show() {
        System.out.println("hello world!");
    }
}

3、启动类

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication // boot启动类配置
@Slf4j // 日志
@EnableScheduling // 启用Spring Boot应用程序中的定时任务功能
public class AppMain {
    public static void main(String[] args) {
        SpringApplication.run(AppMain.class);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值