2016-07-01 34 views
0

我正在使用狀態機構建器在我的應用程序中構建狀態機。 此外,該應用程序還具有實現org.springframework.statemachine.action.Action的Action類。 這些Action類用於執行每個階段的輸入操作。 如果從這些Action類(即從execute(StateContext paramStateContext))方法拋出任何異常,我想在捕獲異常併發送事件(Terminated)並將狀態機驅動到End狀態後,更新帶有錯誤詳細信息的db。 我試圖通過重寫stateMachineError(StateMachine stateMachine,Exception e)方法來使用狀態機監聽器。但不幸的是,這是行不通的。 任何其他Spring狀態機組件用於捕獲異常,然後再使用try catch將Action中的所有代碼包裝起來,並在catch塊中發送Terminated事件,以便狀態機將導航End狀態。 這裏是Iam使用的建造者。使用1.1.0.RELEASE版彈簧的statemachine核心在Spring狀態機中處理來自入口動作類的代碼/配置錯誤

+0

任何幫助..非常感謝 – user2330825

回答

0

你是正確的,既不stateMachineError@onStateMachineError註釋的方法

Builder<String, String> builder = StateMachineBuilder 
       .<String, String> builder(); 
     builder.configureConfiguration() 
     .withConfiguration() 
     .autoStartup(false) 
     .listener(listener()) 
       .beanFactory(
       this.applicationContext.getAutowireCapableBeanFactory()); 

private StateMachineListener<String, String> listener() { 
     return new StateMachineListenerAdapter<String, String>() { 
      @Override 
      public void stateChanged(
        org.springframework.statemachine.state.State<String, String> from, 
        org.springframework.statemachine.state.State<String, String> to) { 
       LOGGER.debug("State change to " + to.getId()); 
      } 

      @Override 
      public void stateMachineError(
        StateMachine<String, String> stateMachine, Exception e) { 
       e.printStackTrace(); 
       LOGGER.debug("Ah... I am not getting executed when exception occurs from entry actions"); 
       LOGGER.debug("Error occured from " + stateMachine.getState() 
         + "and the error is" + e.toString()); 
      } 
     }; 
    } 

蔭在出錯時執行。這在目前處於里程碑1的版本1.2中得到解決。他們介紹了與狀態機上下文中執行errorAction

用戶可以隨時手動捕獲異常,但用行動爲過渡定義,可以定義錯誤行動,如果引發異常時調用。然後可以從傳遞給該操作的StateContext獲得異常。

所有需要的是在定義狀態機配置類中的轉換時指定錯誤操作以及所需的操作。從example in the documentation

public void configure(StateMachineTransitionConfigurer<States, Events> transitions) 
     throws Exception { 
    transitions 
     .withExternal() 
      .source(States.S1) 
      .target(States.S2) 
      .event(Events.E1) 
      .action(action(), errorAction()); 
} 

這方面的一個詳細的討論可以在Spring中的statemachine issue #240被發現。

相關問題