问题描述
我需要以下帮助。如果我的API1的method1或method2失败,并且我想从API2类调用这两个方法以继续执行。我该如何处理。有人可以给我一些代码想法吗?
public class API1 (){
public void method1(){
/* generate token let's say 1234
if token generation or method1 fails call both the methods of API2 class methodA & methodB */
}
public void method2(){
/* if method1 generate successful token then pass the token 1234 from method1 and get the Acct nunber A777 from the Response
if method2 passes then store the account number A777 and pass it to methodB of API2 class
if this method2() fails for some reason call both the methods of API2 class methodA & methodB and continue execution with hard coded acctnumber from MethodB which is defined in the else statement
}
}
public class API2 {
public void methodA(){
//Getting token auth toke let's 9876
}
public void methodB(){
String OtherHardCodedAccountNumber = M999
/* passing the token 9876 from methodA and pass the account number from API1 Class method2 which is A777 */
if(method2 accountNunber is valid){
use A777
}
else {
// use the OtherHardCodedAccountNumber which is M999
}
}
解决方法
应用程序防御机制应该可以为您提供帮助。尝试Netflix hystrix。
步骤: 执行hystrix库:
* API1.method1().
* API1.method2()
如果任何方法失败,则引发异常。如果启用回退,则Hystrix会在执行失败时回退。
如果执行有异常,请执行回退:
* API2.method1().
* API2.method2()
Netflix hystrix基本上为应用程序防御机制提供了各种功能。试试吧。