Skip to content

Commit

Permalink
feat: Add option to provide a custom DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklasWallgren committed Feb 7, 2025
1 parent b041658 commit 2ac6d84
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.kagkarlsson.scheduler.boot.autoconfigure;

import javax.sql.DataSource;

@FunctionalInterface
public interface DataSourceSupplier {
/**
* Supplies an instance of {@link DataSource}.
*
* @return The {@link DataSource} to be used by the scheduler.
*/
DataSource dataSource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,18 @@ public class DbSchedulerAutoConfiguration {
private static final Predicate<Task<?>> shouldBeStarted = task -> task instanceof OnStartup;

private final DbSchedulerProperties config;
private final DataSource existingDataSource;
private final List<Task<?>> configuredTasks;
private final List<SchedulerListener> schedulerListeners;
private final List<ExecutionInterceptor> executionInterceptors;

public DbSchedulerAutoConfiguration(
DbSchedulerProperties dbSchedulerProperties,
DataSource dataSource,
List<Task<?>> configuredTasks,
List<SchedulerListener> schedulerListeners,
List<ExecutionInterceptor> executionInterceptors) {
this.config =
Objects.requireNonNull(
dbSchedulerProperties, "Can't configure db-scheduler without required configuration");
this.existingDataSource =
Objects.requireNonNull(dataSource, "An existing javax.sql.DataSource is required");
this.configuredTasks =
Objects.requireNonNull(configuredTasks, "At least one Task must be configured");
this.schedulerListeners = schedulerListeners;
Expand All @@ -105,15 +101,24 @@ StatsRegistry noopStatsRegistry() {
return StatsRegistry.NOOP;
}

@ConditionalOnMissingBean(DataSourceSupplier.class)
@Bean
public DataSourceSupplier dataSourceSupplier(final DataSource existingDataSource) {
return () -> existingDataSource;
}

@ConditionalOnBean(DataSource.class)
@ConditionalOnMissingBean
@DependsOnDatabaseInitialization
@Bean(destroyMethod = "stop")
public Scheduler scheduler(DbSchedulerCustomizer customizer, StatsRegistry registry) {
public Scheduler scheduler(
DbSchedulerCustomizer customizer,
StatsRegistry registry,
DataSourceSupplier dataSourceSupplier) {
log.info("Creating db-scheduler using tasks from Spring context: {}", configuredTasks);

// Ensure that we are using a transactional aware data source
DataSource transactionalDataSource = configureDataSource(existingDataSource);
DataSource transactionalDataSource = configureDataSource(dataSourceSupplier.dataSource());

// Instantiate a new builder
final SchedulerBuilder builder =
Expand Down Expand Up @@ -217,6 +222,8 @@ static LazyInitializationExcludeFilter eagerDbSchedulerStarter() {
}

private static DataSource configureDataSource(DataSource existingDataSource) {
Objects.requireNonNull(existingDataSource, "An existing javax.sql.DataSource is required");

if (existingDataSource instanceof TransactionAwareDataSourceProxy) {
log.debug("Using an already transaction aware DataSource");
return existingDataSource;
Expand Down

0 comments on commit 2ac6d84

Please sign in to comment.