Example usage for org.springframework.batch.core.job.flow.support StateTransition getNext

List of usage examples for org.springframework.batch.core.job.flow.support StateTransition getNext

Introduction

In this page you can find the example usage for org.springframework.batch.core.job.flow.support StateTransition getNext.

Prototype

public String getNext() 

Source Link

Document

Public getter for the next State name.

Usage

From source file:org.springframework.batch.core.job.flow.support.SimpleFlow.java

/**
 * @return the next {@link Step} (or null if this is the end)
 * @throws org.springframework.batch.core.job.flow.FlowExecutionException
 *//*ww  w.j a v a  2  s. c  o  m*/
protected State nextState(String stateName, FlowExecutionStatus status, StepExecution stepExecution)
        throws FlowExecutionException {
    Set<StateTransition> set = transitionMap.get(stateName);

    if (set == null) {
        throw new FlowExecutionException(
                String.format("No transitions found in flow=%s for state=%s", getName(), stateName));
    }

    String next = null;
    String exitCode = status.getName();

    for (StateTransition stateTransition : set) {
        if (stateTransition.matches(exitCode)
                || (exitCode.equals("PENDING") && stateTransition.matches("STOPPED"))) {
            if (stateTransition.isEnd()) {
                // End of job
                return null;
            }
            next = stateTransition.getNext();
            break;
        }
    }

    if (next == null) {
        throw new FlowExecutionException(
                String.format("Next state not found in flow=%s for state=%s with exit status=%s", getName(),
                        stateName, status.getName()));
    }

    if (!stateMap.containsKey(next)) {
        throw new FlowExecutionException(
                String.format("Next state not specified in flow=%s for next=%s", getName(), next));
    }

    return stateMap.get(next);

}

From source file:org.springframework.batch.core.job.flow.support.SimpleFlow.java

/**
 * Analyse the transitions provided and generate all the information needed
 * to execute the flow.//w  w  w.  j a v  a2  s .co  m
 */
private void initializeTransitions() {
    startState = null;
    transitionMap.clear();
    stateMap.clear();
    boolean hasEndStep = false;

    if (stateTransitions.isEmpty()) {
        throw new IllegalArgumentException(
                "No start state was found. You must specify at least one step in a job.");
    }

    for (StateTransition stateTransition : stateTransitions) {
        State state = stateTransition.getState();
        String stateName = state.getName();
        stateMap.put(stateName, state);
    }

    for (StateTransition stateTransition : stateTransitions) {

        State state = stateTransition.getState();

        if (!stateTransition.isEnd()) {

            String next = stateTransition.getNext();

            if (!stateMap.containsKey(next)) {
                throw new IllegalArgumentException("Missing state for [" + stateTransition + "]");
            }

        } else {
            hasEndStep = true;
        }

        String name = state.getName();

        Set<StateTransition> set = transitionMap.get(name);
        if (set == null) {
            // If no comparator is provided, we will maintain the order of insertion
            if (stateTransitionComparator == null) {
                set = new LinkedHashSet<StateTransition>();
            } else {
                set = new TreeSet<StateTransition>(stateTransitionComparator);
            }

            transitionMap.put(name, set);
        }
        set.add(stateTransition);

    }

    if (!hasEndStep) {
        throw new IllegalArgumentException(
                "No end state was found.  You must specify at least one transition with no next state.");
    }

    startState = stateTransitions.get(0).getState();

}