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