From ac08658c488cd3e7f7f44616fd9f42bfcf90bf9e Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 8 Sep 2009 07:00:31 +0000 Subject: [PATCH] OPEN - issue BATCH-1338: Allow segregation of jobs by type or origin Add group aware job factory and re-org interfaces in JobLocator-JobRegistry (not backwards compatible but probably not a change for anyone outside the framework) --- .../batch/core/configuration/JobRegistry.java | 2 +- ...bRegistry.java => ListableJobLocator.java} | 4 +- .../support/AbstractGroupAwareJobFactory.java | 161 ++++++++++++++++++ .../support/ApplicationContextJobFactory.java | 77 +-------- .../support/ClassPathXmlJobRegistry.java | 42 ++--- .../support/JobRegistryBeanPostProcessor.java | 10 +- .../configuration/support/MapJobRegistry.java | 3 +- .../support/ReferenceJobFactory.java | 21 +-- .../launch/support/SimpleJobOperator.java | 10 +- .../ApplicationContextJobFactoryTests.java | 2 +- .../support/GroupAwareJobFactoryTests.java | 56 ++++++ .../JobRegistryBeanPostProcessorTests.java | 55 +++--- .../xml/JobRegistryJobParserTests.java | 4 +- .../batch/sample/launch/DefaultJobLoader.java | 6 +- .../sample/JobOperatorFunctionalTests.java | 4 +- 15 files changed, 283 insertions(+), 174 deletions(-) rename spring-batch-core/src/main/java/org/springframework/batch/core/configuration/{ListableJobRegistry.java => ListableJobLocator.java} (90%) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractGroupAwareJobFactory.java create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GroupAwareJobFactoryTests.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/JobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/JobRegistry.java index d1fad94c3..341575064 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/JobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/JobRegistry.java @@ -24,7 +24,7 @@ import org.springframework.batch.core.Job; * @author Dave Syer * */ -public interface JobRegistry extends JobLocator { +public interface JobRegistry extends ListableJobLocator { /** * Registers a {@link Job} at runtime. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/ListableJobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/ListableJobLocator.java similarity index 90% rename from spring-batch-core/src/main/java/org/springframework/batch/core/configuration/ListableJobRegistry.java rename to spring-batch-core/src/main/java/org/springframework/batch/core/configuration/ListableJobLocator.java index 841d79759..1a017a448 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/ListableJobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/ListableJobLocator.java @@ -18,12 +18,12 @@ package org.springframework.batch.core.configuration; import java.util.Collection; /** - * A listable extension of {@link JobRegistry}. + * A listable extension of {@link JobLocator}. * * @author Dave Syer * */ -public interface ListableJobRegistry extends JobRegistry { +public interface ListableJobLocator extends JobLocator { /** * Provides the currently registered job names. The return value is diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractGroupAwareJobFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractGroupAwareJobFactory.java new file mode 100644 index 000000000..6042076be --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractGroupAwareJobFactory.java @@ -0,0 +1,161 @@ +/* + * Copyright 2006-2007 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 + * + * http://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.batch.core.configuration.support; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParametersIncrementer; +import org.springframework.batch.core.configuration.JobFactory; + +/** + * A {@link JobFactory} that can optionally prepend a group name to a job's + * name, to make it fit a naming convention for type or origin. E.g. the source + * job might be overnightJob and the group + * financeDepartment, which would result in a {@link Job} with + * identical functionality but named financeDepartment$overnightJob + * . The use of a "$" separator for elements is deliberate, since it is a "safe" + * character in a URL. + * + * + * @author Dave Syer + * + */ +public abstract class AbstractGroupAwareJobFactory implements JobFactory { + + private final String groupName; + + private final String jobName; + + /** + * Create a job factory for a job in no particular group. + * + * @param jobName the name of the job + */ + public AbstractGroupAwareJobFactory(String jobName) { + this(null, jobName); + } + + /** + * Create a job factory for a job in the group provided. The {@link Job} + * eventually returned from {@link #createJob()} will have a name composed + * of the group and job names together. + * + * @param groupName the name of the group + * @param jobName the name of the job + */ + public AbstractGroupAwareJobFactory(String groupName, String jobName) { + this.groupName = groupName; + this.jobName = jobName; + } + + /** + * The main factory method. Delegates to {@link #doCreateJob(String)} to + * create a delegate, which is then wrapped to give a job with the same + * functionality but a composite name (if the group is specified). + * + * @see org.springframework.batch.core.configuration.JobFactory#createJob() + */ + public final Job createJob() { + Job job = doCreateJob(jobName); + return groupName == null ? job : new GroupAwareJob(groupName, job); + } + + /** + * Extension point for concrete subclasses. + * + * @return a Job from the provided name + */ + protected abstract Job doCreateJob(String jobName); + + /** + * Return the bean name of the job in the application context. N.B. this is + * usually the name of the job as well, but it needn't be. The important + * thing is that the job can be located by this name. + * + * @see org.springframework.batch.core.configuration.JobFactory#getJobName() + */ + public final String getJobName() { + return groupName == null ? jobName : groupName + "$" + jobName; + } + + /** + * @author Dave Syer + * + */ + private static class GroupAwareJob implements Job { + + private final Job delegate; + + private final String groupName; + + /** + * @param groupName + * @param delegate + */ + public GroupAwareJob(String groupName, Job delegate) { + super(); + this.groupName = groupName; + this.delegate = delegate; + } + + public void execute(JobExecution execution) { + delegate.execute(execution); + } + + /** + * Concatenates the group name and the delegate job name (joining with a + * "$"). + * + * @see org.springframework.batch.core.Job#getName() + */ + public String getName() { + return groupName + "$" + delegate.getName(); + } + + public boolean isRestartable() { + return delegate.isRestartable(); + } + + public JobParametersIncrementer getJobParametersIncrementer() { + return delegate.getJobParametersIncrementer(); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (obj instanceof GroupAwareJob) { + return ((GroupAwareJob) obj).delegate.equals(delegate); + } + return false; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return delegate.hashCode(); + } + + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactory.java index 09e143245..86d87eeda 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactory.java @@ -16,32 +16,27 @@ package org.springframework.batch.core.configuration.support; import org.springframework.batch.core.Job; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobParametersIncrementer; import org.springframework.batch.core.configuration.JobFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; /** - * A {@link JobFactory} that creates its own {@link ApplicationContext} from a - * path supplied, and pulls a bean out when asked to create a {@link Job}. + * A {@link JobFactory} that creates its own {@link ApplicationContext} and + * pulls a bean out when asked to create a {@link Job}. * * @author Dave Syer * */ -public class ApplicationContextJobFactory implements JobFactory { +public class ApplicationContextJobFactory extends AbstractGroupAwareJobFactory { - final private String jobName; - - final private ApplicationContextFactory applicationContextFactory; + final ApplicationContextFactory applicationContextFactory; /** * @param jobName the id of the {@link Job} in the application context to be * created */ public ApplicationContextJobFactory(ApplicationContextFactory applicationContextFactory, String jobName) { - super(); - this.jobName = jobName; + super(jobName); this.applicationContextFactory = applicationContextFactory; } @@ -51,67 +46,11 @@ public class ApplicationContextJobFactory implements JobFactory { * * @see org.springframework.batch.core.configuration.JobFactory#createJob() */ - public Job createJob() { + @Override + protected Job doCreateJob(String jobName) { ConfigurableApplicationContext context = applicationContextFactory.createApplicationContext(); Job job = (Job) context.getBean(jobName, Job.class); - return new ContextAwareJob(job); - } - - /** - * Return the bean name of the job in the application context. N.B. this is - * usually the name of the job as well, but it needn't be. The important - * thing is that the job can be located by this name. - * - * @see org.springframework.batch.core.configuration.JobFactory#getJobName() - */ - public String getJobName() { - return jobName; - } - - /** - * @author Dave Syer - * - */ - private static class ContextAwareJob implements Job { - private Job delegate; - - /** - * @param delegate - */ - public ContextAwareJob(Job delegate) { - super(); - this.delegate = delegate; - } - - /** - * @param execution - * @see org.springframework.batch.core.Job#execute(org.springframework.batch.core.JobExecution) - */ - public void execute(JobExecution execution) { - delegate.execute(execution); - } - - /** - * @see org.springframework.batch.core.Job#getName() - */ - public String getName() { - return delegate.getName(); - } - - /** - * @see org.springframework.batch.core.Job#isRestartable() - */ - public boolean isRestartable() { - return delegate.isRestartable(); - } - - /** - * @see org.springframework.batch.core.Job#getJobParametersIncrementer() - */ - public JobParametersIncrementer getJobParametersIncrementer() { - return delegate.getJobParametersIncrementer(); - } - + return job; } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java index 9377feb05..36ce1791f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java @@ -24,9 +24,8 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.Job; -import org.springframework.batch.core.configuration.DuplicateJobException; -import org.springframework.batch.core.configuration.JobFactory; -import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.configuration.JobRegistry; +import org.springframework.batch.core.configuration.ListableJobLocator; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.DisposableBean; @@ -37,7 +36,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.io.Resource; /** - * Implementation of the {@link ListableJobRegistry} interface that assumes all + * Implementation of the {@link ListableJobLocator} interface that assumes all * Jobs will be loaded from class path xml resources. Each resource provided is * loaded as an application context with the current context as its parent, and * then all the jobs from the child context are registered under their bean @@ -47,7 +46,7 @@ import org.springframework.core.io.Resource; * @author Dave Syer * @since 2.0 */ -public class ClassPathXmlJobRegistry implements ListableJobRegistry, ApplicationContextAware, InitializingBean, +public class ClassPathXmlJobRegistry implements ListableJobLocator, ApplicationContextAware, InitializingBean, DisposableBean { private static Log logger = LogFactory.getLog(ClassPathXmlJobRegistry.class); @@ -56,7 +55,7 @@ public class ClassPathXmlJobRegistry implements ListableJobRegistry, Application private ApplicationContext parent; - private ListableJobRegistry jobRegistry = new MapJobRegistry(); + private JobRegistry jobRegistry = new MapJobRegistry(); private Collection contexts = new HashSet(); @@ -134,37 +133,18 @@ public class ClassPathXmlJobRegistry implements ListableJobRegistry, Application */ public void destroy() throws Exception { - try { - - for (ConfigurableApplicationContext context : contexts) { - - String[] names = context.getBeanNamesForType(Job.class); - - try { - for (String name : names) { - unregister(name); - } - } - finally { - context.close(); - } - - } + for (ConfigurableApplicationContext context : contexts) { + context.close(); } - finally { - contexts.clear(); + for (String jobName : jobRegistry.getJobNames()) { + jobRegistry.unregister(jobName); } + contexts.clear(); + } public Collection getJobNames() { return jobRegistry.getJobNames(); } - public void register(JobFactory jobFactory) throws DuplicateJobException { - jobRegistry.register(jobFactory); - } - - public void unregister(String jobName) { - jobRegistry.unregister(jobName); - } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java index 6ca62ad61..c541a9880 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java @@ -42,7 +42,7 @@ import org.springframework.util.Assert; public class JobRegistryBeanPostProcessor implements BeanPostProcessor, InitializingBean, DisposableBean { // It doesn't make sense for this to have a default value... - private JobRegistry jobConfigurationRegistry = null; + private JobRegistry jobRegistry = null; private Collection jobNames = new HashSet(); @@ -52,7 +52,7 @@ public class JobRegistryBeanPostProcessor implements BeanPostProcessor, Initiali * @param jobRegistry the jobConfigurationRegistry to set */ public void setJobRegistry(JobRegistry jobRegistry) { - this.jobConfigurationRegistry = jobRegistry; + this.jobRegistry = jobRegistry; } /** @@ -61,7 +61,7 @@ public class JobRegistryBeanPostProcessor implements BeanPostProcessor, Initiali * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { - Assert.notNull(jobConfigurationRegistry, "JobConfigurationRegistry must not be null"); + Assert.notNull(jobRegistry, "JobRegistry must not be null"); } /** @@ -71,7 +71,7 @@ public class JobRegistryBeanPostProcessor implements BeanPostProcessor, Initiali */ public void destroy() throws Exception { for (String name : jobNames) { - jobConfigurationRegistry.unregister(name); + jobRegistry.unregister(name); } jobNames.clear(); } @@ -88,7 +88,7 @@ public class JobRegistryBeanPostProcessor implements BeanPostProcessor, Initiali if (bean instanceof Job) { Job job = (Job) bean; try { - jobConfigurationRegistry.register(new ReferenceJobFactory(job)); + jobRegistry.register(new ReferenceJobFactory(job)); jobNames.add(job.getName()); } catch (DuplicateJobException e) { diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java index 32baf51ec..67ee4bfff 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java @@ -25,7 +25,6 @@ import org.springframework.batch.core.Job; import org.springframework.batch.core.configuration.DuplicateJobException; import org.springframework.batch.core.configuration.JobFactory; import org.springframework.batch.core.configuration.JobRegistry; -import org.springframework.batch.core.configuration.ListableJobRegistry; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.util.Assert; @@ -36,7 +35,7 @@ import org.springframework.util.Assert; * @author Dave Syer * */ -public class MapJobRegistry implements ListableJobRegistry { +public class MapJobRegistry implements JobRegistry { private Map map = new HashMap(); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ReferenceJobFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ReferenceJobFactory.java index 07fedf384..56f5e0b34 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ReferenceJobFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ReferenceJobFactory.java @@ -25,37 +25,24 @@ import org.springframework.batch.core.configuration.JobFactory; * @author Dave Syer * */ -public class ReferenceJobFactory implements JobFactory { +public class ReferenceJobFactory extends AbstractGroupAwareJobFactory { private Job job; - private String name; - /** * @param job the {@link Job} to return from {@link #createJob()}. */ public ReferenceJobFactory(Job job) { - super(); + super(job.getName()); this.job = job; - this.name = job.getName(); } /** * Just return the instance passed in on initialization. - * - * @see org.springframework.batch.core.configuration.JobFactory#createJob() */ - public Job createJob() { + @Override + protected Job doCreateJob(String jobName) { return job; } - /** - * Returns the job name as passed in on initialization. - * - * @see org.springframework.batch.core.configuration.JobFactory#getJobName() - */ - public String getJobName() { - return name; - } - } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java index 34cc30f13..a48670a9c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java @@ -34,7 +34,7 @@ import org.springframework.batch.core.JobParametersIncrementer; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.UnexpectedJobExecutionException; import org.springframework.batch.core.configuration.JobRegistry; -import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.configuration.ListableJobLocator; import org.springframework.batch.core.converter.DefaultJobParametersConverter; import org.springframework.batch.core.converter.JobParametersConverter; import org.springframework.batch.core.explore.JobExplorer; @@ -79,7 +79,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean { private static final String ILLEGAL_STATE_MSG = "Illegal state (only happens on a race condition): " + "%s with name=%s and parameters=%s"; - private ListableJobRegistry jobRegistry; + private ListableJobLocator jobRegistry; private JobExplorer jobExplorer; @@ -112,10 +112,10 @@ public class SimpleJobOperator implements JobOperator, InitializingBean { } /** - * Public setter for the {@link ListableJobRegistry}. - * @param jobRegistry the {@link ListableJobRegistry} to set + * Public setter for the {@link ListableJobLocator}. + * @param jobRegistry the {@link ListableJobLocator} to set */ - public void setJobRegistry(ListableJobRegistry jobRegistry) { + public void setJobRegistry(ListableJobLocator jobRegistry) { this.jobRegistry = jobRegistry; } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactoryTests.java index 0a8a1d0cf..74dfb69f8 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ApplicationContextJobFactoryTests.java @@ -9,7 +9,7 @@ import org.springframework.context.support.StaticApplicationContext; public class ApplicationContextJobFactoryTests { - private ApplicationContextJobFactory factory = new ApplicationContextJobFactory( + private AbstractGroupAwareJobFactory factory = new ApplicationContextJobFactory( new StubApplicationContextFactory(), "job"); @Test diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GroupAwareJobFactoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GroupAwareJobFactoryTests.java new file mode 100644 index 000000000..7e758f1b4 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GroupAwareJobFactoryTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2006-2007 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 + * + * http://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.batch.core.configuration.support; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.JobSupport; + +/** + * @author Dave Syer + * + */ +public class GroupAwareJobFactoryTests { + + private Job job = new JobSupport("foo"); + + @Test + public void testCreateJob() { + AbstractGroupAwareJobFactory factory = new AbstractGroupAwareJobFactory(job.getName()) { + @Override + protected Job doCreateJob(String jobName) { + return job; + } + }; + assertEquals(factory.getJobName(), factory.createJob().getName()); + assertEquals(job, factory.createJob()); + } + + @Test + public void testGetJobName() { + AbstractGroupAwareJobFactory factory = new AbstractGroupAwareJobFactory("jobs", job.getName()) { + @Override + protected Job doCreateJob(String jobName) { + return job; + } + }; + assertEquals(factory.getJobName(), factory.createJob().getName()); + assertEquals("jobs$foo", factory.getJobName()); + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessorTests.java index 9eea071f9..47415ae14 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessorTests.java @@ -20,8 +20,6 @@ import java.util.Collection; import junit.framework.TestCase; import org.springframework.batch.core.configuration.DuplicateJobException; -import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor; -import org.springframework.batch.core.configuration.support.MapJobRegistry; import org.springframework.batch.core.job.JobSupport; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.beans.FatalBeanException; @@ -39,22 +37,21 @@ public class JobRegistryBeanPostProcessorTests extends TestCase { try { processor.afterPropertiesSet(); fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { // expected - assertTrue(e.getMessage().indexOf("JobConfigurationRegistry") >= 0); + assertTrue(e.getMessage().contains("JobRegistry")); } } public void testBeforeInitialization() throws Exception { // should be a no-op - assertEquals("foo", processor.postProcessAfterInitialization("foo", - "bar")); + assertEquals("foo", processor.postProcessAfterInitialization("foo", "bar")); } public void testAfterInitializationWithWrongType() throws Exception { // should be a no-op - assertEquals("foo", processor.postProcessAfterInitialization("foo", - "bar")); + assertEquals("foo", processor.postProcessAfterInitialization("foo", "bar")); } public void testAfterInitializationWithCorrectType() throws Exception { @@ -62,9 +59,8 @@ public class JobRegistryBeanPostProcessorTests extends TestCase { processor.setJobRegistry(registry); JobSupport configuration = new JobSupport(); configuration.setBeanName("foo"); - assertEquals(configuration, processor.postProcessAfterInitialization( - configuration, "bar")); - assertEquals(configuration, registry.getJob("foo")); + assertEquals(configuration, processor.postProcessAfterInitialization(configuration, "bar")); + assertEquals(configuration.getName(), registry.getJob("foo").getName()); } public void testAfterInitializationWithDuplicate() throws Exception { @@ -76,7 +72,8 @@ public class JobRegistryBeanPostProcessorTests extends TestCase { try { processor.postProcessAfterInitialization(configuration, "spam"); fail("Expected FatalBeanException"); - } catch (FatalBeanException e) { + } + catch (FatalBeanException e) { // Expected assertTrue(e.getCause() instanceof DuplicateJobException); } @@ -87,22 +84,20 @@ public class JobRegistryBeanPostProcessorTests extends TestCase { processor.setJobRegistry(registry); JobSupport configuration = new JobSupport(); configuration.setBeanName("foo"); - assertEquals(configuration, processor.postProcessAfterInitialization( - configuration, "bar")); + assertEquals(configuration, processor.postProcessAfterInitialization(configuration, "bar")); processor.destroy(); try { assertEquals(null, registry.getJob("foo")); fail("Expected NoSuchJobConfigurationException"); - } catch (NoSuchJobException e) { + } + catch (NoSuchJobException e) { // expected } } public void testExecutionWithApplicationContext() throws Exception { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "test-context.xml", getClass()); - MapJobRegistry registry = (MapJobRegistry) context - .getBean("registry"); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml", getClass()); + MapJobRegistry registry = (MapJobRegistry) context.getBean("registry"); Collection configurations = registry.getJobNames(); // System.err.println(configurations); String[] names = context.getBeanNamesForType(JobSupport.class); @@ -114,20 +109,12 @@ public class JobRegistryBeanPostProcessorTests extends TestCase { // child beans will have the same name and will be re-registered (and // override, if the registry supports that). assertNotNull(registry.getJob("test-job")); - assertEquals(context.getBean("test-job-with-name"), registry - .getJob("foo")); - assertEquals(context.getBean("test-job-with-bean-name"), registry - .getJob("bar")); - assertEquals(context.getBean("test-job-with-parent-and-name"), registry - .getJob("spam")); - assertEquals(context.getBean("test-job-with-parent-and-bean-name"), - registry.getJob("bucket")); - assertEquals(context.getBean("test-job-with-concrete-parent"), registry - .getJob("maps")); - assertEquals(context.getBean("test-job-with-concrete-parent-and-name"), - registry.getJob("oof")); - assertEquals(context - .getBean("test-job-with-concrete-parent-and-bean-name"), - registry.getJob("rab")); + assertEquals(context.getBean("test-job-with-name"), registry.getJob("foo")); + assertEquals(context.getBean("test-job-with-bean-name"), registry.getJob("bar")); + assertEquals(context.getBean("test-job-with-parent-and-name"), registry.getJob("spam")); + assertEquals(context.getBean("test-job-with-parent-and-bean-name"), registry.getJob("bucket")); + assertEquals(context.getBean("test-job-with-concrete-parent"), registry.getJob("maps")); + assertEquals(context.getBean("test-job-with-concrete-parent-and-name"), registry.getJob("oof")); + assertEquals(context.getBean("test-job-with-concrete-parent-and-bean-name"), registry.getJob("rab")); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobRegistryJobParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobRegistryJobParserTests.java index 4db9c4597..3df954178 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobRegistryJobParserTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobRegistryJobParserTests.java @@ -20,7 +20,7 @@ import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.batch.core.Job; -import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.configuration.ListableJobLocator; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; @@ -38,7 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; public class JobRegistryJobParserTests implements ApplicationContextAware { @Autowired - private ListableJobRegistry jobRegistry; + private ListableJobLocator jobRegistry; private ApplicationContext applicationContext; diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/launch/DefaultJobLoader.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/launch/DefaultJobLoader.java index 01d936af9..14054ad9d 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/launch/DefaultJobLoader.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/launch/DefaultJobLoader.java @@ -20,7 +20,7 @@ import java.util.HashMap; import java.util.Map; import org.springframework.batch.core.Job; -import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.configuration.ListableJobLocator; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.BeansException; @@ -32,7 +32,7 @@ import org.springframework.util.Assert; public class DefaultJobLoader implements JobLoader, ApplicationContextAware { - private ListableJobRegistry registry; + private ListableJobLocator registry; private ApplicationContext applicationContext; @@ -42,7 +42,7 @@ public class DefaultJobLoader implements JobLoader, ApplicationContextAware { this.applicationContext = applicationContext; } - public void setRegistry(ListableJobRegistry registry) { + public void setRegistry(ListableJobLocator registry) { this.registry = registry; } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java index 1b55134f3..91f6649e4 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java @@ -16,7 +16,7 @@ import org.junit.runner.RunWith; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobParametersBuilder; -import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.configuration.support.ReferenceJobFactory; import org.springframework.batch.core.launch.JobOperator; import org.springframework.beans.factory.annotation.Autowired; @@ -36,7 +36,7 @@ public class JobOperatorFunctionalTests { private Job job; @Autowired - private ListableJobRegistry jobRegistry; + private JobRegistry jobRegistry; @Before public void setUp() throws Exception {