OPEN - issue BATCH-890, BATCH-879: Stop transition in XML namespace, decision state support.
Added StepExecution to decider interface and some protection for multithreaded job executions.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.flow;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface FlowExecutionListener {
|
||||
|
||||
/**
|
||||
* @param result
|
||||
*/
|
||||
void close(FlowExecution result);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.flow;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class FlowExecutionListenerSupport implements FlowExecutionListener {
|
||||
|
||||
/**
|
||||
* No-op implementation.
|
||||
* @see FlowExecutionListener#close(FlowExecution)
|
||||
*/
|
||||
public void close(FlowExecution result) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -111,21 +111,37 @@ public class SimpleFlow<T> implements Flow<T>, InitializingBean {
|
||||
* @see Flow#resume(String, Object)
|
||||
*/
|
||||
public FlowExecution resume(String stateName, T context) throws FlowExecutionException {
|
||||
|
||||
String status = FlowExecution.UNKNOWN;
|
||||
State<T> state = stateMap.get(stateName);
|
||||
|
||||
FlowExecutionListener listener = new FlowExecutionListenerSupport();
|
||||
if (context instanceof FlowExecutionListener) {
|
||||
listener = (FlowExecutionListener)context;
|
||||
}
|
||||
|
||||
// Terminate if there are no more states
|
||||
while (state != null) {
|
||||
|
||||
stateName = state.getName();
|
||||
|
||||
try {
|
||||
status = state.handle(context);
|
||||
}
|
||||
catch (Exception e) {
|
||||
listener.close(new FlowExecution(stateName, status));
|
||||
throw new FlowExecutionException(String.format("Ended flow=%s at state=%s with exception", name,
|
||||
stateName), e);
|
||||
}
|
||||
|
||||
state = nextState(stateName, status);
|
||||
|
||||
}
|
||||
return new FlowExecution(stateName, status);
|
||||
|
||||
FlowExecution result = new FlowExecution(stateName, status);
|
||||
listener.close(result);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.flow.FlowExecution;
|
||||
@@ -35,34 +36,34 @@ import org.springframework.batch.flow.StateTransition;
|
||||
*/
|
||||
public class BasicFlowTests {
|
||||
|
||||
private SimpleFlow<String> flow = new SimpleFlow<String>("job");
|
||||
private SimpleFlow<Object> flow = new SimpleFlow<Object>("job");
|
||||
|
||||
private String executor = "data";
|
||||
private Object executor = "data";
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEmptySteps() throws Exception {
|
||||
flow.setStateTransitions(Collections.<StateTransition<String>> emptySet());
|
||||
flow.setStateTransitions(Collections.<StateTransition<Object>> emptySet());
|
||||
flow.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNoNextStepSpecified() throws Exception {
|
||||
flow.setStateTransitions(Collections.singleton(StateTransition.createStateTransition(new StateSupport<String>(
|
||||
flow.setStateTransitions(Collections.singleton(StateTransition.createStateTransition(new StateSupport<Object>(
|
||||
"step"), "foo")));
|
||||
flow.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNoStartStep() throws Exception {
|
||||
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StateSupport<String>("step"),
|
||||
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StateSupport<Object>("step"),
|
||||
FlowExecution.FAILED, "step"), StateTransition
|
||||
.createEndStateTransition(new StateSupport<String>("step"))));
|
||||
.createEndStateTransition(new StateSupport<Object>("step"))));
|
||||
flow.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNoEndStep() throws Exception {
|
||||
flow.setStateTransitions(Collections.singleton(StateTransition.createStateTransition(new StateSupport<String>(
|
||||
flow.setStateTransitions(Collections.singleton(StateTransition.createStateTransition(new StateSupport<Object>(
|
||||
"step"), FlowExecution.FAILED, "step")));
|
||||
flow.setStartStateName("step");
|
||||
flow.afterPropertiesSet();
|
||||
@@ -101,6 +102,24 @@ public class BasicFlowTests {
|
||||
assertEquals("step1", execution.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneStepWithListenerCallsClose() throws Exception {
|
||||
flow.setStateTransitions(Collections
|
||||
.singleton(StateTransition.createEndStateTransition(new StubState("step1"))));
|
||||
flow.afterPropertiesSet();
|
||||
final List<FlowExecution> list = new ArrayList<FlowExecution>();
|
||||
executor = new FlowExecutionListenerSupport() {
|
||||
@Override
|
||||
public void close(FlowExecution result) {
|
||||
list.add(result);
|
||||
}
|
||||
};
|
||||
FlowExecution execution = flow.start(executor);
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
|
||||
assertEquals("step1", execution.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitStartStep() throws Exception {
|
||||
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step"),
|
||||
@@ -136,7 +155,7 @@ public class BasicFlowTests {
|
||||
public void testFailedStep() throws Exception {
|
||||
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1") {
|
||||
@Override
|
||||
public String handle(String executor) {
|
||||
public String handle(Object executor) {
|
||||
return FlowExecution.FAILED;
|
||||
}
|
||||
}, "step2"), StateTransition.createEndStateTransition(new StubState("step2"))));
|
||||
@@ -165,7 +184,7 @@ public class BasicFlowTests {
|
||||
private boolean paused = false;
|
||||
|
||||
@Override
|
||||
public String handle(String executor) throws Exception {
|
||||
public String handle(Object executor) throws Exception {
|
||||
if (!paused) {
|
||||
paused = true;
|
||||
return FlowExecution.PAUSED;
|
||||
@@ -184,23 +203,23 @@ public class BasicFlowTests {
|
||||
assertEquals("step3", execution.getName());
|
||||
}
|
||||
|
||||
private Collection<StateTransition<String>> collect(StateTransition<String> s1, StateTransition<String> s2) {
|
||||
Collection<StateTransition<String>> list = new ArrayList<StateTransition<String>>();
|
||||
private Collection<StateTransition<Object>> collect(StateTransition<Object> s1, StateTransition<Object> s2) {
|
||||
Collection<StateTransition<Object>> list = new ArrayList<StateTransition<Object>>();
|
||||
list.add(s1);
|
||||
list.add(s2);
|
||||
return list;
|
||||
}
|
||||
|
||||
private Collection<StateTransition<String>> collect(StateTransition<String> s1, StateTransition<String> s2,
|
||||
StateTransition<String> s3) {
|
||||
Collection<StateTransition<String>> list = collect(s1, s2);
|
||||
private Collection<StateTransition<Object>> collect(StateTransition<Object> s1, StateTransition<Object> s2,
|
||||
StateTransition<Object> s3) {
|
||||
Collection<StateTransition<Object>> list = collect(s1, s2);
|
||||
list.add(s3);
|
||||
return list;
|
||||
}
|
||||
|
||||
private Collection<StateTransition<String>> collect(StateTransition<String> s1, StateTransition<String> s2,
|
||||
StateTransition<String> s3, StateTransition<String> s4) {
|
||||
Collection<StateTransition<String>> list = collect(s1, s2, s3);
|
||||
private Collection<StateTransition<Object>> collect(StateTransition<Object> s1, StateTransition<Object> s2,
|
||||
StateTransition<Object> s3, StateTransition<Object> s4) {
|
||||
Collection<StateTransition<Object>> list = collect(s1, s2, s3);
|
||||
list.add(s4);
|
||||
return list;
|
||||
}
|
||||
@@ -209,7 +228,7 @@ public class BasicFlowTests {
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
private static class StubState extends StateSupport<String> {
|
||||
private static class StubState extends StateSupport<Object> {
|
||||
|
||||
/**
|
||||
* @param string
|
||||
|
||||
Reference in New Issue
Block a user