SkyQuartzConfig.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2021-2025, LXQ All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the com developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: LXQ (609827073@qq.com)
  16. */
  17. package com.kxs.daemon.quartz.config;
  18. import org.quartz.Calendar;
  19. import org.quartz.JobDetail;
  20. import org.quartz.Scheduler;
  21. import org.quartz.Trigger;
  22. import org.springframework.beans.factory.ObjectProvider;
  23. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  24. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  25. import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
  26. import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
  27. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  28. import org.springframework.context.ApplicationContext;
  29. import org.springframework.context.annotation.Bean;
  30. import org.springframework.context.annotation.Configuration;
  31. import org.springframework.scheduling.annotation.EnableAsync;
  32. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Properties;
  36. /**
  37. * @author LXQ
  38. */
  39. @EnableAsync
  40. @Configuration
  41. @ConditionalOnClass({ Scheduler.class, SchedulerFactoryBean.class })
  42. @EnableConfigurationProperties({ QuartzProperties.class })
  43. public class SkyQuartzConfig {
  44. private final QuartzProperties properties;
  45. private final List<SchedulerFactoryBeanCustomizer> customizers;
  46. private final JobDetail[] jobDetails;
  47. private final Map<String, Calendar> calendars;
  48. private final Trigger[] triggers;
  49. private final ApplicationContext applicationContext;
  50. public SkyQuartzConfig(QuartzProperties properties,
  51. ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers, ObjectProvider<JobDetail[]> jobDetails,
  52. ObjectProvider<Map<String, Calendar>> calendars, ObjectProvider<Trigger[]> triggers,
  53. ApplicationContext applicationContext) {
  54. this.properties = properties;
  55. this.customizers = customizers.getIfAvailable();
  56. this.jobDetails = jobDetails.getIfAvailable();
  57. this.calendars = calendars.getIfAvailable();
  58. this.triggers = triggers.getIfAvailable();
  59. this.applicationContext = applicationContext;
  60. }
  61. @Bean
  62. @ConditionalOnMissingBean
  63. public SchedulerFactoryBean quartzScheduler() {
  64. SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
  65. schedulerFactoryBean
  66. .setJobFactory(new AutowireCapableBeanJobFactory(this.applicationContext.getAutowireCapableBeanFactory()));
  67. if (!this.properties.getProperties().isEmpty()) {
  68. schedulerFactoryBean.setQuartzProperties(this.asProperties(this.properties.getProperties()));
  69. }
  70. if (this.jobDetails != null && this.jobDetails.length > 0) {
  71. schedulerFactoryBean.setJobDetails(this.jobDetails);
  72. }
  73. if (this.calendars != null && !this.calendars.isEmpty()) {
  74. schedulerFactoryBean.setCalendars(this.calendars);
  75. }
  76. if (this.triggers != null && this.triggers.length > 0) {
  77. schedulerFactoryBean.setTriggers(this.triggers);
  78. }
  79. this.customize(schedulerFactoryBean);
  80. return schedulerFactoryBean;
  81. }
  82. private Properties asProperties(Map<String, String> source) {
  83. Properties properties = new Properties();
  84. properties.putAll(source);
  85. return properties;
  86. }
  87. private void customize(SchedulerFactoryBean schedulerFactoryBean) {
  88. if (this.customizers != null) {
  89. for (SchedulerFactoryBeanCustomizer customizer : this.customizers) {
  90. customizer.customize(schedulerFactoryBean);
  91. }
  92. }
  93. }
  94. /**
  95. * 通过SchedulerFactoryBean获取Scheduler的实例
  96. * @return
  97. */
  98. @Bean
  99. public Scheduler scheduler() {
  100. return quartzScheduler().getScheduler();
  101. }
  102. }