OPEN - issue BATCH-324: Step as factory for StepExecutor

http://jira.springframework.org/browse/BATCH-324

Same treatment for JobExecutor - roll it up into the Job interface
This commit is contained in:
dsyer
2008-01-29 18:07:57 +00:00
parent 3d7f40ca90
commit 911e368a71
64 changed files with 486 additions and 581 deletions

View File

@@ -1,34 +0,0 @@
/*
* 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.domain;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
/**
* Interface for running a job.
*
* @author Lucas Ward
* @author Dave Syer
* @see Job
* @see JobExecution
*/
public interface JobExecutor {
public ExitStatus run(Job job, JobExecution execution) throws BatchCriticalException;
}

View File

@@ -19,6 +19,8 @@ package org.springframework.batch.core.domain;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.ClassUtils;
@@ -31,7 +33,7 @@ import org.springframework.util.ClassUtils;
* @author Lucas Ward
* @author Dave Syer
*/
public class Job implements BeanNameAware {
public class JobSupport implements BeanNameAware, Job {
private List steps = new ArrayList();
@@ -44,7 +46,7 @@ public class Job implements BeanNameAware {
/**
* Default constructor.
*/
public Job() {
public JobSupport() {
super();
}
@@ -54,7 +56,7 @@ public class Job implements BeanNameAware {
*
* @param name
*/
public Job(String name) {
public JobSupport(String name) {
super();
this.name = name;
}
@@ -84,10 +86,16 @@ public class Job implements BeanNameAware {
this.name = name;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.IJob#getName()
*/
public String getName() {
return name;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.IJob#getSteps()
*/
public List getSteps() {
return steps;
}
@@ -101,6 +109,9 @@ public class Job implements BeanNameAware {
this.steps.add(step);
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.IJob#getStartLimit()
*/
public int getStartLimit() {
return startLimit;
}
@@ -113,11 +124,21 @@ public class Job implements BeanNameAware {
this.restartable = restartable;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.IJob#isRestartable()
*/
public boolean isRestartable() {
return restartable;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.Job#run(org.springframework.batch.core.domain.JobExecution)
*/
public ExitStatus run(JobExecution execution) throws BatchCriticalException {
throw new UnsupportedOperationException("JobSupport does not provide an implementation of run(). Use a smarter subclass.");
}
public String toString() {
return ClassUtils.getShortName(Job.class) + ": [name=" + name + "]";
return ClassUtils.getShortName(JobSupport.class) + ": [name=" + name + "]";
}
}

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.batch.core.domain;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
/**
* Batch domain interface representing the configuration of a step. As with the
@@ -52,8 +55,16 @@ public interface Step {
int getStartLimit();
/**
* @return a {@link StepExecutor} that could be used to execute this step
* It is not safe to re-use an instance of {@link StepExecutor} to process
* multiple concurrent executions. Use the factory method in {@link Step} to
* create a new instance and execute that.
*
* @param stepExecution an entity representing the step to be executed
*
* @throws StepInterruptedException if the step is interrupted externally
* @throws BatchCriticalException if there is a problem that needs to be
* signalled to the caller
*/
StepExecutor createStepExecutor();
ExitStatus process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException;
}

View File

@@ -1,50 +0,0 @@
/*
* 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.domain;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
/**
* Interface for processing a step. Implementations are free to process the step
* and return when finished, or to schedule the step for processing
* concurrently, or in the future. The status of the execution should be
* trackable with the step execution. The step should be treated as immutable.<br/>
*
* Because step execution parameters and policies can vary from step to step, a
* {@link StepExecutor} should be created by the caller using a {@link Step}.
*
* @author Lucas Ward
* @author Dave Syer
*
*/
public interface StepExecutor {
/**
* It is not safe to re-use an instance of {@link StepExecutor} to process
* multiple concurrent executions. Use the factory method in {@link Step} to
* create a new instance and execute that.
*
* @param stepExecution an entity representing the step to be executed
*
* @throws StepInterruptedException if the step is interrupted externally
* @throws BatchCriticalException if there is a problem that needs to be
* signalled to the caller
*/
ExitStatus process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException;
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.batch.core.domain;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.beans.factory.BeanNameAware;
/**
@@ -133,10 +134,11 @@ public class StepSupport implements Step, BeanNameAware {
*
* @throws UnsupportedOperationException always
*
* @see org.springframework.batch.core.domain.Step#createStepExecutor()
* @see org.springframework.batch.core.domain.Step#process(org.springframework.batch.core.domain.StepExecution)
*/
public StepExecutor createStepExecutor() {
public org.springframework.batch.repeat.ExitStatus process(StepExecution stepExecution)
throws StepInterruptedException, BatchCriticalException {
throw new UnsupportedOperationException(
"Cannot create a StepExecutor. Use a smarter subclass of StepSupport like a SimpleStep.");
"Cannot process a StepExecution. Use a smarter subclass of StepSupport.");
}
}

View File

@@ -15,11 +15,11 @@
*/
package org.springframework.batch.core.repository;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
/**
* Checked exception that indicates a name clash when registering
* {@link Job} instances.
* {@link JobSupport} instances.
*
* @author Dave Syer
*

View File

@@ -15,10 +15,10 @@
*/
package org.springframework.batch.core.repository;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
/**
* Base class for checked exceptions related to {@link Job}
* Base class for checked exceptions related to {@link JobSupport}
* creation, registration or use.
*
* @author Dave Syer

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.repository;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
/**
* A runtime service locator interface for retrieving job configurations by
@@ -27,11 +28,11 @@ import org.springframework.batch.core.domain.Job;
public interface JobLocator {
/**
* Locates a {@link Job} at runtime.
* Locates a {@link JobSupport} at runtime.
*
* @param name the name of the {@link Job} which should be
* @param name the name of the {@link JobSupport} which should be
* unique
* @return a {@link Job} identified by the given name
* @return a {@link JobSupport} identified by the given name
*
* @throws NoSuchJobException if the required configuratio can
* not be found.

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.repository;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
/**
* A runtime service registry interface for registering job configurations by
@@ -27,9 +28,9 @@ import org.springframework.batch.core.domain.Job;
public interface JobRegistry extends JobLocator {
/**
* Registers a {@link Job} at runtime.
* Registers a {@link JobSupport} at runtime.
*
* @param jobConfiguration the {@link Job} to be registered
* @param jobConfiguration the {@link JobSupport} to be registered
*
* @throws DuplicateJobException if a configuration with the
* same name has already been registered.
@@ -37,10 +38,10 @@ public interface JobRegistry extends JobLocator {
void register(Job jobConfiguration) throws DuplicateJobException;
/**
* Unregisters a previously registered {@link Job}. If it was
* Unregisters a previously registered {@link JobSupport}. If it was
* not previously registered there is no error.
*
* @param jobConfiguration the {@link Job} to unregister.
* @param jobConfiguration the {@link JobSupport} to unregister.
*/
void unregister(Job jobConfiguration);
}

View File

@@ -17,7 +17,7 @@ package org.springframework.batch.core.repository;
import java.util.Collection;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
/**
* A listable extension of {@link JobRegistry}.
@@ -31,7 +31,7 @@ public interface ListableJobRegistry extends JobRegistry {
* Provides the currently registered configurations. The return value is
* unmodifiable and disconnected from the underlying registry storage.
*
* @return a collection of {@link Job} instances. Empty if none
* @return a collection of {@link JobSupport} instances. Empty if none
* are registered.
*/
Collection getJobConfigurations();

View File

@@ -15,11 +15,11 @@
*/
package org.springframework.batch.core.repository;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
/**
* Checked exception to indicate that a required {@link Job} is not
* Checked exception to indicate that a required {@link JobSupport} is not
* available.
*
* @author Dave Syer

View File

@@ -29,7 +29,7 @@ public class JobExecutionTests extends TestCase {
private JobExecution execution = new JobExecution(new JobInstance(new Long(11), new JobParameters()), new Long(12));
private JobExecution context = new JobExecution(new JobInstance(new Long(11), new JobParameters(), new Job("foo")), new Long(12));
private JobExecution context = new JobExecution(new JobInstance(new Long(11), new JobParameters(), new JobSupport("foo")), new Long(12));
/**
* Test method for

View File

@@ -25,7 +25,7 @@ import junit.framework.TestCase;
*/
public class JobInstanceTests extends TestCase {
private JobInstance instance = new JobInstance(new Long(11), new JobParameters(), new Job("job"));
private JobInstance instance = new JobInstance(new Long(11), new JobParameters(), new JobSupport("job"));
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getStatus()}.
@@ -66,7 +66,7 @@ public class JobInstanceTests extends TestCase {
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getIdentifier()}.
*/
public void testGetName() {
instance = new JobInstance(new Long(1), new JobParameters(), new Job("foo"));
instance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("foo"));
assertEquals("foo", instance.getJobName());
}

View File

@@ -0,0 +1,100 @@
/*
* 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.domain;
import java.util.Collections;
import org.springframework.batch.core.domain.JobSupport;
import org.springframework.batch.core.domain.StepSupport;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class JobSupportTests extends TestCase {
JobSupport job = new JobSupport("job");
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#JobConfiguration()}.
*/
public void testJobConfiguration() {
job = new JobSupport();
assertNull(job.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#setBeanName(java.lang.String)}.
*/
public void testSetBeanName() {
job.setBeanName("foo");
assertEquals("job", job.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#setBeanName(java.lang.String)}.
*/
public void testSetBeanNameWithNullName() {
job = new JobSupport(null);
assertEquals(null, job.getName());
job.setBeanName("foo");
assertEquals("foo", job.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#setSteps(java.util.List)}.
*/
public void testSetSteps() {
job.setSteps(Collections.singletonList(new StepSupport("step")));
assertEquals(1, job.getSteps().size());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#addStepInstance(org.springframework.batch.core.configuration.StepConfiguration)}.
*/
public void testAddStep() {
job.addStep(new StepSupport("step"));
assertEquals(1, job.getSteps().size());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#setStartLimit(int)}.
*/
public void testSetStartLimit() {
assertEquals(Integer.MAX_VALUE, job.getStartLimit());
job.setStartLimit(10);
assertEquals(10, job.getStartLimit());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.JobSupport#setRestartable(boolean)}.
*/
public void testSetRestartable() {
assertFalse(job.isRestartable());
job.setRestartable(true);
assertTrue(job.isRestartable());
}
}

View File

@@ -1,100 +0,0 @@
/*
* 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.domain;
import java.util.Collections;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.StepSupport;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class JobTests extends TestCase {
Job configuration = new Job("job");
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#JobConfiguration()}.
*/
public void testJobConfiguration() {
configuration = new Job();
assertNull(configuration.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#setBeanName(java.lang.String)}.
*/
public void testSetBeanName() {
configuration.setBeanName("foo");
assertEquals("job", configuration.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#setBeanName(java.lang.String)}.
*/
public void testSetBeanNameWithNullName() {
configuration = new Job(null);
assertEquals(null, configuration.getName());
configuration.setBeanName("foo");
assertEquals("foo", configuration.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#setSteps(java.util.List)}.
*/
public void testSetSteps() {
configuration.setSteps(Collections.singletonList(new StepSupport("step")));
assertEquals(1, configuration.getSteps().size());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#addStepInstance(org.springframework.batch.core.configuration.StepConfiguration)}.
*/
public void testAddStep() {
configuration.addStep(new StepSupport("step"));
assertEquals(1, configuration.getSteps().size());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#setStartLimit(int)}.
*/
public void testSetStartLimit() {
assertEquals(Integer.MAX_VALUE, configuration.getStartLimit());
configuration.setStartLimit(10);
assertEquals(10, configuration.getStartLimit());
}
/**
* Test method for
* {@link org.springframework.batch.core.domain.Job#setRestartable(boolean)}.
*/
public void testSetRestartable() {
assertFalse(configuration.isRestartable());
configuration.setRestartable(true);
assertTrue(configuration.isRestartable());
}
}

View File

@@ -18,7 +18,7 @@ package org.springframework.batch.core.domain;
import junit.framework.TestCase;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobSupport;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -29,7 +29,7 @@ public class SpringBeanJobTests extends TestCase {
public void testBeanName() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
Job configuration = new Job();
JobSupport configuration = new JobSupport();
context.getAutowireCapableBeanFactory().initializeBean(configuration,
"bean");
assertNotNull(configuration.getName());
@@ -44,8 +44,8 @@ public class SpringBeanJobTests extends TestCase {
ConstructorArgumentValues args = new ConstructorArgumentValues();
args.addGenericArgumentValue("foo");
context.registerBeanDefinition("bean", new RootBeanDefinition(
Job.class, args, null));
Job configuration = (Job) context
JobSupport.class, args, null));
JobSupport configuration = (JobSupport) context
.getBean("bean");
assertNotNull(configuration.getName());
assertEquals("foo", configuration.getName());
@@ -58,9 +58,9 @@ public class SpringBeanJobTests extends TestCase {
ConstructorArgumentValues args = new ConstructorArgumentValues();
args.addGenericArgumentValue("bar");
context.registerBeanDefinition("parent", new RootBeanDefinition(
Job.class, args, null));
JobSupport.class, args, null));
context.registerBeanDefinition("bean", new ChildBeanDefinition("parent"));
Job configuration = (Job) context
JobSupport configuration = (JobSupport) context
.getBean("bean");
assertNotNull(configuration.getName());
assertEquals("bar", configuration.getName());

View File

@@ -78,7 +78,7 @@ public class StepInstanceTests extends TestCase {
public void testGetJob(){
Job job = new Job("job");
Job job = new JobSupport("job");
JobInstance jobInstance = new JobInstance(new Long(2), new JobParameters(), job);
instance = new StepInstance(jobInstance, null);
assertEquals(job, instance.getJobInstance().getJob());

View File

@@ -62,14 +62,14 @@ public class StepSupportTests extends TestCase {
public void testUnsuccessfulWrongConfiguration() throws Exception {
try {
new StepSupport().createStepExecutor();
new StepSupport().process(null);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
assertTrue(
"Error message does not contain SimpleStep: "
"Error message does not contain StepExecution: "
+ e.getMessage(), e.getMessage().indexOf(
"SimpleStep") >= 0);
"StepExecution") >= 0);
}
}