24 Ekim 2021 Pazar

SpringBoot StateMachine EnumStateMachineConfigurerAdapter Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
İki tane configure metodu var. Biri states diğeri transitions için

configure metodu
Örnek
Şöyle yaparız
@Configuration
@EnableStateMachine
public class OrderStateMachineConfig extends 
  EnumStateMachineConfigurerAdapter<OrderState,OrderEvent> {

  @Override
  public void configure(StateMachineStateConfigurer<OrderState,OrderEvent> states)
  throws Exception {
    states.withStates()
      .initial(OrderState.ORDERED)
      .states(EnumSet.allOf(OrderState.class));
  }

  @Override
  public void configure(StateMachineTransitionConfigurer<OrderState,OrderEvent> transitions)
  throws Exception {
    transitions
      .withExternal()
        .source(OrderState.ORDERED).event(OrderEvent.ON_SHIPMENT)
.target(OrderState.SHIPPED) .and() .withExternal() .source(OrderState.ORDERED).event(OrderEvent.ON_CANCEL) .target(OrderState.CANCELLED) .and() ... } }
Örnek
Şöyle yaparız
@Configuration
@EnableStateMachineFactory
class PaymentStateMachineConfiguration :
  EnumStateMachineConfigurerAdapter<PaymentStates, PaymentEvents>() {

  override fun configure(states:
    StateMachineStateConfigurer<PaymentStates, PaymentEvents>) {
    states.withStates()
      .initial(AWAITING)
      .state(TRANSACTION_CREATED)
      .state(TRANSACTION_SENT)
      .state(TRANSACTION_FAILED)
      .end(SUCCEEDED)
  }

  override fun configure(transitions:
    StateMachineTransitionConfigurer<PaymentStates,PaymentEvents>) {
    transitions
      .withExternal()
      .source(AWAITING).target(TRANSACTION_CREATED).event(CREATE_TRANSACTION)
      .and()

      .withExternal()
      .source(TRANSACTION_CREATED).target(TRANSACTION_SENT).event(SEND_TRANSACTION)
      .and()
      ...
  }
}

Hiç yorum yok:

Yorum Gönder