接口调用不成功,最多还能调用两次

接口调用不成功,最多还能调用两次

这种需求,我们在实际项目开发中,这种情况是比较多见的。

下面是伪代码:

package com.zyq;

public class Test01 {
    public static void main(String[] args) {
        //
        int count = 0;
        while (count < 2) {
            boolean flag=false;//调用接口的返回结果
            if(flag){
                System.out.println("调用成功");
                break;
            }else{
                System.out.println("没有调用成功");
                count++;
            }
        }
        System.out.println("========");
    }
}

  

 大家在写代码的时候别按照我上面的写哈,真实项目中代码是这样写的,我们调用接口的时候,第一次调用不成功,会让当前线程休眠一会儿,再去进行调用。

伪代码:

public class Test {
    public static void main(String[] args)  {

        int count = 0;
        while (count < 3) {
            count=count+1;
            boolean flag = sendMsg();//调用接口的返回结果
            if (flag) {
                System.out.println("调用成功");
                break;
            } else {
                try {
                    System.out.println("没有调用成功");
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("========");
    }

    public static boolean sendMsg(){
        //业务逻辑处理代码省略
        return false;//表示没有调用成功
    }
}

  

posted on 2021-09-26 11:17  ~码铃薯~  阅读(364)  评论(0编辑  收藏  举报

导航