Example usage for org.springframework.statemachine StateMachineException StateMachineException

List of usage examples for org.springframework.statemachine StateMachineException StateMachineException

Introduction

In this page you can find the example usage for org.springframework.statemachine StateMachineException StateMachineException.

Prototype

public StateMachineException(String message, Throwable cause) 

Source Link

Document

Constructs a generic StateMachineException.

Usage

From source file:demo.web.StateMachineController.java

@PostConstruct
public void setup() {

    stateMachine.addStateListener(new StateMachineListenerAdapter<States, Events>() {
        @Override/*from w ww  . jav  a  2 s .c o  m*/
        public void stateEntered(State<States, Events> state) {
            StateMachineMessage message = new StateMachineMessage();
            message.setMessage("Enter state " + state.getId().toString());
            simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
        }

        @Override
        public void stateExited(State<States, Events> state) {
            StateMachineMessage message = new StateMachineMessage();
            message.setMessage("Exit state " + state.getId().toString());
            simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
        }

        @Override
        public void stateChanged(State<States, Events> from, State<States, Events> to) {
            Map<Object, Object> variables = stateMachine.getExtendedState().getVariables();
            ArrayList<StateMachineEvent> list = new ArrayList<StateMachineEvent>();
            for (States state : stateMachine.getState().getIds()) {
                list.add(new StateMachineEvent(state.toString()));
            }
            simpMessagingTemplate.convertAndSend("/topic/sm.states", list);
            simpMessagingTemplate.convertAndSend("/topic/sm.variables", variables);
        }

        @Override
        public void transitionEnded(Transition<States, Events> transition) {
            if (transition != null && transition.getKind() == TransitionKind.INTERNAL) {
                Map<Object, Object> variables = stateMachine.getExtendedState().getVariables();
                simpMessagingTemplate.convertAndSend("/topic/sm.variables", variables);
            }
        }

        @Override
        public void stateMachineError(StateMachine<States, Events> stateMachine, Exception exception) {
            handleStateMachineError(new StateMachineException("Received error from machine", exception));
        }

    });

    stateMachineEnsemble.addEnsembleListener(new EnsembleListenerAdapter<States, Events>() {

        @Override
        public void ensembleLeaderGranted(StateMachine<States, Events> stateMachine) {
            StateMachineMessage message = new StateMachineMessage();
            message.setMessage("Leader granted " + stateMachine.getUuid().toString());
            simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
        }

        @Override
        public void ensembleLeaderRevoked(StateMachine<States, Events> stateMachine) {
            StateMachineMessage message = new StateMachineMessage();
            message.setMessage("Leader revoked " + stateMachine.getUuid().toString());
            simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
        }
    });
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * Instantiates a new tasks handler. Intentionally private instantiation
 * meant to be called from a builder.//from w  ww.  ja va  2s  .c o  m
 *
 * @param tasks the wrapped tasks
 * @param listener the tasks listener
 * @param taskExecutor the task executor
 * @param persist the state machine persist
 */
private TasksHandler(List<TaskWrapper> tasks, TasksListener listener, TaskExecutor taskExecutor,
        StateMachinePersist<String, String, Void> persist) {
    this.persist = persist;
    try {
        stateMachine = buildStateMachine(tasks, taskExecutor);
        if (persist != null) {
            final LocalStateMachineInterceptor interceptor = new LocalStateMachineInterceptor(persist);
            stateMachine.getStateMachineAccessor()
                    .doWithAllRegions(new StateMachineFunction<StateMachineAccess<String, String>>() {

                        @Override
                        public void apply(StateMachineAccess<String, String> function) {
                            function.addStateMachineInterceptor(interceptor);
                        }
                    });
        }
    } catch (Exception e) {
        throw new StateMachineException("Error building state machine from tasks", e);
    }
    if (listener != null) {
        addTasksListener(listener);
    }
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * Resets state machine states from a backing persistent repository. If
 * {@link StateMachinePersist} is not set this method doesn't do anything.
 * {@link StateMachine} is stopped before states are reseted from a persistent
 * store and started afterwards./* w  w w.j  a va  2  s  .c o m*/
 */
public void resetFromPersistStore() {
    if (persist == null) {
        // TODO: should we throw or silently return?
        return;
    }

    final StateMachineContext<String, String> context;
    try {
        context = persist.read(null);
    } catch (Exception e) {
        throw new StateMachineException("Error reading state from persistent store", e);
    }

    stateMachine.stop();
    stateMachine.getStateMachineAccessor()
            .doWithAllRegions(new StateMachineFunction<StateMachineAccess<String, String>>() {

                @Override
                public void apply(StateMachineAccess<String, String> function) {
                    function.resetStateMachine(context);
                }
            });
    stateMachine.start();
}

From source file:org.springframework.statemachine.zookeeper.ZookeeperStateMachineEnsemble.java

@Override
public synchronized void setState(StateMachineContext<S, E> context) {
    if (log.isDebugEnabled()) {
        log.debug("Setting state context=" + context);
    }/*from w  ww  . jav a  2s . com*/
    try {
        Stat stat = new Stat();
        StateWrapper stateWrapper = stateRef.get();
        if (stateWrapper != null) {
            stat.setVersion(stateWrapper.version);
        }
        if (log.isDebugEnabled()) {
            log.debug("Requesting persist write " + context + " with version " + stat.getVersion()
                    + " for ensemble " + uuid);
        }
        persist.write(context, stat);
        if (log.isDebugEnabled()) {
            log.debug("Request persist write ok " + context + " new version " + stat.getVersion()
                    + " for ensemble " + uuid);
        }
        stateRef.set(new StateWrapper(context, stat.getVersion()));
    } catch (Exception e) {
        throw new StateMachineException("Error persisting data", e);
    }
}

From source file:org.springframework.statemachine.zookeeper.ZookeeperStateMachineEnsemble.java

private StateWrapper readCurrentContext() {
    try {/*from ww w .j  a v  a2 s  . co  m*/
        Stat stat = new Stat();
        registerWatcherForStatePath();
        StateMachineContext<S, E> context = persist.read(stat);
        return new StateWrapper(context, stat.getVersion());
    } catch (Exception e) {
        throw new StateMachineException("Error reading data", e);
    }
}