0

In the Spring Statemachine reference doc is this sample code:

@WithStateMachine
static class Bean1 {

    @OnTransition(source = "S1", target = "S2")
    public void fromS1ToS2() {
    }
}

Is it possible to access the StateContext object from a method annotated with @OnTransition? Perhaps I don't understand the correct use of the annotation...I thought it could be used in a similar manner as an Action, where I could access data stored in the ExtendedState.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Paul
  • 19,704
  • 14
  • 78
  • 96

2 Answers2

1

It seems that I've totally forget to add this specific information on our docs. We can't access StateContext but event headers and ExtendedState are available.

There's a unit test for this in MethodAnnotationTests.

Short story is that processor handling method calling detects argument types ExtendedState and Map if it's annotated with @EventHeaders. I've also been thinking to support StateContext in a same way via method arguments but haven't yet got that far.

@WithStateMachine
public class Bean2 {

  @OnTransition(source = "S1", target = "S2")
  public void method1(@EventHeaders Map<String, Object> headers, ExtendedState extendedState) {
  }

}

I'll also get docs sorted out for this, thx for pointing this out!

Janne Valkealahti
  • 2,552
  • 1
  • 14
  • 12
  • Thanks for the quick answer! I'll look at the tests and source code first next time. I was only interested in `StateContext` as a way to get at `ExtendedState` so this is perfect. Are there plans for additional annotations, maybe something like `@OnStateEnter` and `@OnStateExit`? – Paul Nov 20 '15 at 14:18
  • Well yeah having other annotations is not a bad idea at all. Should probably create similar annotations matching functionality from `StateMachineListener` and app context events. I'll create a gh issues for these. – Janne Valkealahti Nov 20 '15 at 15:27
0

Their documentation says:

A method annotated with @OnTransition may accept a parameter of type ExtendedState, Map if map argument itself is annotated with EventHeaders, StateMachine, Message or Exception.

https://docs.spring.io/spring-statemachine/docs/current/api/org/springframework/statemachine/annotation/OnTransition.html

Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70