Skip to content

Commit f8bdf55

Browse files
committed
Remove the Map based job repository/explorer and their DAOs
This commit removes the deprecated Map-based job repository and job explorer implementations with their respective DAOs. Using the `EnableBatchProcessing` annotation now requires a datasource bean to be defined in the application context. This will be reviewed as part of #3942. This commit is a first pass that updates related tests to use the JDBC-based job repository/explorer with an embedded database. A second pass should be done to improve tests by caching/reusing embedded databases if possible. Issue #3836
1 parent d5509d2 commit f8bdf55

File tree

115 files changed

+1429
-3311
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+1429
-3311
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@
5151
@Import(ScopeConfiguration.class)
5252
public abstract class AbstractBatchConfiguration implements ImportAware, InitializingBean {
5353

54-
@Autowired(required = false)
54+
@Autowired
5555
private DataSource dataSource;
5656

5757
private BatchConfigurer configurer;
@@ -103,22 +103,15 @@ public void afterPropertiesSet() throws Exception {
103103
this.stepBuilderFactory = new StepBuilderFactory(jobRepository(), transactionManager());
104104
}
105105

106-
protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers) throws Exception {
106+
protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers) {
107107
if (this.configurer != null) {
108108
return this.configurer;
109109
}
110110
if (configurers == null || configurers.isEmpty()) {
111-
if (dataSource == null) {
112-
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer();
113-
configurer.initialize();
114-
this.configurer = configurer;
115-
return configurer;
116-
} else {
117-
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(dataSource);
118-
configurer.initialize();
119-
this.configurer = configurer;
120-
return configurer;
121-
}
111+
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(this.dataSource);
112+
configurer.initialize();
113+
this.configurer = configurer;
114+
return configurer;
122115
}
123116
if (configurers.size() > 1) {
124117
throw new IllegalStateException(

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,27 +18,20 @@
1818
import javax.annotation.PostConstruct;
1919
import javax.sql.DataSource;
2020

21-
import org.apache.commons.logging.Log;
22-
import org.apache.commons.logging.LogFactory;
23-
2421
import org.springframework.batch.core.configuration.BatchConfigurationException;
2522
import org.springframework.batch.core.explore.JobExplorer;
2623
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
27-
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
2824
import org.springframework.batch.core.launch.JobLauncher;
2925
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
3026
import org.springframework.batch.core.repository.JobRepository;
3127
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
32-
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
33-
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
34-
import org.springframework.beans.factory.annotation.Autowired;
3528
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3629
import org.springframework.stereotype.Component;
3730
import org.springframework.transaction.PlatformTransactionManager;
31+
import org.springframework.util.Assert;
3832

3933
@Component
4034
public class DefaultBatchConfigurer implements BatchConfigurer {
41-
private static final Log logger = LogFactory.getLog(DefaultBatchConfigurer.class);
4235

4336
private DataSource dataSource;
4437
private PlatformTransactionManager transactionManager;
@@ -47,28 +40,39 @@ public class DefaultBatchConfigurer implements BatchConfigurer {
4740
private JobExplorer jobExplorer;
4841

4942
/**
50-
* Sets the dataSource. If the {@link DataSource} has been set once, all future
51-
* values are passed are ignored (to prevent {@code}@Autowired{@code} from overwriting
52-
* the value).
43+
* Sets the dataSource.
5344
*
54-
* @param dataSource The data source to use
45+
* @param dataSource The data source to use. Must not be {@code null}.
5546
*/
56-
@Autowired(required = false)
5747
public void setDataSource(DataSource dataSource) {
58-
if(this.dataSource == null) {
59-
this.dataSource = dataSource;
60-
}
61-
62-
if(getTransactionManager() == null) {
63-
logger.warn("No transaction manager was provided, using a DataSourceTransactionManager");
64-
this.transactionManager = new DataSourceTransactionManager(this.dataSource);
65-
}
48+
Assert.notNull(dataSource, "DataSource must not be null");
49+
this.dataSource = dataSource;
6650
}
6751

68-
protected DefaultBatchConfigurer() {}
52+
public DataSource getDataSource() {
53+
return this.dataSource;
54+
}
6955

56+
/**
57+
* Create a new {@link DefaultBatchConfigurer} with the passed datasource. This constructor
58+
* will configure a default {@link DataSourceTransactionManager}.
59+
*
60+
* @param dataSource to use for the job repository and job explorer
61+
*/
7062
public DefaultBatchConfigurer(DataSource dataSource) {
71-
setDataSource(dataSource);
63+
this(dataSource, new DataSourceTransactionManager(dataSource));
64+
}
65+
66+
/**
67+
* Create a new {@link DefaultBatchConfigurer} with the passed datasource and transaction manager.
68+
* @param dataSource to use for the job repository and job explorer
69+
* @param transactionManager to use for the job repository
70+
*/
71+
public DefaultBatchConfigurer(DataSource dataSource, PlatformTransactionManager transactionManager) {
72+
Assert.notNull(dataSource, "DataSource must not be null");
73+
Assert.notNull(transactionManager, "transactionManager must not be null");
74+
this.dataSource = dataSource;
75+
this.transactionManager = transactionManager;
7276
}
7377

7478
@Override
@@ -94,26 +98,8 @@ public JobExplorer getJobExplorer() {
9498
@PostConstruct
9599
public void initialize() {
96100
try {
97-
if(dataSource == null) {
98-
logger.warn("No datasource was provided...using a Map based JobRepository");
99-
100-
if(getTransactionManager() == null) {
101-
logger.warn("No transaction manager was provided, using a ResourcelessTransactionManager");
102-
this.transactionManager = new ResourcelessTransactionManager();
103-
}
104-
105-
MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(getTransactionManager());
106-
jobRepositoryFactory.afterPropertiesSet();
107-
this.jobRepository = jobRepositoryFactory.getObject();
108-
109-
MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
110-
jobExplorerFactory.afterPropertiesSet();
111-
this.jobExplorer = jobExplorerFactory.getObject();
112-
} else {
113-
this.jobRepository = createJobRepository();
114-
this.jobExplorer = createJobExplorer();
115-
}
116-
101+
this.jobRepository = createJobRepository();
102+
this.jobExplorer = createJobExplorer();
117103
this.jobLauncher = createJobLauncher();
118104
} catch (Exception e) {
119105
throw new BatchConfigurationException(e);
@@ -122,14 +108,14 @@ public void initialize() {
122108

123109
protected JobLauncher createJobLauncher() throws Exception {
124110
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
125-
jobLauncher.setJobRepository(jobRepository);
111+
jobLauncher.setJobRepository(this.jobRepository);
126112
jobLauncher.afterPropertiesSet();
127113
return jobLauncher;
128114
}
129115

130116
protected JobRepository createJobRepository() throws Exception {
131117
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
132-
factory.setDataSource(dataSource);
118+
factory.setDataSource(this.dataSource);
133119
factory.setTransactionManager(getTransactionManager());
134120
factory.afterPropertiesSet();
135121
return factory.getObject();

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,9 +85,7 @@
8585
* }
8686
* </pre>
8787
*
88-
* If a user does not provide a {@link javax.sql.DataSource} within the context, a Map based
89-
* {@link org.springframework.batch.core.repository.JobRepository} will be used. If multiple
90-
* {@link javax.sql.DataSource}s are defined in the context, the one annotated with
88+
* If multiple {@link javax.sql.DataSource}s are defined in the context, the one annotated with
9189
* {@link org.springframework.context.annotation.Primary} will be used (Note that if none
9290
* of them is annotated with {@link org.springframework.context.annotation.Primary}, the one
9391
* named <code>dataSource</code> will be used if any, otherwise a {@link UnsatisfiedDependencyException}
@@ -111,14 +109,8 @@
111109
* job repository and transaction manager into every step</li>
112110
* </ul>
113111
*
114-
* The transaction manager provided by this annotation will be of type:
115-
*
116-
* <ul>
117-
* <li>{@link org.springframework.batch.support.transaction.ResourcelessTransactionManager}
118-
* if no {@link javax.sql.DataSource} is provided within the context</li>
119-
* <li>{@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
120-
* if a {@link javax.sql.DataSource} is provided within the context</li>
121-
* </ul>
112+
* The transaction manager provided by this annotation will be of type {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
113+
* configured with the {@link javax.sql.DataSource} provided within the context.
122114
*
123115
* In order to use a custom transaction manager, a custom {@link BatchConfigurer} should be provided. For example:
124116
*

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/AbstractJobExplorerFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,9 +29,9 @@
2929
* object implementations.
3030
*
3131
* @see JobExplorerFactoryBean
32-
* @see MapJobExplorerFactoryBean
3332
*
3433
* @author Dave Syer
34+
* @author Mahmoud Ben Hassine
3535
* @since 2.0
3636
*/
3737
public abstract class AbstractJobExplorerFactoryBean implements FactoryBean<JobExplorer> {

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/MapJobExplorerFactoryBean.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)