如果我当前的类API失败,如何从另一个类调用方法



我需要以下方面的帮助。如果我的API1的方法1或方法2失败,并且我想从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基本上为应用程序防御机制提供了各种功能。试试看。

相关内容

  • 没有找到相关文章

最新更新