From f2129d4ea704158eda592d636caa5afee6e4c655 Mon Sep 17 00:00:00 2001 From: dsyer Date: Sat, 1 Mar 2008 08:35:12 +0000 Subject: [PATCH] OPEN - issue BATCH-398: That old stateful / stateless thing again.... http://jira.springframework.org/browse/BATCH-398 Fix delegate job (duplicate job regsitry). --- .../batch/core/repository/JobFactory.java | 32 +++++++++++++++++++ .../batch/core/repository/JobRegistry.java | 8 ++--- .../core/repository/ListableJobRegistry.java | 9 ++---- .../configuration/MapJobRegistry.java | 4 +-- .../JobRegistryBeanPostProcessorTests.java | 2 +- .../configuration/MapJobRegistryTests.java | 6 ++-- .../src/main/resources/jobs/delegatingJob.xml | 8 +---- .../batch/sample/DefaultJobLoader.java | 15 ++++++--- .../batch/sample/TaskExecutorLauncher.java | 12 +++++-- 9 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobFactory.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobFactory.java new file mode 100644 index 000000000..d4ba1994c --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobFactory.java @@ -0,0 +1,32 @@ +/* + * 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.repository; + +import org.springframework.batch.core.domain.Job; + +/** + * Strategy for creating a single job. + * + * @author Dave Syer + * + */ +public interface JobFactory { + + Job createJob(); + + String getJobName(); + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRegistry.java index 2034f4558..3d2a909ee 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRegistry.java @@ -29,18 +29,18 @@ public interface JobRegistry extends JobLocator { /** * Registers a {@link Job} at runtime. * - * @param jobConfiguration the {@link Job} to be registered + * @param job the {@link Job} to be registered * * @throws DuplicateJobException if a configuration with the * same name has already been registered. */ - void register(Job jobConfiguration) throws DuplicateJobException; + void register(Job job) throws DuplicateJobException; /** * Unregisters a previously registered {@link Job}. If it was * not previously registered there is no error. * - * @param jobConfiguration the {@link Job} to unregister. + * @param job the {@link Job} to unregister. */ - void unregister(Job jobConfiguration); + void unregister(Job job); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/ListableJobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/ListableJobRegistry.java index 2632c4507..e4f0657b7 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/ListableJobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/ListableJobRegistry.java @@ -17,8 +17,6 @@ package org.springframework.batch.core.repository; import java.util.Collection; -import org.springframework.batch.core.domain.Job; - /** * A listable extension of {@link JobRegistry}. * @@ -28,11 +26,10 @@ import org.springframework.batch.core.domain.Job; public interface ListableJobRegistry extends JobRegistry { /** - * Provides the currently registered configurations. The return value is + * Provides the currently registered job names. The return value is * unmodifiable and disconnected from the underlying registry storage. * - * @return a collection of {@link Job} instances. Empty if none - * are registered. + * @return a collection of String. Empty if none are registered. */ - Collection getJobConfigurations(); + Collection getJobNames(); } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/configuration/MapJobRegistry.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/configuration/MapJobRegistry.java index 1df3bb876..e43ad882a 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/configuration/MapJobRegistry.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/configuration/MapJobRegistry.java @@ -88,9 +88,9 @@ public class MapJobRegistry implements ListableJobRegistry { /* (non-Javadoc) * @see org.springframework.batch.container.common.configuration.ListableJobConfigurationRegistry#getJobConfigurations() */ - public Collection getJobConfigurations() { + public Collection getJobNames() { synchronized (map) { - return Collections.unmodifiableCollection(new HashSet(map.values())); + return Collections.unmodifiableCollection(new HashSet(map.keySet())); } } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/JobRegistryBeanPostProcessorTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/JobRegistryBeanPostProcessorTests.java index 1c9342892..5d6907d2b 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/JobRegistryBeanPostProcessorTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/JobRegistryBeanPostProcessorTests.java @@ -101,7 +101,7 @@ public class JobRegistryBeanPostProcessorTests extends TestCase { "test-context.xml", getClass()); MapJobRegistry registry = (MapJobRegistry) context .getBean("registry"); - Collection configurations = registry.getJobConfigurations(); + Collection configurations = registry.getJobNames(); // System.err.println(configurations); String[] names = context.getBeanNamesForType(JobSupport.class); int count = names.length; diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/MapJobRegistryTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/MapJobRegistryTests.java index 791adb3f2..828605644 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/MapJobRegistryTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/configuration/MapJobRegistryTests.java @@ -81,16 +81,16 @@ public class MapJobRegistryTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.execution.configuration.MapJobRegistry#getJobConfigurations()}. + * Test method for {@link org.springframework.batch.execution.configuration.MapJobRegistry#getJobNames()}. * @throws Exception */ public void testGetJobConfigurations() throws Exception { Job configuration = new JobSupport("foo"); registry.register(configuration); registry.register(new JobSupport("bar")); - Collection configurations = registry.getJobConfigurations(); + Collection configurations = registry.getJobNames(); assertEquals(2, configurations.size()); - assertTrue(configurations.contains(configuration)); + assertTrue(configurations.contains(configuration.getName())); } } diff --git a/spring-batch-samples/src/main/resources/jobs/delegatingJob.xml b/spring-batch-samples/src/main/resources/jobs/delegatingJob.xml index 4589eaa3a..20318d781 100644 --- a/spring-batch-samples/src/main/resources/jobs/delegatingJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/delegatingJob.xml @@ -15,13 +15,7 @@ integrated into a batch job. - - - - - + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DefaultJobLoader.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DefaultJobLoader.java index 4c5f27a3c..2faeb8c94 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DefaultJobLoader.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DefaultJobLoader.java @@ -36,12 +36,17 @@ public class DefaultJobLoader implements JobLoader, public Map getConfigurations() { Map result = new HashMap(configurations); - for (Iterator iterator = registry.getJobConfigurations().iterator(); iterator + for (Iterator iterator = registry.getJobNames().iterator(); iterator .hasNext();) { - Job configuration = (Job) iterator.next(); - String name = configuration.getName(); - if (!configurations.containsKey(name)) { - result.put(name, ": " + configuration); + try { + Job configuration = (Job) registry.getJob((String) iterator.next()); + String name = configuration.getName(); + if (!configurations.containsKey(name)) { + result.put(name, ": " + configuration); + } + } + catch (NoSuchJobException e) { + throw new IllegalStateException("Registry could not locate its own job (NoSuchJobException)."); } } return result; diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskExecutorLauncher.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskExecutorLauncher.java index bdbe8b96a..aaa6a7c3c 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskExecutorLauncher.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskExecutorLauncher.java @@ -15,6 +15,7 @@ */ package org.springframework.batch.sample; +import org.springframework.batch.core.repository.JobRegistry; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -23,11 +24,16 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * */ public class TaskExecutorLauncher { + + private JobRegistry registry; + + private void register(String[] paths) { + // registry.register(jobConfiguration) + } public static void main(String[] args) throws Exception { - // Paths to individual job configurations. Each one must include the - // step scope and the jobConfigurationRegistryBeanPostProcessor. + // Paths to individual job configurations. final String[] paths = new String[] { "jobs/adhocLoopJob.xml", "jobs/footballJob.xml" }; @@ -39,6 +45,8 @@ public class TaskExecutorLauncher { // included in the paths above. final ApplicationContext parent = new ClassPathXmlApplicationContext( "adhoc-job-launcher-context.xml"); +// parent.getAutowireCapableBeanFactory().autowireBeanProperties( +// this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); new Thread(new Runnable() { public void run() {