1818import com .gooddata .md .maintenance .ExportImportService ;
1919import com .gooddata .notification .NotificationService ;
2020import com .gooddata .projecttemplate .ProjectTemplateService ;
21+ import com .gooddata .retry .RetrySettings ;
22+ import com .gooddata .retry .GetServerErrorRetryStrategy ;
23+ import com .gooddata .retry .RetryableRestTemplate ;
2124import com .gooddata .util .ResponseErrorHandler ;
2225import com .gooddata .authentication .LoginPasswordAuthentication ;
2326import com .gooddata .warehouse .WarehouseService ;
3841import org .springframework .context .annotation .Bean ;
3942import org .springframework .http .MediaType ;
4043import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
44+ import org .springframework .retry .backoff .ExponentialBackOffPolicy ;
45+ import org .springframework .retry .backoff .FixedBackOffPolicy ;
46+ import org .springframework .retry .policy .SimpleRetryPolicy ;
47+ import org .springframework .retry .support .RetryTemplate ;
4148import org .springframework .util .StreamUtils ;
4249import org .springframework .web .client .RestTemplate ;
4350
4855
4956import static com .gooddata .util .Validate .notNull ;
5057import static java .util .Arrays .asList ;
51- import static java .util .Collections .singletonList ;
5258import static org .apache .http .util .VersionInfo .loadVersionInfo ;
5359
5460/**
@@ -216,7 +222,7 @@ protected GoodData(GoodDataEndpoint endpoint, Authentication authentication) {
216222 protected GoodData (GoodDataEndpoint endpoint , Authentication authentication , GoodDataSettings settings ) {
217223 httpClient = authentication .createHttpClient (endpoint , createHttpClientBuilder (settings ));
218224
219- restTemplate = createRestTemplate (endpoint , httpClient );
225+ restTemplate = createRestTemplate (endpoint , httpClient , settings . getRetrySettings () );
220226
221227 accountService = new AccountService (getRestTemplate (), settings );
222228 projectService = new ProjectService (getRestTemplate (), accountService , settings );
@@ -240,7 +246,7 @@ protected GoodData(GoodDataEndpoint endpoint, Authentication authentication, Goo
240246 lcmService = new LcmService (getRestTemplate (), settings );
241247 }
242248
243- static RestTemplate createRestTemplate (GoodDataEndpoint endpoint , HttpClient httpClient ) {
249+ static RestTemplate createRestTemplate (GoodDataEndpoint endpoint , HttpClient httpClient , RetrySettings retrySettings ) {
244250 notNull (endpoint , "endpoint" );
245251 notNull (httpClient , "httpClient" );
246252
@@ -253,7 +259,12 @@ static RestTemplate createRestTemplate(GoodDataEndpoint endpoint, HttpClient htt
253259 presetHeaders .put ("Accept" , MediaType .APPLICATION_JSON_VALUE );
254260 presetHeaders .put (Header .GDC_VERSION , readApiVersion ());
255261
256- final RestTemplate restTemplate = new RestTemplate (factory );
262+ final RestTemplate restTemplate ;
263+ if (retrySettings == null ) {
264+ restTemplate = new RestTemplate (factory );
265+ } else {
266+ restTemplate = createRetryRestTemplate (retrySettings , factory );
267+ }
257268 restTemplate .setInterceptors (asList (
258269 new HeaderSettingRequestInterceptor (presetHeaders ),
259270 new DeprecationWarningRequestInterceptor ()));
@@ -263,6 +274,30 @@ static RestTemplate createRestTemplate(GoodDataEndpoint endpoint, HttpClient htt
263274 return restTemplate ;
264275 }
265276
277+ private static RestTemplate createRetryRestTemplate (RetrySettings retrySettings , UriPrefixingClientHttpRequestFactory factory ) {
278+ final RetryTemplate retryTemplate = new RetryTemplate ();
279+
280+ if (retrySettings .getRetryCount () != null ) {
281+ retryTemplate .setRetryPolicy (new SimpleRetryPolicy (retrySettings .getRetryCount ()));
282+ }
283+
284+ if (retrySettings .getRetryInitialInterval () != null ) {
285+ if (retrySettings .getRetryMultiplier () != null ) {
286+ final ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy ();
287+ exponentialBackOffPolicy .setInitialInterval (retrySettings .getRetryInitialInterval ());
288+ exponentialBackOffPolicy .setMultiplier (retrySettings .getRetryMultiplier ());
289+ exponentialBackOffPolicy .setMaxInterval (retrySettings .getRetryMaxInterval ());
290+ retryTemplate .setBackOffPolicy (exponentialBackOffPolicy );
291+ } else {
292+ final FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy ();
293+ backOffPolicy .setBackOffPeriod (retrySettings .getRetryInitialInterval ());
294+ retryTemplate .setBackOffPolicy (backOffPolicy );
295+ }
296+ }
297+
298+ return new RetryableRestTemplate (factory , retryTemplate , new GetServerErrorRetryStrategy ());
299+ }
300+
266301 private HttpClientBuilder createHttpClientBuilder (final GoodDataSettings settings ) {
267302 final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager ();
268303 connectionManager .setDefaultMaxPerRoute (settings .getMaxConnections ());
0 commit comments