Skip to content

Commit 899a984

Browse files
committed
Simplify retries
1 parent 41091aa commit 899a984

File tree

8 files changed

+19
-20
lines changed

8 files changed

+19
-20
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package io.github.mfvanek.spring.boot2.test.config;
22

3-
import lombok.RequiredArgsConstructor;
43
import org.springframework.beans.factory.annotation.Value;
54
import org.springframework.context.annotation.Bean;
65
import org.springframework.context.annotation.Configuration;
76
import org.springframework.web.reactive.function.client.WebClient;
87

98
@Configuration
10-
@RequiredArgsConstructor
119
public class WebClientConfig {
1210

1311
@Value("${app.external-base-url}")
1412
private String external;
1513

1614
@Bean
17-
WebClient webClient() {
15+
public WebClient webClient() {
1816
return WebClient.builder().baseUrl(external).build();
1917
}
2018
}

spring-boot-2-demo-app/src/main/java/io/github/mfvanek/spring/boot2/test/service/PublicApiService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import io.github.mfvanek.spring.boot2.test.service.dto.CurrentTime;
77
import io.github.mfvanek.spring.boot2.test.service.dto.ParsedDateTime;
8-
import lombok.Getter;
98
import lombok.RequiredArgsConstructor;
109
import lombok.extern.slf4j.Slf4j;
1110
import org.springframework.beans.factory.annotation.Value;
1211
import org.springframework.http.MediaType;
1312
import org.springframework.lang.Nullable;
13+
import org.springframework.retry.ExhaustedRetryException;
1414
import org.springframework.stereotype.Service;
1515
import org.springframework.web.reactive.function.client.WebClient;
1616
import reactor.core.publisher.Mono;
@@ -23,11 +23,11 @@
2323
@Slf4j
2424
@Service
2525
@RequiredArgsConstructor
26-
@Getter
2726
public class PublicApiService {
2827

2928
@Value("${app.retries}")
30-
private String retries;
29+
private int retries;
30+
3131
private final ObjectMapper mapper;
3232
private final WebClient webClient;
3333

@@ -36,7 +36,7 @@ public LocalDateTime getZonedTime() {
3636
try {
3737
final ParsedDateTime result = getZonedTimeFromWorldTimeApi().getDatetime();
3838
return result.toLocalDateTime();
39-
} catch (RuntimeException e) {
39+
} catch (ExhaustedRetryException e) {
4040
log.warn("Failed to get response", e);
4141
} catch (JsonProcessingException e) {
4242
log.warn("Failed to convert response", e);
@@ -51,12 +51,12 @@ private CurrentTime getZonedTimeFromWorldTimeApi() throws JsonProcessingExceptio
5151
.accept(MediaType.APPLICATION_JSON)
5252
.retrieve()
5353
.bodyToMono(String.class)
54-
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(2))
54+
.retryWhen(Retry.fixedDelay(retries, Duration.ofSeconds(2))
5555
.doBeforeRetry(retrySignal -> log.info("Retrying request to {}, attempt {}/{} due to error:",
5656
webClient.options().uri(String.join("", zoneNames)), retries, retrySignal.totalRetries() + 1, retrySignal.failure()))
5757
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
5858
log.error("Request to {} failed after {} attempts.", webClient.options().uri(String.join("", zoneNames)), retrySignal.totalRetries() + 1);
59-
return new RuntimeException("Retries exhausted", retrySignal.failure());
59+
return new ExhaustedRetryException("Retries exhausted", retrySignal.failure());
6060
})
6161
);
6262
return mapper.readValue(response.block(), CurrentTime.class);

spring-boot-2-demo-app/src/test/java/io/github/mfvanek/spring/boot2/test/service/PublicApiServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void retriesThreeTimesToGetZonedTime(@Nonnull final CapturedOutput output) throw
6565
));
6666

6767
final LocalDateTime result = publicApiService.getZonedTime();
68-
verify(1 + 3, getRequestedFor(urlPathMatching("/" + zoneNames)));
68+
verify(2, getRequestedFor(urlPathMatching("/" + zoneNames)));
6969

7070
assertThat(result).isNull();
7171
assertThat(output).contains("Retrying request to ", "Retries exhausted");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
app:
22
external-base-url: "http://localhost:${wiremock.server.port}/"
3+
retries: 1
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package io.github.mfvanek.spring.boot3.test.config;
22

3-
import lombok.RequiredArgsConstructor;
43
import org.springframework.beans.factory.annotation.Value;
54
import org.springframework.context.annotation.Bean;
65
import org.springframework.context.annotation.Configuration;
76
import org.springframework.web.reactive.function.client.WebClient;
87

98
@Configuration
10-
@RequiredArgsConstructor
119
public class WebClientConfig {
10+
1211
@Value("${app.external-base-url}")
1312
private String external;
1413

1514
@Bean
16-
WebClient webClient() {
15+
public WebClient webClient() {
1716
return WebClient.builder().baseUrl(external).build();
1817
}
1918
}

spring-boot-3-demo-app/src/main/java/io/github/mfvanek/spring/boot3/test/service/PublicApiService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import io.github.mfvanek.spring.boot3.test.service.dto.CurrentTime;
66
import io.github.mfvanek.spring.boot3.test.service.dto.ParsedDateTime;
7-
import lombok.Getter;
87
import lombok.RequiredArgsConstructor;
98
import lombok.extern.slf4j.Slf4j;
109
import org.springframework.beans.factory.annotation.Value;
1110
import org.springframework.http.MediaType;
1211
import org.springframework.lang.Nullable;
12+
import org.springframework.retry.ExhaustedRetryException;
1313
import org.springframework.stereotype.Service;
1414
import org.springframework.web.reactive.function.client.WebClient;
1515
import reactor.core.publisher.Mono;
@@ -22,11 +22,11 @@
2222
@Slf4j
2323
@Service
2424
@RequiredArgsConstructor
25-
@Getter
2625
public class PublicApiService {
2726

2827
@Value("${app.retries}")
29-
private String retries;
28+
private int retries;
29+
3030
private final ObjectMapper mapper;
3131
private final WebClient webClient;
3232

@@ -35,7 +35,7 @@ public LocalDateTime getZonedTime() {
3535
try {
3636
final ParsedDateTime result = getZonedTimeFromWorldTimeApi().getDatetime();
3737
return result.toLocalDateTime();
38-
} catch (RuntimeException e) {
38+
} catch (ExhaustedRetryException e) {
3939
log.warn("Failed to get response", e);
4040
} catch (JsonProcessingException e) {
4141
log.warn("Failed to convert response", e);
@@ -50,12 +50,12 @@ private CurrentTime getZonedTimeFromWorldTimeApi() throws JsonProcessingExceptio
5050
.accept(MediaType.APPLICATION_JSON)
5151
.retrieve()
5252
.bodyToMono(String.class)
53-
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(2))
53+
.retryWhen(Retry.fixedDelay(retries, Duration.ofSeconds(2))
5454
.doBeforeRetry(retrySignal -> log.info("Retrying request to {}, attempt {}/{} due to error:",
5555
webClient.options().uri(String.join("", zoneNames)), retries, retrySignal.totalRetries() + 1, retrySignal.failure()))
5656
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
5757
log.error("Request to {} failed after {} attempts.", webClient.options().uri(String.join("", zoneNames)), retrySignal.totalRetries() + 1);
58-
return new RuntimeException("Retries exhausted", retrySignal.failure());
58+
return new ExhaustedRetryException("Retries exhausted", retrySignal.failure());
5959
})
6060
);
6161
return mapper.readValue(response.block(), CurrentTime.class);

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/service/PublicApiServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void retriesThreeTimesToGetZonedTime(@Nonnull final CapturedOutput output) throw
6565
));
6666

6767
final LocalDateTime result = publicApiService.getZonedTime();
68-
verify(1 + 3, getRequestedFor(urlPathMatching("/" + zoneNames)));
68+
verify(2, getRequestedFor(urlPathMatching("/" + zoneNames)));
6969

7070
assertThat(result).isNull();
7171
assertThat(output).contains("Retrying request to ", "Retries exhausted");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
app:
22
external-base-url: "http://localhost:${wiremock.server.port}/"
3+
retries: 1

0 commit comments

Comments
 (0)