Apply timeout control around Java operation

前端 未结 7 890
滥情空心
滥情空心 2021-02-07 06:54

I\'m using a third party Java library to interact with a REST API. The REST API can sometimes take a long time to respond, eventually resulting in a java.net.ConnectExcept

7条回答
  •  花落未央
    2021-02-07 07:42

        static final int NUM_TRIES =4;
        int tried =0;
        boolean result =false;
        while (tried < NUM_TRIES && !result)
        {
        try {
            Entity entity = new Entity();
    
                result = entity.methodThatMakesUseOfRestApi();
    
    
        }  
         catch () {
            if ( tried == NUM_TRIES)
            {
               throw new TimeoutException();
            }
    
        }
                    tried++;
                Thread.sleep(4000);
      }
    

提交回复
热议问题