View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.execchain;
29  
30  import java.io.IOException;
31  import java.io.InterruptedIOException;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.HttpException;
36  import org.apache.http.annotation.Immutable;
37  import org.apache.http.client.ServiceUnavailableRetryStrategy;
38  import org.apache.http.client.methods.CloseableHttpResponse;
39  import org.apache.http.client.methods.HttpExecutionAware;
40  import org.apache.http.client.methods.HttpRequestWrapper;
41  import org.apache.http.client.protocol.HttpClientContext;
42  import org.apache.http.conn.routing.HttpRoute;
43  import org.apache.http.util.Args;
44  
45  /**
46   * {@link ClientExecChain} implementation that can automatically retry the request in case of
47   * a non-2xx response using the {@link ServiceUnavailableRetryStrategy} interface.
48   *
49   * @since 4.3
50   */
51  @Immutable
52  public class ServiceUnavailableRetryExec implements ClientExecChain {
53  
54      private final Log log = LogFactory.getLog(getClass());
55  
56      private final ClientExecChain requestExecutor;
57      private final ServiceUnavailableRetryStrategy retryStrategy;
58  
59      public ServiceUnavailableRetryExec(
60              final ClientExecChain requestExecutor,
61              final ServiceUnavailableRetryStrategy retryStrategy) {
62          super();
63          Args.notNull(requestExecutor, "HTTP request executor");
64          Args.notNull(retryStrategy, "Retry strategy");
65          this.requestExecutor = requestExecutor;
66          this.retryStrategy = retryStrategy;
67      }
68  
69      public CloseableHttpResponse execute(
70              final HttpRoute route,
71              final HttpRequestWrapper request,
72              final HttpClientContext context,
73              final HttpExecutionAware execAware) throws IOException, HttpException {
74          for (int c = 1;; c++) {
75              final CloseableHttpResponse response = this.requestExecutor.execute(
76                      route, request, context, execAware);
77              try {
78                  if (this.retryStrategy.retryRequest(response, c, context)) {
79                      response.close();
80                      final long nextInterval = this.retryStrategy.getRetryInterval();
81                      try {
82                          this.log.trace("Wait for " + nextInterval);
83                          Thread.sleep(nextInterval);
84                      } catch (final InterruptedException e) {
85                          Thread.currentThread().interrupt();
86                          throw new InterruptedIOException();
87                      }
88                  } else {
89                      return response;
90                  }
91              } catch (final RuntimeException ex) {
92                  response.close();
93                  throw ex;
94              } catch (final IOException ex) {
95                  response.close();
96                  throw ex;
97              }
98          }
99      }
100 
101 }