001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.client;
029
030import org.apache.http.HttpResponse;
031import org.apache.http.HttpStatus;
032import org.apache.http.annotation.Contract;
033import org.apache.http.annotation.ThreadingBehavior;
034import org.apache.http.client.ServiceUnavailableRetryStrategy;
035import org.apache.http.protocol.HttpContext;
036import org.apache.http.util.Args;
037
038/**
039 * Default implementation of the {@link ServiceUnavailableRetryStrategy} interface.
040 * that retries {@code 503} (Service Unavailable) responses for a fixed number of times
041 * at a fixed interval.
042 *
043 * @since 4.2
044 */
045@Contract(threading = ThreadingBehavior.IMMUTABLE)
046public class DefaultServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
047
048    /**
049     * Maximum number of allowed retries if the server responds with a HTTP code
050     * in our retry code list. Default value is 1.
051     */
052    private final int maxRetries;
053
054    /**
055     * Retry interval between subsequent requests, in milliseconds. Default
056     * value is 1 second.
057     */
058    private final long retryInterval;
059
060    public DefaultServiceUnavailableRetryStrategy(final int maxRetries, final int retryInterval) {
061        super();
062        Args.positive(maxRetries, "Max retries");
063        Args.positive(retryInterval, "Retry interval");
064        this.maxRetries = maxRetries;
065        this.retryInterval = retryInterval;
066    }
067
068    public DefaultServiceUnavailableRetryStrategy() {
069        this(1, 1000);
070    }
071
072    @Override
073    public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) {
074        return executionCount <= maxRetries &&
075            response.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE;
076    }
077
078    @Override
079    public long getRetryInterval() {
080        return retryInterval;
081    }
082
083}