Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acee549

Browse files
committed
BATCH-2247: Added creation of a JobExplorer by default to address the inability to create a Map based JobExplorer using java config
1 parent fb5bf9f commit acee549

File tree

8 files changed

+67
-22
lines changed

8 files changed

+67
-22
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.springframework.batch.core.configuration.JobRegistry;
1919
import org.springframework.batch.core.configuration.support.MapJobRegistry;
20+
import org.springframework.batch.core.explore.JobExplorer;
2021
import org.springframework.batch.core.launch.JobLauncher;
2122
import org.springframework.batch.core.repository.JobRepository;
2223
import org.springframework.batch.core.scope.StepScope;
@@ -70,6 +71,9 @@ public StepBuilderFactory stepBuilders() throws Exception {
7071
@Bean
7172
public abstract JobLauncher jobLauncher() throws Exception;
7273

74+
@Bean
75+
public abstract JobExplorer jobExplorer() throws Exception;
76+
7377
@Bean
7478
public JobRegistry jobRegistry() throws Exception {
7579
return new MapJobRegistry();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.configuration.annotation;
1717

18+
import org.springframework.batch.core.explore.JobExplorer;
1819
import org.springframework.batch.core.launch.JobLauncher;
1920
import org.springframework.batch.core.repository.JobRepository;
2021
import org.springframework.transaction.PlatformTransactionManager;
@@ -33,4 +34,5 @@ public interface BatchConfigurer {
3334

3435
JobLauncher getJobLauncher() throws Exception;
3536

37+
JobExplorer getJobExplorer() throws Exception;
3638
}

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717

1818
import org.apache.commons.logging.Log;
1919
import org.apache.commons.logging.LogFactory;
20-
import org.springframework.batch.core.configuration.BatchConfigurationException;
21-
import org.springframework.stereotype.Component;
22-
23-
import javax.annotation.PostConstruct;
24-
import javax.sql.DataSource;
25-
20+
import org.springframework.batch.core.explore.JobExplorer;
21+
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
22+
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
2623
import org.springframework.batch.core.launch.JobLauncher;
2724
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
2825
import org.springframework.batch.core.repository.JobRepository;
@@ -31,8 +28,12 @@
3128
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
3229
import org.springframework.beans.factory.annotation.Autowired;
3330
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
31+
import org.springframework.stereotype.Component;
3432
import org.springframework.transaction.PlatformTransactionManager;
3533

34+
import javax.annotation.PostConstruct;
35+
import javax.sql.DataSource;
36+
3637
@Component
3738
public class DefaultBatchConfigurer implements BatchConfigurer {
3839
private static final Log logger = LogFactory.getLog(DefaultBatchConfigurer.class);
@@ -41,6 +42,7 @@ public class DefaultBatchConfigurer implements BatchConfigurer {
4142
private PlatformTransactionManager transactionManager;
4243
private JobRepository jobRepository;
4344
private JobLauncher jobLauncher;
45+
private JobExplorer jobExplorer;
4446

4547
@Autowired(required = false)
4648
public void setDataSource(DataSource dataSource) {
@@ -69,6 +71,11 @@ public JobLauncher getJobLauncher() {
6971
return jobLauncher;
7072
}
7173

74+
@Override
75+
public JobExplorer getJobExplorer() {
76+
return jobExplorer;
77+
}
78+
7279
@PostConstruct
7380
public void initialize() throws Exception {
7481
if(dataSource == null) {
@@ -78,11 +85,20 @@ public void initialize() throws Exception {
7885
this.transactionManager = new ResourcelessTransactionManager();
7986
}
8087

81-
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(this.transactionManager);
82-
factory.afterPropertiesSet();
83-
this.jobRepository = factory.getObject();
88+
MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(this.transactionManager);
89+
jobRepositoryFactory.afterPropertiesSet();
90+
this.jobRepository = jobRepositoryFactory.getObject();
91+
92+
MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
93+
jobExplorerFactory.afterPropertiesSet();
94+
this.jobExplorer = jobExplorerFactory.getObject();
8495
} else {
8596
this.jobRepository = createJobRepository();
97+
98+
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
99+
jobExplorerFactoryBean.setDataSource(this.dataSource);
100+
jobExplorerFactoryBean.afterPropertiesSet();
101+
this.jobExplorer = jobExplorerFactoryBean.getObject();
86102
}
87103

88104
this.jobLauncher = createJobLauncher();

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
package org.springframework.batch.core.configuration.annotation;
1717

18-
import java.util.Collection;
19-
2018
import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
2119
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
2220
import org.springframework.batch.core.configuration.support.DefaultJobLoader;
21+
import org.springframework.batch.core.explore.JobExplorer;
2322
import org.springframework.batch.core.launch.JobLauncher;
2423
import org.springframework.batch.core.repository.JobRepository;
2524
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,6 +27,8 @@
2827
import org.springframework.context.annotation.Configuration;
2928
import org.springframework.transaction.PlatformTransactionManager;
3029

30+
import java.util.Collection;
31+
3132
/**
3233
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
3334
* available by implementing the {@link BatchConfigurer} interface.
@@ -65,6 +66,12 @@ public PlatformTransactionManager transactionManager() throws Exception {
6566
return getConfigurer(configurers).getTransactionManager();
6667
}
6768

69+
@Override
70+
@Bean
71+
public JobExplorer jobExplorer() throws Exception {
72+
return getConfigurer(configurers).getJobExplorer();
73+
}
74+
6875
@Bean
6976
public AutomaticJobRegistrar jobRegistrar() throws Exception {
7077
registrar.setJobLoader(new DefaultJobLoader(jobRegistry()));

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
*/
1616
package org.springframework.batch.core.configuration.annotation;
1717

18-
import java.util.concurrent.atomic.AtomicReference;
19-
2018
import org.aopalliance.intercept.MethodInterceptor;
2119
import org.aopalliance.intercept.MethodInvocation;
2220
import org.springframework.aop.framework.ProxyFactory;
2321
import org.springframework.aop.target.AbstractLazyCreationTargetSource;
2422
import org.springframework.batch.core.configuration.JobRegistry;
2523
import org.springframework.batch.core.configuration.support.MapJobRegistry;
24+
import org.springframework.batch.core.explore.JobExplorer;
2625
import org.springframework.batch.core.launch.JobLauncher;
2726
import org.springframework.batch.core.repository.JobRepository;
2827
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,6 +30,8 @@
3130
import org.springframework.context.annotation.Configuration;
3231
import org.springframework.transaction.PlatformTransactionManager;
3332

33+
import java.util.concurrent.atomic.AtomicReference;
34+
3435
/**
3536
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
3637
* available by implementing the {@link BatchConfigurer} interface. The main components are created as lazy proxies that
@@ -58,6 +59,8 @@ public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
5859

5960
private AtomicReference<PlatformTransactionManager> transactionManager = new AtomicReference<PlatformTransactionManager>();
6061

62+
private AtomicReference<JobExplorer> jobExplorer = new AtomicReference<JobExplorer>();
63+
6164
@Override
6265
@Bean
6366
public JobRepository jobRepository() throws Exception {
@@ -76,6 +79,12 @@ public JobRegistry jobRegistry() throws Exception {
7679
return createLazyProxy(jobRegistry, JobRegistry.class);
7780
}
7881

82+
@Override
83+
@Bean
84+
public JobExplorer jobExplorer() {
85+
return createLazyProxy(jobExplorer, JobExplorer.class);
86+
}
87+
7988
@Override
8089
@Bean
8190
public PlatformTransactionManager transactionManager() throws Exception {
@@ -107,6 +116,7 @@ protected void initialize() throws Exception {
107116
jobLauncher.set(configurer.getJobLauncher());
108117
transactionManager.set(configurer.getTransactionManager());
109118
jobRegistry.set(new MapJobRegistry());
119+
jobExplorer.set(configurer.getJobExplorer());
110120
initialized = true;
111121
}
112122

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package org.springframework.batch.core.configuration.annotation;
1717

18-
import javax.annotation.PostConstruct;
19-
import javax.sql.DataSource;
20-
21-
import org.springframework.batch.core.PooledEmbeddedDataSource;
2218
import org.springframework.batch.core.Step;
2319
import org.springframework.beans.factory.annotation.Autowired;
2420
import org.springframework.context.annotation.Bean;
@@ -30,6 +26,9 @@
3026
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
3127
import org.springframework.util.ClassUtils;
3228

29+
import javax.annotation.PostConstruct;
30+
import javax.sql.DataSource;
31+
3332
@Configuration
3433
public class DataSourceConfiguration {
3534

@@ -49,7 +48,7 @@ protected void initialize() {
4948

5049
@Bean
5150
public DataSource dataSource() {
52-
return new PooledEmbeddedDataSource(new EmbeddedDatabaseFactory().getDatabase());
51+
return new EmbeddedDatabaseFactory().getDatabase();
5352
}
5453

5554
}

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobLoaderConfigurationTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package org.springframework.batch.core.configuration.annotation;
1717

18-
import static org.junit.Assert.assertEquals;
19-
20-
import javax.annotation.PostConstruct;
21-
2218
import org.junit.Test;
2319
import org.springframework.batch.core.BatchStatus;
2420
import org.springframework.batch.core.Job;
@@ -30,6 +26,7 @@
3026
import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
3127
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
3228
import org.springframework.batch.core.configuration.support.GenericApplicationContextFactory;
29+
import org.springframework.batch.core.explore.JobExplorer;
3330
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
3431
import org.springframework.batch.core.launch.JobLauncher;
3532
import org.springframework.batch.core.scope.context.ChunkContext;
@@ -40,6 +37,10 @@
4037
import org.springframework.context.annotation.Bean;
4138
import org.springframework.context.annotation.Configuration;
4239

40+
import javax.annotation.PostConstruct;
41+
42+
import static org.junit.Assert.assertEquals;
43+
4344
/**
4445
* @author Dave Syer
4546
*
@@ -72,6 +73,8 @@ private void testJob(String jobName, BatchStatus status, int stepExecutionCount,
7273
.toJobParameters());
7374
assertEquals(status, execution.getStatus());
7475
assertEquals(stepExecutionCount, execution.getStepExecutions().size());
76+
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
77+
assertEquals(1, jobExplorer.getJobInstanceCount(jobName));
7578
context.close();
7679

7780
}

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/MapJobRepositoryConfigurationTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.batch.core.PooledEmbeddedDataSource;
2424
import org.springframework.batch.core.Step;
2525
import org.springframework.batch.core.StepContribution;
26+
import org.springframework.batch.core.explore.JobExplorer;
2627
import org.springframework.batch.core.launch.JobLauncher;
2728
import org.springframework.batch.core.repository.JobRepository;
2829
import org.springframework.batch.core.scope.context.ChunkContext;
@@ -44,6 +45,7 @@ public class MapJobRepositoryConfigurationTests {
4445
JobLauncher jobLauncher;
4546
JobRepository jobRepository;
4647
Job job;
48+
JobExplorer jobExplorer;
4749

4850
@Test
4951
public void testRoseyScenario() throws Exception {
@@ -65,11 +67,13 @@ private void testConfigurationClass(Class<?> clazz) throws Exception {
6567
this.jobLauncher = context.getBean(JobLauncher.class);
6668
this.jobRepository = context.getBean(JobRepository.class);
6769
this.job = context.getBean(Job.class);
70+
this.jobExplorer = context.getBean(JobExplorer.class);
6871

6972
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
7073
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
7174
JobExecution repositoryJobExecution = jobRepository.getLastJobExecution(job.getName(), new JobParameters());
7275
assertEquals(jobExecution.getId(), repositoryJobExecution.getId());
76+
assertEquals("job", jobExplorer.getJobNames().iterator().next());
7377
context.close();
7478
}
7579

0 commit comments

Comments
 (0)