Catch Throwable instead Exception

- Fixes #164
This commit is contained in:
Janne Valkealahti
2016-02-20 09:48:18 +00:00
parent 160ceef61d
commit c3b3fe65b7
3 changed files with 108 additions and 23 deletions

View File

@@ -44,7 +44,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateChanged(from, to);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateChanged", e);
}
}
@@ -56,7 +56,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateEntered(state);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateEntered", e);
}
}
@@ -68,7 +68,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateExited(state);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateExited", e);
}
}
@@ -80,7 +80,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.eventNotAccepted(event);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during eventNotAccepted", e);
}
}
@@ -92,7 +92,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.transition(transition);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during transition", e);
}
}
@@ -104,7 +104,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.transitionStarted(transition);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during transitionStarted", e);
}
}
@@ -116,7 +116,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.transitionEnded(transition);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during transitionEnded", e);
}
}
@@ -128,7 +128,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateMachineStarted(stateMachine);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateMachineStarted", e);
}
}
@@ -140,7 +140,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateMachineStopped(stateMachine);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateMachineStopped", e);
}
}
@@ -152,7 +152,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateMachineError(stateMachine, exception);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateMachineError", e);
}
}
@@ -164,7 +164,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.extendedStateChanged(key, value);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during extendedStateChanged", e);
}
}
@@ -176,7 +176,7 @@ public class CompositeStateMachineListener<S, E> extends AbstractCompositeListen
StateMachineListener<S, E> listener = iterator.next();
try {
listener.stateContext(stateContext);
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during stateContext", e);
}
}

View File

@@ -139,7 +139,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishStateChanged(this, stateContext.getSource(), stateContext.getTarget());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyStateChanged", e);
}
}
@@ -155,7 +155,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishStateEntered(this, stateContext.getTarget());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyStateEntered", e);
}
}
@@ -171,7 +171,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishStateExited(this, stateContext.getSource());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyStateExited", e);
}
}
@@ -187,7 +187,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishEventNotAccepted(this, stateContext.getMessage());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyEventNotAccepted", e);
}
}
@@ -203,7 +203,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishTransitionStart(this, stateContext.getTransition());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyTransitionStart", e);
}
}
@@ -219,7 +219,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishTransition(this, stateContext.getTransition());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyTransition", e);
}
}
@@ -235,7 +235,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishTransitionEnd(this, stateContext.getTransition());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyTransitionEnd", e);
}
}
@@ -251,7 +251,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishStateMachineStart(this, stateContext.getStateMachine());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyStateMachineStarted", e);
}
}
@@ -267,7 +267,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishStateMachineStop(this, stateContext.getStateMachine());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyStateMachineStopped", e);
}
}
@@ -283,7 +283,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishStateMachineError(this, stateContext.getStateMachine(), stateContext.getException());
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyStateMachineError", e);
}
}
@@ -299,7 +299,7 @@ public abstract class StateMachineObjectSupport<S, E> extends LifecycleObjectSup
eventPublisher.publishExtendedStateChanged(this, key, value);
}
}
} catch (Exception e) {
} catch (Throwable e) {
log.warn("Error during notifyExtendedStateChanged", e);
}
}

View File

@@ -164,6 +164,28 @@ public class StateMachineErrorTests extends AbstractStateMachineTests {
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S2));
}
@Test
public void testListenerErrorsCauseNoMalfunction2() throws Exception {
context.register(EventListenerConfig2.class, Config1.class);
context.refresh();
@SuppressWarnings("unchecked")
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
StartedStateMachineListener listener1 = new StartedStateMachineListener();
ErroringStateMachineListener2 listener2 = new ErroringStateMachineListener2();
StateChangedStateMachineListener listener3 = new StateChangedStateMachineListener();
machine.addStateListener(listener1);
machine.addStateListener(listener2);
machine.start();
assertThat(listener1.latch.await(2, TimeUnit.SECONDS), is(true));
machine.addStateListener(listener3);
machine.sendEvent(TestEvents.E1);
assertThat(listener3.latch.await(2, TimeUnit.SECONDS), is(true));
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S2));
}
@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@@ -391,4 +413,67 @@ public class StateMachineErrorTests extends AbstractStateMachineTests {
}
}
static class ErroringStateMachineListener2 implements StateMachineListener<TestStates, TestEvents> {
@Override
public void stateChanged(State<TestStates, TestEvents> from, State<TestStates, TestEvents> to) {
throw new Error();
}
@Override
public void stateEntered(State<TestStates, TestEvents> state) {
throw new Error();
}
@Override
public void stateExited(State<TestStates, TestEvents> state) {
throw new Error();
}
@Override
public void eventNotAccepted(Message<TestEvents> event) {
throw new Error();
}
@Override
public void transition(Transition<TestStates, TestEvents> transition) {
throw new Error();
}
@Override
public void transitionStarted(Transition<TestStates, TestEvents> transition) {
throw new Error();
}
@Override
public void transitionEnded(Transition<TestStates, TestEvents> transition) {
throw new Error();
}
@Override
public void stateMachineStarted(StateMachine<TestStates, TestEvents> stateMachine) {
throw new Error();
}
@Override
public void stateMachineStopped(StateMachine<TestStates, TestEvents> stateMachine) {
throw new Error();
}
@Override
public void stateMachineError(StateMachine<TestStates, TestEvents> stateMachine, Exception exception) {
throw new Error();
}
@Override
public void extendedStateChanged(Object key, Object value) {
throw new Error();
}
@Override
public void stateContext(StateContext<TestStates, TestEvents> stateContext) {
throw new Error();
}
}
}