Skip to content

Commit

Permalink
Add HTTP fetch retry
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Feb 23, 2023
1 parent d0d67a3 commit 3278b8b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
7 changes: 0 additions & 7 deletions checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
<module name="IllegalImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<module name="ImportOrder">
<property name="groups"
value="/^ar\./,/^ch\./,/^com\./,/^io\./,/^net\./,/^org\./,/^si\./,/^java\./,/^javax\./"/>
<property name="option" value="bottom"/>
<property name="sortStaticImportsAlphabetically" value="true"/>
<property name="separated" value="true"/>
</module>
<module name="MethodLength"/>
<module name="ParameterNumber"/>
<module name="EmptyForIteratorPad">
Expand Down
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ dependencies {
'org.postgresql:postgresql:42.5.2',
'com.vladmihalcea:hibernate-types-52:2.21.1',
'com.mchange:c3p0:0.9.5.5',
"org.springframework:spring-aspects:5.3.25",
"org.springframework:spring-orm:5.3.25",
"org.springframework:spring-jdbc:5.3.25",
"org.springframework:spring-tx:5.3.25",
"org.springframework:spring-test:5.3.25",
"org.springframework.retry:spring-retry:1.3.4",
)
metrics(
"io.dropwizard.metrics:metrics-core:4.2.15",
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/java/org/mapfish/print/http/HttpRequestFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;
import org.springframework.retry.annotation.Retryable;
import org.springframework.retry.annotation.Backoff;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -191,6 +193,13 @@ public ClientHttpResponse execute() {
return result;
}

@Retryable(value = IOException.class, maxAttemptsExpression = "${httpfetch.retry.maxAttempts}",
backoff = @Backoff(delayExpression = "${httpfetch.retry.backoffDelay}"))
private ClientHttpResponse fetch() throws IOException {
ClientHttpResponse originalResponse = this.originalRequest.execute();
context.stopIfCanceled();
return new CachedClientHttpResponse(originalResponse);
}
@Override
public Void call() throws Exception {
return context.mdcContextEx(() -> {
Expand All @@ -199,9 +208,8 @@ public Void call() throws Exception {
StatsUtils.quotePart(getURI().getHost());
final Timer.Context timerDownload =
HttpRequestFetcher.this.registry.timer(baseMetricName).time();
try (ClientHttpResponse originalResponse = this.originalRequest.execute()) {
context.stopIfCanceled();
this.response = new CachedClientHttpResponse(originalResponse);
try {
this.response = this.fetch();
} catch (IOException e) {
LOGGER.error("Request failed {}", this.originalRequest.getURI(), e);
this.response = new AbstractClientHttpResponse() {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/mapfish-spring.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ db.schema=public

# The default print-apps location
printapps.location=servlet:///print-apps

# Configure the retry mechanism for HTTP requests
httpfetch.retry.maxAttempts=2
httpfetch.retry.backoffDelay=100

0 comments on commit 3278b8b

Please sign in to comment.