private State createState(Object input) { State result; if (input instanceof Step) { if (!states.containsKey(input)) { Step step = (Step) input; states.put(input, new StepState(prefix + step.getName(), step)); } result = states.get(input); } else if (input instanceof JobExecutionDecider) { if (!states.containsKey(input)) { states.put(input, new DecisionState((JobExecutionDecider) input, prefix + "decision" + (decisionCounter++))); } result = states.get(input); } else if (input instanceof Flow) { if (!states.containsKey(input)) { states.put(input, new FlowState((Flow) input, prefix + ((Flow) input).getName())); } result = states.get(input); } else { throw new FlowBuilderException("No state can be created for: " + input); } dirty = true; return result; }
/** * Provides an extension point to provide alternative {@link StepState} * implementations within a {@link SimpleFlow} * * @param state The state that will be used to create the StepState * @param oldName The name to be replaced * @param stateName The name for the new State * @return a state for the requested data */ protected State createNewStepState(State state, String oldName, String stateName) { return new StepState(stateName, ((StepState) state).getStep(oldName)); }
@Override public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { FlowExecutionStatus result = super.handle(executor); executor.getJobExecution().getExecutionContext().put("batch.lastSteps", Collections.singletonList(getStep().getName())); return result; } }
@Bean public SimpleFlow simpleFlow() { SimpleFlow simpleFlow = new SimpleFlow("simpleFlow"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(dummyStep()), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); simpleFlow.setStateTransitions(transitions); return simpleFlow; }
/** * Provides an extension point to provide alternative {@link StepState} * implementations within a {@link SimpleFlow} * * @param state The state that will be used to create the StepState * @param oldName The name to be replaced * @param stateName The name for the new State * @return a state for the requested data */ protected State createNewStepState(State state, String oldName, String stateName) { return new StepState(stateName, ((StepState) state).getStep(oldName)); }
@Override public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { FlowExecutionStatus result = super.handle(executor); executor.getJobExecution().getExecutionContext().put("batch.lastSteps", Collections.singletonList(getStep().getName())); return result; } }
@Test public void testGetSteps() throws Exception { SimpleFlow flow = new JsrFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); assertEquals(2, job.getStepNames().size()); }
/** * Provides an extension point to provide alternative {@link StepState} * implementations within a {@link SimpleFlow} * * @param state The state that will be used to create the StepState * @param oldName The name to be replaced * @param stateName The name for the new State * @return */ protected State createNewStepState(State state, String oldName, String stateName) { return new StepState(stateName, ((StepState) state).getStep(oldName)); }
@Override public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { FlowExecutionStatus result = super.handle(executor); executor.getJobExecution().getExecutionContext().put("batch.lastSteps", Collections.singletonList(getStep().getName())); return result; } }
@Test public void testGetSteps() throws Exception { SimpleFlow flow = new SimpleFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); assertEquals(2, job.getStepNames().size()); }
/** * Provides an extension point to provide alternative {@link StepState} * implementations within a {@link SimpleFlow} * * @param state The state that will be used to create the StepState * @param oldName The name to be replaced * @param stateName The name for the new State * @return a state for the requested data */ protected State createNewStepState(State state, String oldName, String stateName) { return new StepState(stateName, ((StepState) state).getStep(oldName)); }
@Override public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { FlowExecutionStatus result = super.handle(executor); executor.getJobExecution().getExecutionContext().put("batch.lastSteps", Collections.singletonList(getStep().getName())); return result; } }
@Test public void testGetStepNotStepState() throws Exception { SimpleFlow flow = new SimpleFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); Step step = job.getStep("end0"); assertNull(step); }
/** * Convenience method to get a state that proxies the input but with a * different name, appropriate to this flow. If the state is a StepState * then the step name is also changed. * * @param state * @return */ private State getProxyState(State state) { String oldName = state.getName(); if (oldName.startsWith(prefix)) { return state; } String stateName = prefix + oldName; if (state instanceof StepState) { return new StepState(stateName, ((StepState) state).getStep()); } return new DelegateState(stateName, state); }
@Test public void testGetStepNotExists() throws Exception { SimpleFlow flow = new SimpleFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); Step step = job.getStep("foo"); assertNull(step); }
@Test public void testGetStepNotExists() throws Exception { SimpleFlow flow = new JsrFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); Step step = job.getStep("foo"); assertNull(step); }
@Test public void testGetStepNotStepState() throws Exception { SimpleFlow flow = new JsrFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); Step step = job.getStep("end0"); assertNull(step); }
@Test public void testGetStepNestedFlow() throws Exception { SimpleFlow nested = new JsrFlow("nested"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end1")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end1"))); nested.setStateTransitions(transitions); nested.afterPropertiesSet(); SimpleFlow flow = new JsrFlow("job"); transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "nested")); transitions.add(StateTransition.createStateTransition(new FlowState(nested, "nested"), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); flow.afterPropertiesSet(); job.setFlow(flow); job.afterPropertiesSet(); List<String> names = new ArrayList<>(job.getStepNames()); Collections.sort(names); assertEquals("[step1, step2]", names.toString()); }
@Test public void testBasicFlow() throws Throwable { SimpleFlow flow = new SimpleFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); job.setFlow(flow); job.execute(jobExecution); if (!jobExecution.getAllFailureExceptions().isEmpty()) { throw jobExecution.getAllFailureExceptions().get(0); } assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); }
@Test public void testBasicFlow() throws Throwable { SimpleFlow flow = new JsrFlow("job"); List<StateTransition> transitions = new ArrayList<>(); transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step")), "end0")); transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); flow.setStateTransitions(transitions); job.setFlow(flow); job.execute(jobExecution); if (!jobExecution.getAllFailureExceptions().isEmpty()) { throw jobExecution.getAllFailureExceptions().get(0); } assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); }