我正在使用狀態機構建器在我的應用程序中構建狀態機。 此外,該應用程序還具有實現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
A
回答
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被發現。
相關問題
- 1. AngularJS AJAX狀態代碼錯誤處理
- 2. 代碼錯誤(狀態機)
- 3. Spring狀態機池錯誤
- 4. Spring:在@RequestParam中輸入類型不匹配時處理錯誤
- 5. 一個通用的機制來處理HTTP狀態代碼
- 6. 錯誤批處理HTTP狀態代碼測試中的R
- 7. Spring MVC - 動態錯誤處理頁面
- 8. 處理PHPUnit_Extensions_Selenium2TestCase中的HTTP狀態代碼
- 9. 如何配置Spring 3 MVC來處理類似於異常的錯誤?
- 10. 在OpenRasta中設置來自IOperationInterceptor的HTTP狀態代碼
- 11. 如何處理動態代碼塊中的Objective-C錯誤?
- 12. 快速錯誤處理 - 獲取狀態代碼
- 13. Scrapy錯誤 - 未處理或不允許HTTP狀態代碼
- 14. koa錯誤處理和狀態代碼與koa
- 15. 無法在AJAX中獲取正確的狀態代碼錯誤處理程序
- 16. phpunit中錯誤的狀態代碼
- 17. WebClient的 - 會在錯誤狀態代碼
- 18. 修復或更換處理程序代碼來自動隨機
- 19. 處理來自多個商家的錯誤代碼
- 20. HTMLUNIT中的錯誤代理自動配置
- 21. 來自用戶代理的Javascript狀態
- 22. 使用Spring Boot中的動態端口配置Geb配置
- 23. 來自HTTP狀態代碼的NSError
- 24. 在r代碼中處理ftp錯誤
- 25. 如何在JSP錯誤處理程序中設置HTTP狀態碼
- 26. 處理配置錯誤 - global.asax?
- 27. IIS7和HTTP狀態代碼處理
- 28. 如何預先處理來自spring配置文件的值?
- 29. VSTS RIG安裝配置 - 處於「斷開」狀態的代理計算機
- 30. 如何在Solaris中配置Java的代理設置以處理代理自動配置(PAC)腳本?
任何幫助..非常感謝 – user2330825