Example usage for org.springframework.statemachine.ensemble StateMachineEnsembleException StateMachineEnsembleException

List of usage examples for org.springframework.statemachine.ensemble StateMachineEnsembleException StateMachineEnsembleException

Introduction

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

Prototype

public StateMachineEnsembleException(String message) 

Source Link

Document

Instantiates a new state machine ensemble exception.

Usage

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

private void handleZkDisconnect() {
    log.info("Handling Zookeeper disconnect");
    notifyError(new StateMachineEnsembleException("Lost connection to zookeeper"));
    notifyLeft();//from   w  w  w . j av a  2  s  .c  o m
}

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

/**
 * Handles internal logic of reading and comparing current
 * wrapper references and re-plays logs if needed.
 *
 * @return true if log is replayed//from w  w  w.j  a v  a 2s .  c  o  m
 * @throws Exception if error occurred
 */
private boolean handleDataChange() throws Exception {
    StateWrapper currentWrapper = stateRef.get();
    StateWrapper notifyWrapper = notifyRef.get();
    StateWrapper newWrapper = readCurrentContext();
    traceLogWrappers(currentWrapper, notifyWrapper, newWrapper);

    if (currentWrapper.version + 1 == newWrapper.version && notifyWrapper.version >= currentWrapper.version
            && stateRef.compareAndSet(currentWrapper, newWrapper)) {
        // simply used to check if we don't need to replay, if so
        // we can just try to notify
        mayNotifyStateChanged(newWrapper);
    } else {
        final int start = (notifyWrapper != null ? (notifyWrapper.version) : 0) % logSize;
        int count = newWrapper.version - (notifyWrapper != null ? (notifyWrapper.version) : 0);
        if (log.isDebugEnabled()) {
            log.debug("Events missed, trying to replay start " + start + " count " + count);
        }
        for (int i = start; i < (start + count); i++) {
            Stat stat = new Stat();
            StateMachineContext<S, E> context = ((ZookeeperStateMachinePersist<S, E>) persist).readLog(i, stat);
            int ver = (stat.getVersion() - 1) * logSize + (i + 1);

            // check if we're behind more than a log size meaning we can't
            // replay full history, notify and break out from a loop
            if (i + logSize < ver) {
                notifyError(new StateMachineEnsembleException("Current version behind more than log size"));
                break;
            }
            if (log.isDebugEnabled()) {
                log.debug("Replay position " + i + " with version " + ver);
                log.debug("Context in position " + i + " " + context);
            }

            StateWrapper wrapper = new StateWrapper(context, ver);

            // need to set stateRef when replaying if its
            // context is not set or otherwise just set
            // if stateRef still is currentWrapper
            StateWrapper currentWrapperx = stateRef.get();
            if (currentWrapperx.context == null) {
                stateRef.set(wrapper);
            } else if (wrapper.version == currentWrapperx.version + 1) {
                stateRef.set(wrapper);
            }
            mayNotifyStateChanged(wrapper);
        }
        // did we replay
        return count > 0;
    }
    return false;
}