The @Fallback policy
The @Fallback annotation can only be applied on a method; annotating a class will give an unexpected result:
@Retry(maxRetries = 2)
@Fallback(StringFallbackHandler.class)
public String shouldFallback() {
...
}
The fallback method is called after the number of retries is reached. In the previous example, the method will be retried twice in case of an error, and then the fallback will be used to invoke another piece of code—in this case, the following StringFallbackHandler class:
import javax.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.faulttolerance.ExecutionContext;
import org.eclipse.microprofile.faulttolerance.FallbackHandler;
@ApplicationScoped
public class StringFallbackHandler implements FallbackHandler<String> {
@ConfigProperty(name="app1.requestFallbackReply", defaultValue = "Unconfigured Default Reply")
private String replyString;
@Override
public String handle(ExecutionContext ec) {
return replyString;
}
}
Fallback code can be defined by a class implementing the FallbackHandler interface (see the previous code) or by a method in the current bean. In the StringFallbackHandler code, a MicroProfile Config property named app1.requestFallbackReply is used to externalize the application's fallback string value.
推薦閱讀
- 智慧城市:大數據、互聯網時代的城市治理(第4版)
- 物聯網與北斗應用
- 解析QUIC/HTTP3:未來互聯網的基石
- Modern JavaScript Web Development Cookbook
- 重新定義Spring Cloud實戰
- Web Application Development with R Using Shiny
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- 世界互聯網發展報告·2019
- 端到端QoS網絡設計
- Getting Started with nopCommerce
- 從實踐中學習手機抓包與數據分析
- 中國互聯網發展報告2021
- 現代通信系統(第5版)
- Building RESTful Web Services with .NET Core
- 物聯網,So Easy!