Springboot 配置类 Quartz设置默认启动关闭具体实现
时间: 2024-05-14 15:14:29 浏览: 231
Quartz调度任务基于Springboot实现
1. 创建 QuartzConfig 配置类
```java
@Configuration
public class QuartzConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(QuartzConfig.class);
private JobFactory jobFactory;
public QuartzConfig(JobFactory jobFactory) {
this.jobFactory = jobFactory;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(jobFactory);
schedulerFactoryBean.setStartupDelay(5);
schedulerFactoryBean.setAutoStartup(true);
schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
schedulerFactoryBean.setQuartzProperties(quartzProperties());
LOGGER.info("创建 schedulerFactoryBean 成功");
return schedulerFactoryBean;
}
@Bean
public Properties quartzProperties() {
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.instanceName", "MyScheduler");
properties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
properties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true");
properties.setProperty("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
properties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "10");
properties.setProperty("org.quartz.threadPool.threadPriority", "5");
properties.setProperty("org.quartz.jobStore.misfireThreshold", "60000");
properties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.jobStore.isClustered", "true");
properties.setProperty("org.quartz.jobStore.clusterCheckinInterval", "15000");
LOGGER.info("创建 quartzProperties 成功");
return properties;
}
}
```
2. 创建 JobFactory 实现类
```java
@Component
public class QuartzJobFactory extends AdaptableJobFactory {
@Autowired
private ApplicationContext applicationContext;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
applicationContext.getAutowireCapableBeanFactory().autowireBean(jobInstance);
return jobInstance;
}
}
```
3. 创建 JobDetail 和 Trigger
```java
@Component
public class MyJob {
public void execute() {
System.out.println("任务执行");
}
public JobDetail jobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("job1", "group1")
.storeDurably()
.build();
}
public Trigger trigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)
.repeatForever();
return TriggerBuilder.newTrigger()
.forJob(jobDetail())
.withIdentity("trigger1", "group1")
.withSchedule(scheduleBuilder)
.build();
}
}
```
4. 启动项目
启动项目后,会自动启动 Quartz 定时任务,如果想关闭 Quartz 定时任务,可以在配置文件中设置 autoStartup=false。例如:
```
spring.quartz.auto-startup=false
```
阅读全文