- Hands-On Enterprise Java Microservices with Eclipse MicroProfile
- Cesar Saavedra Heiko W. Rupp Jeff Mesnil Pavol Loffay Antoine Sabot Durand Scott Stark
- 159字
- 2021-06-24 12:56:23
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.
推薦閱讀
- FreeSWITCH 1.2
- Web Application Development with R Using Shiny
- 計算機網絡與數據通信
- 信息通信網絡建設安全管理概要2
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 數字通信同步技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- VMware NSX網絡虛擬化入門
- React Cookbook
- 異構蜂窩網絡關鍵理論與技術
- 大型企業微服務架構實踐與運營
- 智慧城市中的物聯網技術
- 趣話通信:6G的前世、今生和未來
- 物聯網導論
- 5G新型多址技術
- 網絡是怎樣連接的