-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability for composed task runner to use the proper JobRepository …
…and TaskExecutor * BeanPostProcessor has been added so that CTR can use its jobRepository vs. the one provided by BatchAutoConfiguration * BeanPostProcessor has been added so that CTR can use its own JobLauncher. This is because CTR needs a ThreadPoolTaskExecutor vs the SyncTaskExecutor used by default.
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...a/org/springframework/cloud/dataflow/composedtaskrunner/JobLauncherBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.dataflow.composedtaskrunner; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.batch.core.configuration.BatchConfigurationException; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.task.TaskExecutor; | ||
|
||
|
||
/** | ||
* CTR requires the use of the {@link java.util.concurrent.ThreadPoolExecutor}. | ||
* As of Batch 5.x DefaultBatchConfiguration is now used to override default beans, however this disables | ||
* BatchAutoConfiguration. To work around this CTR creates its own {@link JobLauncher} that uses this {@link TaskExecutor}. | ||
* | ||
* @author Glenn Renfro | ||
*/ | ||
public class JobLauncherBeanPostProcessor implements BeanPostProcessor, Ordered { | ||
private static final Logger logger = LoggerFactory.getLogger(JobLauncherBeanPostProcessor.class); | ||
|
||
private ApplicationContext context; | ||
|
||
public JobLauncherBeanPostProcessor(ApplicationContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.LOWEST_PRECEDENCE; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
if (beanName.equals("jobLauncher")) { | ||
logger.debug("Replacing BatchAutoConfiguration's jobLauncher Bean with one provided by composed task runner."); | ||
bean = jobLauncher(context.getBean("jobRepository", JobRepository.class), | ||
context.getBean("taskExecutor", TaskExecutor.class)); | ||
} | ||
return bean; | ||
} | ||
|
||
private JobLauncher jobLauncher(JobRepository jobRepository, TaskExecutor taskExecutor) { | ||
TaskExecutorJobLauncher taskExecutorJobLauncher = new TaskExecutorJobLauncher(); | ||
taskExecutorJobLauncher.setJobRepository(jobRepository); | ||
taskExecutorJobLauncher.setTaskExecutor(taskExecutor); | ||
try { | ||
taskExecutorJobLauncher.afterPropertiesSet(); | ||
return taskExecutorJobLauncher; | ||
} catch (Exception e) { | ||
throw new BatchConfigurationException("Unable to configure the default job launcher", e); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...org/springframework/cloud/dataflow/composedtaskrunner/JobRepositoryBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.dataflow.composedtaskrunner; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.cloud.dataflow.composedtaskrunner.properties.ComposedTaskProperties; | ||
import org.springframework.cloud.dataflow.composedtaskrunner.support.ComposedTaskException; | ||
import org.springframework.cloud.dataflow.core.database.support.MultiSchemaIncrementerFactory; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
/** | ||
* CTR requires that the JobRepository that it uses to have its own {@link MultiSchemaIncrementerFactory}. | ||
* As of Batch 5.x DefaultBatchConfiguration is now used to override default beans, however this disables | ||
* BatchAutoConfiguration. To work around this we use a bean post processor to create our own {@link JobRepository}. | ||
* | ||
* @author Glenn Renfro | ||
*/ | ||
public class JobRepositoryBeanPostProcessor implements BeanPostProcessor, Ordered { | ||
private static final Logger logger = LoggerFactory.getLogger(JobRepositoryBeanPostProcessor.class); | ||
|
||
private PlatformTransactionManager transactionManager; | ||
private DataSource incrementerDataSource; | ||
private ComposedTaskProperties composedTaskProperties; | ||
|
||
public JobRepositoryBeanPostProcessor(PlatformTransactionManager transactionManager, DataSource incrementerDataSource, | ||
ComposedTaskProperties composedTaskProperties) { | ||
this.transactionManager = transactionManager; | ||
this.incrementerDataSource = incrementerDataSource; | ||
this.composedTaskProperties = composedTaskProperties; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.HIGHEST_PRECEDENCE; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
if (beanName.equals("jobRepository")) { | ||
logger.debug("Replacing BatchAutoConfiguration's jobRepository Bean with one provided by composed task runner."); | ||
bean = jobRepository(transactionManager, incrementerDataSource, composedTaskProperties); | ||
} | ||
return bean; | ||
} | ||
|
||
private JobRepository jobRepository(PlatformTransactionManager transactionManager, DataSource incrementerDataSource, | ||
ComposedTaskProperties composedTaskProperties) { | ||
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); | ||
MultiSchemaIncrementerFactory incrementerFactory = new MultiSchemaIncrementerFactory(incrementerDataSource); | ||
factory.setIncrementerFactory(incrementerFactory); | ||
factory.setDataSource(incrementerDataSource); | ||
factory.setTransactionManager(transactionManager); | ||
factory.setIsolationLevelForCreate(composedTaskProperties.getTransactionIsolationLevel()); | ||
try { | ||
factory.afterPropertiesSet(); | ||
return factory.getObject(); | ||
} | ||
catch (Exception exception) { | ||
throw new ComposedTaskException(exception.getMessage()); | ||
} | ||
} | ||
} |