Remove input/output parameters from step handler (use job execution context instead).
This commit is contained in:
@@ -17,7 +17,6 @@ package org.springframework.batch.integration.job;
|
||||
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.core.AttributeAccessorSupport;
|
||||
|
||||
/**
|
||||
* Encapsulation of a request to execute a job execution through a message flow
|
||||
@@ -35,7 +34,7 @@ import org.springframework.core.AttributeAccessorSupport;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobExecutionRequest extends AttributeAccessorSupport {
|
||||
public class JobExecutionRequest {
|
||||
|
||||
private JobExecution jobExecution;
|
||||
|
||||
|
||||
@@ -38,10 +38,6 @@ public class StepExecutionMessageHandler {
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
private String[] inputKeys = new String[0];
|
||||
|
||||
private String[] outputKeys = new String[0];
|
||||
|
||||
/**
|
||||
* Public setter for the {@link Step}.
|
||||
* @param step the step to set
|
||||
@@ -51,25 +47,6 @@ public class StepExecutionMessageHandler {
|
||||
this.step = step;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the input keys. Attributes from the incoming request
|
||||
* will be added to the execution context for the step.
|
||||
* @param inputKeys the inputKeys to set
|
||||
*/
|
||||
public void setInputKeys(String[] inputKeys) {
|
||||
this.inputKeys = inputKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the output keys. Attributes from a successful step
|
||||
* execution context will be added to the request before it is handed on to
|
||||
* the next handler.
|
||||
* @param outputKeys the outputKeys to set
|
||||
*/
|
||||
public void setOutputKeys(String[] outputKeys) {
|
||||
this.outputKeys = outputKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobRepository} that is needed to manage the
|
||||
* state of the batch meta domain (jobs, steps, executions) during the life
|
||||
@@ -114,7 +91,7 @@ public class StepExecutionMessageHandler {
|
||||
.getExitStatus().equals(ExitStatus.FINISHED)) ? true : false;
|
||||
|
||||
if (!isRestart || lastStepExecution == null) {
|
||||
stepExecution.setExecutionContext(getExecutionContextWithInputs(request));
|
||||
stepExecution.setExecutionContext(new ExecutionContext());
|
||||
}
|
||||
|
||||
step.execute(stepExecution);
|
||||
@@ -133,49 +110,10 @@ public class StepExecutionMessageHandler {
|
||||
throw e;
|
||||
}
|
||||
|
||||
// TODO: could a failure here could cause job to be in inconsistent
|
||||
// state?
|
||||
getMessageWithOutputs(request, stepExecution.getExecutionContext());
|
||||
return request;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the message header of the input attributes and add in output
|
||||
* attributes from the execution context.
|
||||
* @param request
|
||||
* @param executionContext
|
||||
* @return
|
||||
*/
|
||||
private JobExecutionRequest getMessageWithOutputs(JobExecutionRequest request, ExecutionContext executionContext) {
|
||||
for (int i = 0; i < inputKeys.length; i++) {
|
||||
String key = inputKeys[i];
|
||||
request.removeAttribute(key);
|
||||
}
|
||||
for (int i = 0; i < outputKeys.length; i++) {
|
||||
String key = outputKeys[i];
|
||||
request.setAttribute(key, executionContext.get(key));
|
||||
}
|
||||
// TODO: is this safe, or should we build a new message?
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new execution context with all the attributes requested from
|
||||
* the message (if they exist).
|
||||
*
|
||||
* @return an {@link ExecutionContext}
|
||||
*/
|
||||
private ExecutionContext getExecutionContextWithInputs(JobExecutionRequest request) {
|
||||
ExecutionContext executionContext = new ExecutionContext();
|
||||
for (int i = 0; i < inputKeys.length; i++) {
|
||||
String key = inputKeys[i];
|
||||
Object value = request.getAttribute(key);
|
||||
executionContext.put(key, value);
|
||||
}
|
||||
return executionContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param request
|
||||
* @return
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package org.springframework.batch.integration.job;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -36,8 +35,6 @@ import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.integration.JobRepositorySupport;
|
||||
import org.springframework.batch.integration.JobSupport;
|
||||
import org.springframework.batch.integration.StepSupport;
|
||||
import org.springframework.batch.integration.job.JobExecutionRequest;
|
||||
import org.springframework.batch.integration.job.StepExecutionMessageHandler;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
import org.springframework.beans.factory.annotation.Required;
|
||||
@@ -94,14 +91,13 @@ public class StepExecutionMessageHandlerTests {
|
||||
public void testHandleWithInputs() throws Exception {
|
||||
JobRepositorySupport jobRepository = new JobRepositorySupport();
|
||||
StepExecutionMessageHandler handler = createHandler(jobRepository);
|
||||
handler.setInputKeys(new String[] { "foo" });
|
||||
JobExecutionRequest jobExecutionRequest = new JobExecutionRequest(jobRepository.createJobExecution(
|
||||
new JobSupport("job"), new JobParameters()));
|
||||
jobExecutionRequest.setAttribute("foo", "bar");
|
||||
jobExecutionRequest.getJobExecution().getExecutionContext().putString("foo", "bar");
|
||||
JobExecutionRequest message = handler.handle(jobExecutionRequest);
|
||||
assertEquals(1, message.getJobExecution().getStepExecutions().size());
|
||||
StepExecution stepExecution = (StepExecution) message.getJobExecution().getStepExecutions().iterator().next();
|
||||
assertTrue(stepExecution.getExecutionContext().containsKey("foo"));
|
||||
JobExecution jobExecution = message.getJobExecution();
|
||||
assertTrue(jobExecution .getExecutionContext().containsKey("foo"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -109,21 +105,20 @@ public class StepExecutionMessageHandlerTests {
|
||||
public void testHandleWithInputsAndOutputs() throws Exception {
|
||||
JobRepositorySupport jobRepository = new JobRepositorySupport();
|
||||
StepExecutionMessageHandler handler = createHandler(jobRepository);
|
||||
handler.setInputKeys(new String[] { "foo" });
|
||||
handler.setOutputKeys(new String[] { "bar" });
|
||||
JobExecutionRequest jobExecutionRequest = new JobExecutionRequest(jobRepository.createJobExecution(
|
||||
new JobSupport("job"), new JobParameters()));
|
||||
jobExecutionRequest.setAttribute("foo", "bar");
|
||||
jobExecutionRequest.getJobExecution().getExecutionContext().putString("foo", "bar");
|
||||
// The step has to add the output attribute to the context
|
||||
handler.setStep(new StepSupport("step") {
|
||||
@Override
|
||||
public void execute(StepExecution stepExecution) throws JobInterruptedException {
|
||||
stepExecution.getExecutionContext().putString("bar", "spam");
|
||||
stepExecution.getJobExecution().getExecutionContext().putString("bar", "spam");
|
||||
}
|
||||
});
|
||||
handler.handle(jobExecutionRequest);
|
||||
assertFalse(jobExecutionRequest.hasAttribute("foo"));
|
||||
assertTrue(jobExecutionRequest.hasAttribute("bar"));
|
||||
JobExecutionRequest message = handler.handle(jobExecutionRequest);
|
||||
JobExecution jobExecution = message.getJobExecution();
|
||||
assertTrue(jobExecution .getExecutionContext().containsKey("foo"));
|
||||
assertTrue(jobExecution .getExecutionContext().containsKey("bar"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Reference in New Issue
Block a user