Search code examples
java-7

Retry based on HTTP response


I want to implement retry framework on HTTP response of API in java.

If response is:

400 : make the parameter null in json and retry again

202 : return success

429 : wait for 2 minutes and try again

5XX : wait for 5 minutes and try again

if retry count exceeds then throw the exception. Is there any library available which supports retry on response type and also allows to edit the request object? If no how can I design one? Is there any tutorials around it?


Solution

  • I know it's been 2 years for this question but maybe this solution helps someone. this solution works for me

    public static void main(String[] args) {
            try {
                long start = System.currentTimeMillis();
                int retry = 0;
                boolean delay = false;
                int status = 0;
                do {
                    if (delay) {
                        Thread.sleep(2000);
                    }
                    String url = "http://httpstat.us/429";
                    URL u;
                    u = new URL(url);
                    HttpURLConnection con = (HttpURLConnection) u.openConnection();
                    int random = (int) Math.random() * 10000;
                    con.setRequestProperty("User-Agent", "skym/1.0.8 /" + random);
                    con.setRequestProperty("Accept", "application/json");
                    con.setRequestMethod("GET");
                    con.setDoOutput(false);
                    con.setDoInput(true);
                    con.setUseCaches(false);
                    con.setAllowUserInteraction(false);
                    con.setConnectTimeout(30000);
                    con.setReadTimeout(30000);
                    con.setRequestProperty("Content-Type", "application/xml");
                    status = con.getResponseCode();
                    con.disconnect();
                    System.out.println(status);
                    System.out.println("Done in " + (System.currentTimeMillis() - start));
                    retry++;
                    delay = true;
                } while (retry < 5 && status == 429);
    
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    

    we can modify this code instead of waiting 2 minute cause some API require 5 minutes or more than five so we need to get the time we should by con.getHeaderFieldInt("Retry-After", -1);

    so the code after modification will be

    public static void main(String[] args) {
            try {
                long start = System.currentTimeMillis();
                int retry = 0;
                boolean delay = false;
                int MIN_RETRY_AFTER = 2;
                int MAX_RETRY_AFTER = 10;
                int status = 0;
                int retryAfter = 0;
                int random = 0;
                URL u;
                do {
                    if (delay) {
                        Thread.sleep(1000 * retryAfter);
                    }
    
                    String url = "http://httpstat.us/429";
    
                    u = new URL(url);
                    HttpURLConnection con = (HttpURLConnection) u.openConnection();
                    random = (int) Math.random() * 10000;
                    con.setRequestProperty("User-Agent", "skym/1.0.8 /" + random);
                    con.setRequestProperty("Accept", "application/json");
                    con.setRequestMethod("GET");
                    con.setDoOutput(false);
                    con.setDoInput(true);
                    con.setUseCaches(false);
                    con.setAllowUserInteraction(false);
                    con.setConnectTimeout(30000);
                    con.setReadTimeout(30000);
                    con.setRequestProperty("Content-Type", "application/xml");
                    status = con.getResponseCode();
    //              to get the time you should wait before the next request
                    retryAfter = con.getHeaderFieldInt("Retry-After", -1);
                    if (retryAfter < 0) {
                        retryAfter = 0;
                    }
                    if (retryAfter < MIN_RETRY_AFTER) {
                        retryAfter = MIN_RETRY_AFTER;
                    } else if (retryAfter > MAX_RETRY_AFTER) {
                        retryAfter = MAX_RETRY_AFTER;
                    }
                    con.disconnect();
                    System.out.println(status);
                    System.out.println(retryAfter);
                    System.out.println("Done in " + (System.currentTimeMillis() - start));
                    retry++;
                    delay = true;
                } while (retry < 5 && status == 429);
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }