Quartz 2025 整合Springboot简单案例
package com.xxgc.quartzdemo.job;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
/**
* 打工仔
*/
@Slf4j
@Component
public class ZhangSanJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//最终要执行的事件
log.info("在吗?出来玩");
}
}
package com.xxgc.quartzdemo.scheduler;
import com.xxgc.quartzdemo.job.ZhangSanJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 早安调度器
* 老板 ,用于安排任务和时间
*/
@Configuration
public class ZhaoAnScheduler {
@Bean
public Scheduler zhaoAn() throws SchedulerException {
//Factory 工厂 Scheduler 调度器 用工厂生产一个老板
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = stdSchedulerFactory.getScheduler();
//触发器 (闹钟) CRON表达式 Trigger触发器
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
//安排打工人 Job任务
JobDetail jobDetail = JobBuilder.newJob(ZhangSanJob.class).storeDurably().build();
//老板安排任务
scheduler.scheduleJob(jobDetail,cronTrigger);
//启动任务
scheduler.start();
return scheduler;
}
}
package com.xxgc.quartzdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
//开启任务调度
@EnableScheduling
@SpringBootApplication
public class QuartzDemoApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzDemoApplication.class, args);
}
}
server:
port: 8083
最后修改于 2025-05-22 16:27:25
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

