|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.scheduler.quartz; |
| 19 | + |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import javax.sql.DataSource; |
| 23 | + |
| 24 | +import lombok.Data; |
| 25 | +import lombok.extern.slf4j.Slf4j; |
| 26 | + |
| 27 | +import org.quartz.Scheduler; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 31 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 32 | +import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration; |
| 33 | +import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer; |
| 34 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 35 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.core.annotation.Order; |
| 38 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 39 | + |
| 40 | +import com.zaxxer.hikari.HikariConfig; |
| 41 | +import com.zaxxer.hikari.HikariDataSource; |
| 42 | + |
| 43 | +@Slf4j |
| 44 | +@AutoConfiguration(after = {DataSourceAutoConfiguration.class}, before = QuartzSchedulerAutoConfiguration.class) |
| 45 | +@ConditionalOnClass(value = Scheduler.class) |
| 46 | +@ConditionalOnProperty(prefix = "scheduler-plugin.quartz", name = "using-separate-datasource", havingValue = "true") |
| 47 | +@EnableConfigurationProperties(QuartzSchedulerDataSourceAutoConfiguration.QuartzDataSourceConfig.class) |
| 48 | +public class QuartzSchedulerDataSourceAutoConfiguration { |
| 49 | + |
| 50 | + /** |
| 51 | + * Initialize After {@link QuartzAutoConfiguration.JdbcStoreTypeConfiguration#dataSourceCustomizer} |
| 52 | + */ |
| 53 | + @Bean |
| 54 | + @Order(1) |
| 55 | + public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer(QuartzDataSourceConfig quartzDataSourceConfig, |
| 56 | + DataSource defaultDataSource) { |
| 57 | + return schedulerFactoryBean -> { |
| 58 | + if (!quartzDataSourceConfig.isUsingSeparateDatasource()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + final HikariConfig quartzHikariConfig = Optional.ofNullable(quartzDataSourceConfig.getHikari()) |
| 62 | + .orElse(generateQuartzHikariConfigFromDataSource((HikariDataSource) defaultDataSource)); |
| 63 | + final HikariDataSource quartzDataSource = new HikariDataSource(quartzHikariConfig); |
| 64 | + schedulerFactoryBean.setDataSource(quartzDataSource); |
| 65 | + schedulerFactoryBean.setTransactionManager(new DataSourceTransactionManager(quartzDataSource)); |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + @Data |
| 70 | + @ConfigurationProperties(prefix = "scheduler-plugin.quartz") |
| 71 | + public static class QuartzDataSourceConfig { |
| 72 | + |
| 73 | + private boolean usingSeparateDatasource = false; |
| 74 | + private HikariConfig hikari; |
| 75 | + } |
| 76 | + |
| 77 | + private HikariConfig generateQuartzHikariConfigFromDataSource(HikariDataSource hikariDataSource) { |
| 78 | + HikariConfig defaultQuartzHikariConfig = new HikariConfig(); |
| 79 | + hikariDataSource.copyStateTo(defaultQuartzHikariConfig); |
| 80 | + defaultQuartzHikariConfig.setPoolName("QuartzHikariCP"); |
| 81 | + return defaultQuartzHikariConfig; |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments