1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.impl.client;
29
30 import java.io.IOException;
31 import java.io.InterruptedIOException;
32 import java.net.URI;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.http.HttpHost;
37 import org.apache.http.HttpRequest;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.annotation.Contract;
40 import org.apache.http.annotation.ThreadingBehavior;
41 import org.apache.http.client.HttpClient;
42 import org.apache.http.client.ResponseHandler;
43 import org.apache.http.client.ServiceUnavailableRetryStrategy;
44 import org.apache.http.client.methods.HttpUriRequest;
45 import org.apache.http.conn.ClientConnectionManager;
46 import org.apache.http.params.HttpParams;
47 import org.apache.http.protocol.HttpContext;
48 import org.apache.http.util.Args;
49 import org.apache.http.util.EntityUtils;
50
51
52
53
54
55
56
57
58
59 @Deprecated
60 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
61 public class AutoRetryHttpClient implements HttpClient {
62
63 private final HttpClient backend;
64
65 private final ServiceUnavailableRetryStrategy retryStrategy;
66
67 private final Log log = LogFactory.getLog(getClass());
68
69 public AutoRetryHttpClient(
70 final HttpClient client, final ServiceUnavailableRetryStrategy retryStrategy) {
71 super();
72 Args.notNull(client, "HttpClient");
73 Args.notNull(retryStrategy, "ServiceUnavailableRetryStrategy");
74 this.backend = client;
75 this.retryStrategy = retryStrategy;
76 }
77
78
79
80
81
82
83 public AutoRetryHttpClient() {
84 this(new DefaultHttpClient(), new DefaultServiceUnavailableRetryStrategy());
85 }
86
87
88
89
90
91
92
93
94
95 public AutoRetryHttpClient(final ServiceUnavailableRetryStrategy config) {
96 this(new DefaultHttpClient(), config);
97 }
98
99
100
101
102
103
104
105
106
107 public AutoRetryHttpClient(final HttpClient client) {
108 this(client, new DefaultServiceUnavailableRetryStrategy());
109 }
110
111 @Override
112 public HttpResponse execute(final HttpHost target, final HttpRequest request)
113 throws IOException {
114 final HttpContext defaultContext = null;
115 return execute(target, request, defaultContext);
116 }
117
118 @Override
119 public <T> T execute(final HttpHost target, final HttpRequest request,
120 final ResponseHandler<? extends T> responseHandler) throws IOException {
121 return execute(target, request, responseHandler, null);
122 }
123
124 @Override
125 public <T> T execute(final HttpHost target, final HttpRequest request,
126 final ResponseHandler<? extends T> responseHandler, final HttpContext context)
127 throws IOException {
128 final HttpResponse resp = execute(target, request, context);
129 return responseHandler.handleResponse(resp);
130 }
131
132 @Override
133 public HttpResponse execute(final HttpUriRequest request) throws IOException {
134 final HttpContext context = null;
135 return execute(request, context);
136 }
137
138 @Override
139 public HttpResponse execute(final HttpUriRequest request, final HttpContext context)
140 throws IOException {
141 final URI uri = request.getURI();
142 final HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(),
143 uri.getScheme());
144 return execute(httpHost, request, context);
145 }
146
147 @Override
148 public <T> T execute(final HttpUriRequest request,
149 final ResponseHandler<? extends T> responseHandler) throws IOException {
150 return execute(request, responseHandler, null);
151 }
152
153 @Override
154 public <T> T execute(final HttpUriRequest request,
155 final ResponseHandler<? extends T> responseHandler, final HttpContext context)
156 throws IOException {
157 final HttpResponse resp = execute(request, context);
158 return responseHandler.handleResponse(resp);
159 }
160
161 @Override
162 public HttpResponse execute(final HttpHost target, final HttpRequest request,
163 final HttpContext context) throws IOException {
164 for (int c = 1;; c++) {
165 final HttpResponse response = backend.execute(target, request, context);
166 try {
167 if (retryStrategy.retryRequest(response, c, context)) {
168 EntityUtils.consume(response.getEntity());
169 final long nextInterval = retryStrategy.getRetryInterval();
170 try {
171 log.trace("Wait for " + nextInterval);
172 Thread.sleep(nextInterval);
173 } catch (final InterruptedException e) {
174 Thread.currentThread().interrupt();
175 throw new InterruptedIOException();
176 }
177 } else {
178 return response;
179 }
180 } catch (final RuntimeException ex) {
181 try {
182 EntityUtils.consume(response.getEntity());
183 } catch (final IOException ioex) {
184 log.warn("I/O error consuming response content", ioex);
185 }
186 throw ex;
187 }
188 }
189 }
190
191 @Override
192 public ClientConnectionManager getConnectionManager() {
193 return backend.getConnectionManager();
194 }
195
196 @Override
197 public HttpParams getParams() {
198 return backend.getParams();
199 }
200
201 }