001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client2.ext;
014
015import java.io.*;
016
017import org.apache.http.impl.client.*;
018import org.apache.http.protocol.*;
019
020/**
021 * An extension of {@link StandardHttpRequestRetryHandler} that adds support for a retry interval.
022 */
023public class BasicHttpRequestRetryHandler extends StandardHttpRequestRetryHandler {
024
025   private final int retryInterval;
026
027   /**
028    * Create the request retry handler.
029    *
030    * Uses the following list of non-retryable IOException classes:
031    * <ul>
032    * <li>InterruptedIOException</li>
033    * <li>UnknownHostException</li>
034    * <li>ConnectException</li>
035    * <li>SSLException</li>
036    * </ul>
037    *
038    * @param retryCount How many times to retry. <code>0</code> means no retries.
039    * @param retryInterval The time in milliseconds to sleep before trying again, or <code>&lt;=0</code> for no interval.
040    * @param requestSentRetryEnabled Specify <jk>true</jk> if it's OK to retry non-idempotent requests that have been sent.
041    */
042   public BasicHttpRequestRetryHandler(int retryCount, int retryInterval, boolean requestSentRetryEnabled) {
043      super(retryCount, requestSentRetryEnabled);
044      this.retryInterval = retryInterval;
045   }
046
047   @Override
048   public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
049      if (retryInterval > 0) {
050         try {
051            Thread.sleep(retryInterval);
052         } catch (InterruptedException e) {
053            e.printStackTrace();
054         }
055      }
056      return super.retryRequest(exception, executionCount, context);
057   }
058}