springboot整合quartz(2)
application.yml
# datasource
spring:
datasource:
url: jdbc:mysql://localhost/quartzDemo?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
spring-quartz.properties
# 表的前缀,默认QRTZ_
org.quartz.jobStore.tablePrefix=qrtz_
# 是否加入集群
org.quartz.jobStore.isClustered=true
# 调度实例失效的检查时间间隔 ms
org.quartz.jobStore.clusterCheckinInterval=20000
#数据库持久化
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
# 数据库代理类,一般org.quartz.impl.jdbcjobstore.StdJDBCDelegate可以满足大部分数据库
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.scheduler.instanceName=ClusterQuartz
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=25
org.quartz.threadPool.threadPriority=5
package com.hgc.zm.bootquartz;
import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
/**
* @author zm
* @version 1.0
* @date 2021/4/26 15:02
*/
@PersistJobDataAfterExecution //JobData 持久化
@DisallowConcurrentExecution //禁止并发
public class MyJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
Thread.sleep(2000);
System.err.println(context.getScheduler().getSchedulerInstanceId());
System.err.println("taskname=" + context.getJobDetail().getKey().getName());
System.err.println("执行时间" + new Date());
} catch (InterruptedException | SchedulerException e) {
e.printStackTrace();
}
}
}
获取Scheduler配置类
package com.hgc.zm.config;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* @author zm
* @version 1.0
* @date 2021/4/26 15:07
*/
@Configuration
public class SchedulerConfig {
@Autowired
private DataSource dataSource;
@Bean
public Scheduler getScheduler() throws IOException {
return schedulerFactoryBean().getScheduler();
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setSchedulerName("cluster_scheduler");
factory.setDataSource(dataSource);
factory.setApplicationContextSchedulerContextKey("application");
factory.setQuartzProperties(quartzProperties());
factory.setTaskExecutor(schedulerThreadPool());
factory.setStartupDelay(10);
return factory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/spring-quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean
public Executor schedulerThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors());
executor.setQueueCapacity(Runtime.getRuntime().availableProcessors());
return executor;
}
}
监听器
package com.hgc.zm.listen;
import com.hgc.zm.bootquartz.MyJob;
import com.hgc.zm.config.SchedulerConfig;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author zm
* @version 1.0
* @date 2021/4/26 15:25
*/
@Component
public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private SchedulerConfig scheduler;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
TriggerKey triggerKey = TriggerKey.triggerKey("trigger1", "group1");
try {
Trigger trigger = scheduler.getScheduler().getTrigger(triggerKey);
if (trigger == null) {
trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
.build();
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity("job1", "group1")
.build();
scheduler.getScheduler().scheduleJob(jobDetail, trigger);
scheduler.getScheduler().start();
}
} catch (SchedulerException | IOException e) {
e.printStackTrace();
}
}
}
启动类
package com.hgc.zm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZmApplication {
public static void main(String[] args) {
SpringApplication.run(ZmApplication.class, args);
}
}

浙公网安备 33010602011771号