quartz是java裏頭定時任務的經典開源實現,這裏講述一下如何在springboot中集成quartz。html
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz.version}</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>${quartz.version}</version> </dependency>
org.quartz.scheduler.instanceName=spring-boot-quartz-demo org.quartz.scheduler.instanceId=AUTO org.quartz.threadPool.threadCount=5 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
@Configuration public class QuartzConfig { public static final String QUARTZ_PROPERTIES_PATH = "/quartz.properties"; @Bean public JobFactory jobFactory(ApplicationContext applicationContext) { AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); jobFactory.setApplicationContext(applicationContext); return jobFactory; } @Bean public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setAutoStartup(true); factory.setJobFactory(jobFactory); factory.setQuartzProperties(quartzProperties()); return factory; } @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource(QUARTZ_PROPERTIES_PATH)); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } public static class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware { private transient AutowireCapableBeanFactory beanFactory; @Override public void setApplicationContext(final ApplicationContext context) { beanFactory = context.getAutowireCapableBeanFactory(); } @Override protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { final Object job = super.createJobInstance(bundle); beanFactory.autowireBean(job); return job; } } }
@PersistJobDataAfterExecution @DisallowConcurrentExecution public class DemoJob implements Job{ private String jobArg; @Autowired DemoService demoService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { demoService.hello(jobArg); } public void setJobArg(String jobArg) { this.jobArg = jobArg; } }
註冊到quartz去調度java
@Autowired SchedulerFactoryBean schedulerFactoryBean; public void scheduleJobs() throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("jobArg", "world"); JobDetail jobDetail = JobBuilder.newJob(DemoJob.class) .setJobData(jobDataMap) .withDescription("demo") .withIdentity("demo-job", "demo-group") .build(); Trigger trigger = TriggerBuilder.newTrigger() .forJob(jobDetail) .withSchedule(cronSchedule("0/5 * * * * ?")) .build(); if(!scheduler.checkExists(JobKey.jobKey("demo-job","demo-group"))){ scheduler.scheduleJob(jobDetail,trigger); } scheduler.start(); }
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName=spring-boot-quartz-demo org.quartz.scheduler.instanceId=AUTO org.quartz.scheduler.skipUpdateCheck=true #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=5 org.quartz.threadPool.threadPriority=5 #============================================================================ # Configure JobStore #============================================================================ org.quartz.jobStore.misfireThreshold=60000 org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate org.quartz.jobStore.useProperties=false #org.quartz.jobStore.dataSource=dataSource org.quartz.jobStore.tablePrefix=QRTZ_ org.quartz.jobStore.isClustered=true
@Bean public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, DataSource dataSource, PlatformTransactionManager transactionManager) throws IOException { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setAutoStartup(true); factory.setJobFactory(jobFactory); factory.setQuartzProperties(quartzProperties()); //jdbc store factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); return factory; }
其餘的配置不變
springboot項目正常配置datasource便可
sql的schema文件見安裝包的docs/dbTables目錄下git