Align with breaking changes in latest Batch snapshots
Batch is now auto-configured to use the context's PlatformTransactionManager and DataSource or `@BatchDataSource`. When this does not meet the user's needs, they can use `@EnableBatchProcessing` or sub-class `DefaultBatchConfiguration` to take complete control with the auto-configuration backing off. Closes gh-32330pull/32205/head
parent
dc56fa62bc
commit
64f4da80cb
@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.boot.autoconfigure.batch;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
|
||||
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.batch.core.repository.support.JobRepositoryFactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.autoconfigure.batch.BatchProperties.Isolation;
|
||||
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* Basic {@link BatchConfigurer} implementation.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Andy Wilkinson
|
||||
* @author Kazuki Shimizu
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
|
||||
|
||||
private final BatchProperties properties;
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
private final TransactionManagerCustomizers transactionManagerCustomizers;
|
||||
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
private JobExplorer jobExplorer;
|
||||
|
||||
/**
|
||||
* Create a new {@link BasicBatchConfigurer} instance.
|
||||
* @param properties the batch properties
|
||||
* @param dataSource the underlying data source
|
||||
* @param transactionManagerCustomizers transaction manager customizers (or
|
||||
* {@code null})
|
||||
*/
|
||||
protected BasicBatchConfigurer(BatchProperties properties, DataSource dataSource,
|
||||
TransactionManagerCustomizers transactionManagerCustomizers) {
|
||||
this.properties = properties;
|
||||
this.dataSource = dataSource;
|
||||
this.transactionManagerCustomizers = transactionManagerCustomizers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobRepository getJobRepository() {
|
||||
return this.jobRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobLauncher getJobLauncher() {
|
||||
return this.jobLauncher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobExplorer getJobExplorer() throws Exception {
|
||||
return this.jobExplorer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
try {
|
||||
this.jobRepository = createJobRepository();
|
||||
this.jobLauncher = createJobLauncher();
|
||||
this.jobExplorer = createJobExplorer();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Unable to initialize Spring Batch", ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected JobExplorer createJobExplorer() throws Exception {
|
||||
PropertyMapper map = PropertyMapper.get();
|
||||
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
|
||||
factory.setDataSource(this.dataSource);
|
||||
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
protected JobLauncher createJobLauncher() throws Exception {
|
||||
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
|
||||
jobLauncher.setJobRepository(getJobRepository());
|
||||
jobLauncher.afterPropertiesSet();
|
||||
return jobLauncher;
|
||||
}
|
||||
|
||||
protected JobRepository createJobRepository() throws Exception {
|
||||
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
|
||||
PropertyMapper map = PropertyMapper.get();
|
||||
map.from(this.dataSource).to(factory::setDataSource);
|
||||
map.from(this::determineIsolationLevel).whenNonNull().to(factory::setIsolationLevelForCreate);
|
||||
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
|
||||
map.from(this::buildTransactionManager).to(factory::setTransactionManager);
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the isolation level for create* operation of the {@link JobRepository}.
|
||||
* @return the isolation level or {@code null} to use the default
|
||||
*/
|
||||
protected String determineIsolationLevel() {
|
||||
Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate();
|
||||
return (isolation != null) ? isolation.toIsolationName() : null;
|
||||
}
|
||||
|
||||
protected PlatformTransactionManager createTransactionManager() {
|
||||
return new DataSourceTransactionManager(this.dataSource);
|
||||
}
|
||||
|
||||
private PlatformTransactionManager buildTransactionManager() {
|
||||
PlatformTransactionManager transactionManager = createTransactionManager();
|
||||
if (this.transactionManagerCustomizers != null) {
|
||||
this.transactionManagerCustomizers.customize(transactionManager);
|
||||
}
|
||||
this.transactionManager = transactionManager;
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
PlatformTransactionManager getTransactionManager() {
|
||||
return this.transactionManager;
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.boot.autoconfigure.batch;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* Provide a {@link BatchConfigurer} according to the current environment.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@ConditionalOnClass(PlatformTransactionManager.class)
|
||||
@ConditionalOnBean(DataSource.class)
|
||||
@ConditionalOnMissingBean(BatchConfigurer.class)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
class BatchConfigurerConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnMissingBean(name = "entityManagerFactory")
|
||||
static class JdbcBatchConfiguration {
|
||||
|
||||
@Bean
|
||||
BasicBatchConfigurer batchConfigurer(BatchProperties properties, DataSource dataSource,
|
||||
@BatchDataSource ObjectProvider<DataSource> batchDataSource,
|
||||
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
|
||||
return new BasicBatchConfigurer(properties, batchDataSource.getIfAvailable(() -> dataSource),
|
||||
transactionManagerCustomizers.getIfAvailable());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(EntityManagerFactory.class)
|
||||
@ConditionalOnBean(name = "entityManagerFactory")
|
||||
static class JpaBatchConfiguration {
|
||||
|
||||
@Bean
|
||||
JpaBatchConfigurer batchConfigurer(BatchProperties properties, DataSource dataSource,
|
||||
@BatchDataSource ObjectProvider<DataSource> batchDataSource,
|
||||
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers,
|
||||
EntityManagerFactory entityManagerFactory) {
|
||||
return new JpaBatchConfigurer(properties, batchDataSource.getIfAvailable(() -> dataSource),
|
||||
transactionManagerCustomizers.getIfAvailable(), entityManagerFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.boot.autoconfigure.batch;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.autoconfigure.batch.BatchProperties.Isolation;
|
||||
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* A {@link BasicBatchConfigurer} tailored for JPA.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class JpaBatchConfigurer extends BasicBatchConfigurer {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(JpaBatchConfigurer.class);
|
||||
|
||||
private final EntityManagerFactory entityManagerFactory;
|
||||
|
||||
/**
|
||||
* Create a new {@link BasicBatchConfigurer} instance.
|
||||
* @param properties the batch properties
|
||||
* @param dataSource the underlying data source
|
||||
* @param transactionManagerCustomizers transaction manager customizers (or
|
||||
* {@code null})
|
||||
* @param entityManagerFactory the entity manager factory (or {@code null})
|
||||
*/
|
||||
protected JpaBatchConfigurer(BatchProperties properties, DataSource dataSource,
|
||||
TransactionManagerCustomizers transactionManagerCustomizers, EntityManagerFactory entityManagerFactory) {
|
||||
super(properties, dataSource, transactionManagerCustomizers);
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String determineIsolationLevel() {
|
||||
String name = super.determineIsolationLevel();
|
||||
if (name != null) {
|
||||
return name;
|
||||
}
|
||||
else {
|
||||
logger.warn("JPA does not support custom isolation levels, so locks may not be taken when launching Jobs. "
|
||||
+ "To silence this warning, set 'spring.batch.jdbc.isolation-level-for-create' to 'default'.");
|
||||
return Isolation.DEFAULT.toIsolationName();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PlatformTransactionManager createTransactionManager() {
|
||||
return new JpaTransactionManager(this.entityManagerFactory);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue