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

问题描述

我需要以下帮助。如果我的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基本上为应用程序防御机制提供了各种功能。试试吧。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...