Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
- Extract/Rearrange methods
- Inline variables
- Improve Javadoc formatting
  • Loading branch information
fmbenhassine committed May 15, 2022
1 parent 6707705 commit 72d9177
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.batch.core.configuration.annotation;

import java.util.Collection;
import java.util.Map;

import javax.sql.DataSource;

Expand All @@ -39,8 +40,8 @@
import org.springframework.util.Assert;

/**
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
* available by implementing the {@link BatchConfigurer} interface. {@link BatchConfigurer}.
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch.
* Customization is available by implementing the {@link BatchConfigurer} interface.
*
* @author Dave Syer
* @author Michael Minella
Expand All @@ -57,8 +58,6 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial

private BatchConfigurer configurer;

private JobRegistry jobRegistry = new MapJobRegistry();

private JobBuilderFactory jobBuilderFactory;

private StepBuilderFactory stepBuilderFactory;
Expand Down Expand Up @@ -120,7 +119,7 @@ public StepBuilderFactory stepBuilders() throws Exception {
*/
@Bean
public JobRegistry jobRegistry() throws Exception {
return this.jobRegistry;
return new MapJobRegistry();
}

/**
Expand All @@ -133,10 +132,11 @@ public JobRegistry jobRegistry() throws Exception {

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
AnnotationAttributes enabled = AnnotationAttributes.fromMap(importMetadata.getAnnotationAttributes(
EnableBatchProcessing.class.getName(), false));
Assert.notNull(enabled,
"@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName());
Map<String, Object> annotationAttributes =
importMetadata.getAnnotationAttributes(EnableBatchProcessing.class.getName(), false);
AnnotationAttributes enabled = AnnotationAttributes.fromMap(annotationAttributes);
String message = "@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName();
Assert.notNull(enabled, message);
}

@Override
Expand All @@ -156,23 +156,11 @@ protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers)
return this.configurer;
}
if (configurers == null || configurers.isEmpty()) {
DataSource dataSource;
try {
dataSource = this.context.getBean(DataSource.class);
} catch (NoUniqueBeanDefinitionException exception) {
throw new IllegalStateException(
"Multiple data sources are defined in the application context and no primary candidate was found. " +
"To use the default BatchConfigurer, one of the data sources should be annotated with '@Primary'.",
exception);
} catch (NoSuchBeanDefinitionException exception) {
throw new IllegalStateException(
"To use the default BatchConfigurer, the application context must contain at least one data source.",
exception);
}
DataSource dataSource = getDataSource();
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(dataSource);
configurer.initialize();
this.configurer = configurer;
return configurer;
return this.configurer;
}
if (configurers.size() > 1) {
throw new IllegalStateException(
Expand All @@ -183,4 +171,21 @@ protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers)
return this.configurer;
}

private DataSource getDataSource() {
DataSource dataSource;
try {
dataSource = this.context.getBean(DataSource.class);
} catch (NoUniqueBeanDefinitionException exception) {
throw new IllegalStateException(
"Multiple data sources are defined in the application context and no primary candidate was found. " +
"To use the default BatchConfigurer, one of the data sources should be annotated with '@Primary'.",
exception);
} catch (NoSuchBeanDefinitionException exception) {
throw new IllegalStateException(
"To use the default BatchConfigurer, the application context must contain at least one data source.",
exception);
}
return dataSource;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@ public class DefaultBatchConfigurer implements BatchConfigurer {
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;

/**
* Sets the dataSource.
*
* @param dataSource The data source to use. Must not be {@code null}.
*/
public void setDataSource(DataSource dataSource) {
Assert.notNull(dataSource, "DataSource must not be null");
this.dataSource = dataSource;
}

/**
* @return The {@link DataSource} used by the {@link DefaultBatchConfigurer}.
*/
public DataSource getDataSource() {
return this.dataSource;
}

/**
* Create a new {@link DefaultBatchConfigurer} with the passed datasource. This constructor
* will configure a default {@link DataSourceTransactionManager}.
Expand All @@ -81,24 +64,42 @@ public DefaultBatchConfigurer(DataSource dataSource, PlatformTransactionManager
this.transactionManager = transactionManager;
}

@Override
public JobRepository getJobRepository() {
return jobRepository;
/**
* Sets the dataSource.
*
* @param dataSource The data source to use. Must not be {@code null}.
*/
public void setDataSource(DataSource dataSource) {
Assert.notNull(dataSource, "DataSource must not be null");
this.dataSource = dataSource;
}

/**
* @return The {@link DataSource} used by the {@link DefaultBatchConfigurer}.
*/
public DataSource getDataSource() {
return this.dataSource;
}


@Override
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
public JobRepository getJobRepository() {
return this.jobRepository;
}

@Override
public JobLauncher getJobLauncher() {
return jobLauncher;
return this.jobLauncher;
}

@Override
public JobExplorer getJobExplorer() {
return jobExplorer;
return this.jobExplorer;
}

@Override
public PlatformTransactionManager getTransactionManager() {
return this.transactionManager;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Convenient factory for a {@link JobBuilder} which sets the {@link JobRepository} automatically.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class JobBuilderFactory {
Expand All @@ -43,8 +44,7 @@ public JobBuilderFactory(JobRepository jobRepository) {
* @return a job builder
*/
public JobBuilder get(String name) {
JobBuilder builder = new JobBuilder(name).repository(jobRepository);
return builder;
return new JobBuilder(name).repository(this.jobRepository);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* {@link PlatformTransactionManager} automatically.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class StepBuilderFactory {
Expand Down Expand Up @@ -51,9 +52,9 @@ public StepBuilderFactory(JobRepository jobRepository, PlatformTransactionManage
* @return a step builder
*/
public StepBuilder get(String name) {
StepBuilder builder = new StepBuilder(name).repository(jobRepository).transactionManager(
transactionManager);
return builder;
return new StepBuilder(name)
.repository(this.jobRepository)
.transactionManager(this.transactionManager);
}

}

0 comments on commit 72d9177

Please sign in to comment.