SpringBoot整合Quartz
导入pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
QuartzConfig
package com.pickle.config;
import com.pickle.quartz.MyQuartz;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/14 11:42
*/
@Configuration
public class QuartzConfig {
/**
* 定义具体的工作明细
* @return
*/
@Bean
public JobDetail printJobDetail(){
//绑定具体的工作
return JobBuilder.newJob(MyQuartz.class).storeDurably().build();
}
/**
* 触发器
* 绑定对应的工作明细
* @return
*/
@Bean
public Trigger printTrigger(){
//日期和星期一般不会同时规定
//ToDo:cron表达式
ScheduleBuilder<CronTrigger> scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
return TriggerBuilder.newTrigger().forJob(printJobDetail()).withSchedule(scheduleBuilder).build();
}
}
Spring简化Quartz
添加注解
编写任务
package com.pickle.demo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/14 11:57
*/
@Component
public class MyBean {
@Scheduled(cron = "0/1 * * * * ?")
public void print(){
System.out.println("Quartz task on..");
}
}
标签:return,技术,springframework,任务,org,import,定时,com,public
From: https://www.cnblogs.com/poteitoutou/p/18072559