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.
推薦閱讀
- 物聯網(IoT)基礎:網絡技術+協議+用例
- 物聯網網絡安全及應用
- TCP/IP入門經典(第5版)
- 網絡安全技術與解決方案(修訂版)
- Getting Started with nopCommerce
- 5G技術核心與增強:從R15到R17
- 精通SEO:100%網站流量提升密碼
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 網絡空間作戰:機理與籌劃
- 巧學活用CISCO網絡典型配置
- 網絡分析技術揭秘:原理、實踐與WinPcap深入解析
- 深入淺出計算機網絡
- 云計算、網絡安全和網絡盜竊:網絡世界防盜初學指南
- 物聯網工程應用技術
- 5G智聯萬物:輕松讀懂5G應用與智能未來