Apply timeout control around Java operation

前端 未结 7 893
滥情空心
滥情空心 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 (<it's been ages now, I don't want to wait any longer>) {
            if ( tried == NUM_TRIES)
            {
               throw new TimeoutException();
            }
    
        }
                    tried++;
                Thread.sleep(4000);
      }
    
    0 讨论(0)
提交回复
热议问题