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.lang.reflect.UndeclaredThrowableException;
32  
33  import org.apache.http.HttpException;
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.annotation.ThreadingBehavior;
36  import org.apache.http.client.BackoffManager;
37  import org.apache.http.client.ConnectionBackoffStrategy;
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   * @since 4.3
47   */
48  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
49  public class BackoffStrategyExec implements ClientExecChain {
50  
51      private final ClientExecChain requestExecutor;
52      private final ConnectionBackoffStrategy connectionBackoffStrategy;
53      private final BackoffManager backoffManager;
54  
55      public BackoffStrategyExec(
56              final ClientExecChain requestExecutor,
57              final ConnectionBackoffStrategy connectionBackoffStrategy,
58              final BackoffManager backoffManager) {
59          super();
60          Args.notNull(requestExecutor, "HTTP client request executor");
61          Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
62          Args.notNull(backoffManager, "Backoff manager");
63          this.requestExecutor = requestExecutor;
64          this.connectionBackoffStrategy = connectionBackoffStrategy;
65          this.backoffManager = backoffManager;
66      }
67  
68      @Override
69      public CloseableHttpResponse execute(
70              final HttpRoute route,
71              final HttpRequestWrapper request,
72              final HttpClientContext context,
73              final HttpExecutionAware execAware) throws IOException, HttpException {
74          Args.notNull(route, "HTTP route");
75          Args.notNull(request, "HTTP request");
76          Args.notNull(context, "HTTP context");
77          CloseableHttpResponse out = null;
78          try {
79              out = this.requestExecutor.execute(route, request, context, execAware);
80          } catch (final Exception ex) {
81              if (out != null) {
82                  out.close();
83              }
84              if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
85                  this.backoffManager.backOff(route);
86              }
87              if (ex instanceof RuntimeException) {
88                  throw (RuntimeException) ex;
89              }
90              if (ex instanceof HttpException) {
91                  throw (HttpException) ex;
92              }
93              if (ex instanceof IOException) {
94                  throw (IOException) ex;
95              }
96              throw new UndeclaredThrowableException(ex);
97          }
98          if (this.connectionBackoffStrategy.shouldBackoff(out)) {
99              this.backoffManager.backOff(route);
100         } else {
101             this.backoffManager.probe(route);
102         }
103         return out;
104     }
105 
106 }