Update docs

- Add section about error actions for states.
- Relates to #270
This commit is contained in:
Janne Valkealahti
2016-11-20 16:24:34 +00:00
parent f3016bd751
commit 6a5a704c75
2 changed files with 58 additions and 3 deletions

View File

@@ -221,9 +221,8 @@ wrapped within a `Runnable` which may get cancelled via
action, you need to be able to catch `InterruptedException` which is
raised if task is cancelled.
[[statemachine-config-actions-errorhandling]]
[[statemachine-config-transition-actions-errorhandling]]
==== Transition Action Error Handling
User can always catch exceptions manually but with actions defined for
transitions it is possible to define error action which is called if
exception is reased. Exception is then available from a `StateContext`
@@ -241,6 +240,19 @@ Similar logic can be done manually for every action if needed.
include::samples/DocsConfigurationSampleTests.java[tags=snippetED]
----
[[statemachine-config-state-actions-errorhandling]]
==== State Action Error Handling
Similar logic for error handling what is available for transition
actions is also available for actions defined for state behaviour and
its entry and exit.
For these `StateConfigurer` has methods `stateEntry`, `stateDo` and
`stateExit` to define `error` action together with an actual `action`.
[source,java,indent=0]
----
include::samples/DocsConfigurationSampleTests.java[tags=snippetEE]
----
=== Configuring Pseudo States
@@ -1608,7 +1620,7 @@ include::samples/DocsConfigurationSampleTests.java[tags=snippet4]
[TIP]
====
Actions defined for transitions also have their own error handling
logic <<statemachine-config-actions-errorhandling>>.
logic <<statemachine-config-transition-actions-errorhandling>>.
====
[[sm-persist]]

View File

@@ -353,6 +353,49 @@ public class DocsConfigurationSampleTests extends AbstractStateMachineTests {
}
}
// tag::snippetEE[]
@Configuration
@EnableStateMachine
public class Config55
extends EnumStateMachineConfigurerAdapter<States, Events> {
@Override
public void configure(StateMachineStateConfigurer<States, Events> states)
throws Exception {
states
.withStates()
.initial(States.S1)
.stateEntry(States.S2, action(), errorAction())
.stateDo(States.S2, action(), errorAction())
.stateExit(States.S2, action(), errorAction())
.state(States.S3);
}
@Bean
public Action<States, Events> action() {
return new Action<States, Events>() {
@Override
public void execute(StateContext<States, Events> context) {
throw new RuntimeException("MyError");
}
};
}
@Bean
public Action<States, Events> errorAction() {
return new Action<States, Events>() {
@Override
public void execute(StateContext<States, Events> context) {
// RuntimeException("MyError") added to context
Exception exception = context.getException();
exception.getMessage();
}
};
}
}
// end::snippetEE[]
// tag::snippetFA[]
@Configuration