1

I am calling an API to get the response, but if API is down, my request hangs there and control doesn't come back until the API is up. Once API is Up and Running all the requests that are hanged are flooded all at once to it and then we are getting the response.

My requirement is like this :
1.Call the API
2.Wait for 15 Seconds to get the response
3.If i don't get the response with in 15 seconds, do something(Example : throw an exception)
4.Close the connection that i had connected.

Please help with code snippet.
I have tried using CloseableHttpClient,HttpResponse,CloseableHttpResponse.

kashyappvb
  • 15
  • 1
  • 6
  • Assuming you are not using any specific framework, you should set the timeout on the request, not in the response. I think this thread has the solution you are looking for https://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout – gzp___ Jun 22 '18 at 07:44

2 Answers2

2

Try using OKHTTP. In it your code would look like this.

new Response.Builder()
                   .code(600) //Simply put whatever value you want to designate to aborted request.
                   .request(chain.request())
                   .build();
1

What I would suggest is the use of a general solution

(Use this in case if you do not find a proper timeout variable to set and to solve your issue).

You could use java.util.concurrent.Future on your API call. Take a look at a tutorial here.

Also, here is my own example:

public String callLongRunOpperation() throws Exception {
             final  long EXECUTION_TIMEOUT = 8L;
             java.util.concurrent.Future <Object> future; 
             java.util.concurrent.ExecutorService executorService = java.util.concurrent.Executors.newFixedThreadPool(1);
             final String res;   
             future = executorService.submit(new java.util.concurrent.Callable<Object>() { 
                   public Object call() throws Exception {
                     return longrunApiCall();
                   }
                 });
               //execute 
                try {
                       res = (String) future.get(EXECUTION_TIMEOUT, java.util.concurrent.TimeUnit.SECONDS); 
                    } catch (java.util.concurrent.ExecutionException ex) { 
                        ex.printStackTrace();
                         // handle execution exception here
                    }
                      catch (InterruptedException ee){
                          ee.printStackTrace();
                         // handle interrupt here
                    }
                       catch (java.util.concurrent.TimeoutException te){
                          // handle timeout here
                          te.printStackTrace();
                    }
                     finally {
                          future.cancel(true);
                          executorService.shutdown();
                    }               
              return res;
         }

Hope this helps.

PKey
  • 3,715
  • 1
  • 14
  • 39