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.Header;
36  import org.apache.http.HttpException;
37  import org.apache.http.annotation.Immutable;
38  import org.apache.http.client.ServiceUnavailableRetryStrategy;
39  import org.apache.http.client.methods.CloseableHttpResponse;
40  import org.apache.http.client.methods.HttpExecutionAware;
41  import org.apache.http.client.methods.HttpRequestWrapper;
42  import org.apache.http.client.protocol.HttpClientContext;
43  import org.apache.http.conn.routing.HttpRoute;
44  import org.apache.http.util.Args;
45  
46  /**
47   * Request executor in the request execution chain that is responsible
48   * for making a decision whether a request that received a non-2xx response
49   * from the target server should be re-executed.
50   * <p/>
51   * Further responsibilities such as communication with the opposite
52   * endpoint is delegated to the next executor in the request execution
53   * chain.
54   *
55   * @since 4.3
56   */
57  @Immutable
58  public class ServiceUnavailableRetryExec implements ClientExecChain {
59  
60      private final Log log = LogFactory.getLog(getClass());
61  
62      private final ClientExecChain requestExecutor;
63      private final ServiceUnavailableRetryStrategy retryStrategy;
64  
65      public ServiceUnavailableRetryExec(
66              final ClientExecChain requestExecutor,
67              final ServiceUnavailableRetryStrategy retryStrategy) {
68          super();
69          Args.notNull(requestExecutor, "HTTP request executor");
70          Args.notNull(retryStrategy, "Retry strategy");
71          this.requestExecutor = requestExecutor;
72          this.retryStrategy = retryStrategy;
73      }
74  
75      public CloseableHttpResponse execute(
76              final HttpRoute route,
77              final HttpRequestWrapper request,
78              final HttpClientContext context,
79              final HttpExecutionAware execAware) throws IOException, HttpException {
80          final Header[] origheaders = request.getAllHeaders();
81          for (int c = 1;; c++) {
82              final CloseableHttpResponse response = this.requestExecutor.execute(
83                      route, request, context, execAware);
84              try {
85                  if (this.retryStrategy.retryRequest(response, c, context)) {
86                      response.close();
87                      final long nextInterval = this.retryStrategy.getRetryInterval();
88                      if (nextInterval > 0) {
89                          try {
90                              this.log.trace("Wait for " + nextInterval);
91                              Thread.sleep(nextInterval);
92                          } catch (final InterruptedException e) {
93                              Thread.currentThread().interrupt();
94                              throw new InterruptedIOException();
95                          }
96                      }
97                      request.setHeaders(origheaders);
98                  } else {
99                      return response;
100                 }
101             } catch (final RuntimeException ex) {
102                 response.close();
103                 throw ex;
104             }
105         }
106     }
107 
108 }