RESOLVED - issue BATCH-945: Add support for step and job name in late binding
Also added some null checks in SimpleJobExplorer
This commit is contained in:
@@ -91,6 +91,9 @@ public class SimpleJobExplorer implements JobExplorer {
|
||||
*/
|
||||
public JobExecution getJobExecution(Long executionId) {
|
||||
JobExecution jobExecution = jobExecutionDao.getJobExecution(executionId);
|
||||
if (jobExecution==null) {
|
||||
return null;
|
||||
}
|
||||
getJobExecutionDependencies(jobExecution);
|
||||
return jobExecution;
|
||||
}
|
||||
@@ -100,6 +103,9 @@ public class SimpleJobExplorer implements JobExplorer {
|
||||
*/
|
||||
public StepExecution getStepExecution(Long executionId, String stepName) {
|
||||
JobExecution jobExecution = getJobExecution(executionId);
|
||||
if (jobExecution==null) {
|
||||
return null;
|
||||
}
|
||||
return stepExecutionDao.getStepExecution(jobExecution, stepName);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +21,11 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameter;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
@@ -57,6 +59,40 @@ public class StepContext extends SynchronizedAttributeAccessor {
|
||||
this.stepExecution = stepExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient accessor for current step name identifier. Usually this is the
|
||||
* same as the bean name of the step that is executing (but might not be
|
||||
* e.g. in a partition).
|
||||
*
|
||||
* @return the step name identifier of the current {@link StepExecution}
|
||||
*/
|
||||
public String getStepName() {
|
||||
return stepExecution.getStepName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient accessor for current job name identifier.
|
||||
*
|
||||
* @return the job name identifier of the enclosing {@link JobInstance}
|
||||
* associated with the current {@link StepExecution}
|
||||
*/
|
||||
public String getJobName() {
|
||||
Assert.state(stepExecution.getJobExecution() != null, "StepExecution does not have a JobExecution");
|
||||
Assert.state(stepExecution.getJobExecution().getJobInstance() != null,
|
||||
"StepExecution does not have a JobInstance");
|
||||
return stepExecution.getJobExecution().getJobInstance().getJobName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient accessor for System properties to make it easy to access them
|
||||
* from placeholder expressions.
|
||||
*
|
||||
* @return the current System properties
|
||||
*/
|
||||
public Properties getSystemProperties() {
|
||||
return System.getProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a map containing the items from the step {@link ExecutionContext}
|
||||
*/
|
||||
@@ -136,7 +172,7 @@ public class StepContext extends SynchronizedAttributeAccessor {
|
||||
|
||||
Map<String, Set<Runnable>> copy = Collections.unmodifiableMap(callbacks);
|
||||
|
||||
for(Entry<String, Set<Runnable>> entry : copy.entrySet()) {
|
||||
for (Entry<String, Set<Runnable>> entry : copy.entrySet()) {
|
||||
Set<Runnable> set = entry.getValue();
|
||||
for (Runnable callback : set) {
|
||||
if (callback != null) {
|
||||
|
||||
@@ -72,6 +72,14 @@ public class SimpleJobExplorerTests extends TestCase {
|
||||
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingGetJobExecution() throws Exception {
|
||||
expect(jobExecutionDao.getJobExecution(123L)).andReturn(null);
|
||||
replay(jobExecutionDao);
|
||||
assertNull(jobExplorer.getJobExecution(123L));
|
||||
verify(jobExecutionDao);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetStepExecution() throws Exception {
|
||||
expect(jobExecutionDao.getJobExecution(123L)).andReturn(jobExecution);
|
||||
|
||||
@@ -41,7 +41,7 @@ public class StepContextTests {
|
||||
|
||||
private List<String> list = new ArrayList<String>();
|
||||
|
||||
private StepExecution stepExecution = new StepExecution("step", new JobExecution(0L), 1L);
|
||||
private StepExecution stepExecution = new StepExecution("step", new JobExecution(new JobInstance(2L, null, "job"), 0L), 1L);
|
||||
|
||||
private StepContext context = new StepContext(stepExecution);
|
||||
|
||||
@@ -133,6 +133,16 @@ public class StepContextTests {
|
||||
assertTrue(list.contains("spam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStepName() throws Exception {
|
||||
assertEquals("step", context.getStepName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobName() throws Exception {
|
||||
assertEquals("job", context.getJobName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStepExecutionContext() throws Exception {
|
||||
ExecutionContext executionContext = stepExecution.getExecutionContext();
|
||||
@@ -140,6 +150,12 @@ public class StepContextTests {
|
||||
assertEquals("bar", context.getStepExecutionContext().get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSystemProperties() throws Exception {
|
||||
System.setProperty("foo", "bar");
|
||||
assertEquals("bar", context.getSystemProperties().getProperty("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobExecutionContext() throws Exception {
|
||||
ExecutionContext executionContext = stepExecution.getJobExecution().getExecutionContext();
|
||||
|
||||
Reference in New Issue
Block a user