Skip to content

Commit 75fead1

Browse files
committed
Clean up and exception related examples changes
1 parent 81355cd commit 75fead1

File tree

6 files changed

+66
-59
lines changed

6 files changed

+66
-59
lines changed

resilience4j/retry/client/pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939
<version>1.5.0</version>
4040
</dependency>
4141

42-
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
43-
<dependency>
44-
<groupId>org.apache.httpcomponents</groupId>
45-
<artifactId>httpclient</artifactId>
46-
<version>4.5.12</version>
47-
</dependency>
48-
4942
<dependency>
5043
<groupId>io.github.resilience4j</groupId>
5144
<artifactId>resilience4j-all</artifactId>
@@ -58,12 +51,6 @@
5851
<version>1.18.12</version>
5952
</dependency>
6053

61-
<dependency>
62-
<groupId>com.fasterxml.jackson.core</groupId>
63-
<artifactId>jackson-databind</artifactId>
64-
<version>2.9.8</version>
65-
</dependency>
66-
6754
<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-micrometer -->
6855
<dependency>
6956
<groupId>io.github.resilience4j</groupId>

resilience4j/retry/client/src/main/java/io/reflectoring/resilience4j/retry/Examples.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
import static java.time.temporal.ChronoUnit.SECONDS;
44

5+
import io.github.resilience4j.core.IntervalFunction;
6+
import io.github.resilience4j.micrometer.tagged.TaggedRetryMetrics;
7+
import io.github.resilience4j.retry.Retry;
8+
import io.github.resilience4j.retry.RetryConfig;
9+
import io.github.resilience4j.retry.RetryRegistry;
10+
import io.micrometer.core.instrument.Meter;
11+
import io.micrometer.core.instrument.MeterRegistry;
12+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
13+
import io.reflectoring.resilience4j.retry.exceptions.FlightServiceBaseException;
514
import io.reflectoring.resilience4j.retry.exceptions.RateLimitExceededException;
615
import io.reflectoring.resilience4j.retry.exceptions.SeatsUnavailableException;
716
import io.reflectoring.resilience4j.retry.model.BookingRequest;
@@ -14,15 +23,6 @@
1423
import io.reflectoring.resilience4j.retry.services.failures.FailHalfTheTime;
1524
import io.reflectoring.resilience4j.retry.services.failures.FailNTimes;
1625
import io.reflectoring.resilience4j.retry.services.failures.RateLimitFailNTimes;
17-
import io.reflectoring.resilience4j.retry.services.failures.SeatsUnavailableFailureNTimes;
18-
import io.github.resilience4j.core.IntervalFunction;
19-
import io.github.resilience4j.micrometer.tagged.TaggedRetryMetrics;
20-
import io.github.resilience4j.retry.Retry;
21-
import io.github.resilience4j.retry.RetryConfig;
22-
import io.github.resilience4j.retry.RetryRegistry;
23-
import io.micrometer.core.instrument.Meter;
24-
import io.micrometer.core.instrument.MeterRegistry;
25-
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
2626
import io.vavr.CheckedFunction0;
2727
import java.time.Duration;
2828
import java.util.Collections;
@@ -36,9 +36,7 @@
3636
import java.util.function.Predicate;
3737
import java.util.function.Supplier;
3838
import java.util.stream.StreamSupport;
39-
import lombok.extern.java.Log;
4039

41-
@Log
4240
public class Examples {
4341

4442
void generalUsagePattern() {
@@ -131,38 +129,45 @@ void predicateExample() {
131129
}
132130
}
133131

134-
void exceptionsExample() {
132+
void retryExceptionsIgnoreExceptionsExample() {
135133
RetryConfig config = RetryConfig.custom().
136-
maxAttempts(3).
137-
waitDuration(Duration.of(3, SECONDS)).
138-
retryExceptions(RateLimitExceededException.class).
139-
ignoreExceptions(SeatsUnavailableException.class).
140-
build();
134+
maxAttempts(3).
135+
waitDuration(Duration.of(3, SECONDS)).
136+
retryExceptions(FlightServiceBaseException.class).
137+
ignoreExceptions(SeatsUnavailableException.class).
138+
build();
141139
RetryRegistry registry = RetryRegistry.of(config);
142140
Retry retry = registry.retry("flightBookService", config);
143141

144142
FlightBookingService service = new FlightBookingService();
145-
System.out.println("Example to illustrate: rate limit exception - will be retried");
143+
System.out.println("Example to illustrate: rate limit runtime exception - will be retried");
146144
// rate limit exception
147145
service.setPotentialFailure(new RateLimitFailNTimes(2));
148146
Flight flight = new Flight("XY 213", "07/30/2020", "NYC", "LAX");
149147
BookingRequest request = new BookingRequest(UUID.randomUUID().toString(), flight, 2, "C");
150148
Supplier<BookingResponse> bookingResponseSupplier = () -> service.bookFlight(request);
151149
Supplier<BookingResponse> bookingResponse = Retry.decorateSupplier(retry, bookingResponseSupplier);
152150

153-
System.out.println(bookingResponse.get());
151+
try {
152+
System.out.println(bookingResponse.get());
153+
}
154+
catch (RateLimitExceededException rle) {
155+
rle.printStackTrace();
156+
}
154157

155-
System.out.println("Example to illustrate: no seats available - no retry");
156-
// seats not available exception
157-
service.setPotentialFailure(new SeatsUnavailableFailureNTimes(2));
158-
Supplier<BookingResponse> bookingResponseSupplier2 = () -> service.bookFlight(request);
159-
Supplier<BookingResponse> bookingResponse2 = Retry.decorateSupplier(retry, bookingResponseSupplier2);
158+
System.out.println("Example to illustrate: no seats available runtime exception will not be retried");
159+
// seats not available checked exception
160+
FlightBookingService service2 = new FlightBookingService();
161+
Flight flight2 = new Flight("XY 765", "07/30/2020", "NYC", "LAX");
162+
BookingRequest request2 = new BookingRequest(UUID.randomUUID().toString(), flight2, 2, "C");
163+
CheckedFunction0<BookingResponse> bookingResponseSupplier2 = () -> service2.bookFlight(request2);
164+
CheckedFunction0<BookingResponse> bookingResponse2 = Retry.decorateCheckedSupplier(retry, bookingResponseSupplier2);
160165

161166
try {
162-
System.out.println(bookingResponse2.get());
167+
System.out.println(bookingResponse2.apply());
163168
}
164-
catch (RuntimeException re) {
165-
re.printStackTrace();
169+
catch (Throwable e) {
170+
e.printStackTrace();
166171
}
167172
}
168173

@@ -314,8 +319,8 @@ public static void main(String[] args) {
314319
examples.predicateExample();
315320
System.out.println("----------------------------------------------------------------------");
316321

317-
System.out.println("---------------------------- exceptionsExample ------------------------------------------");
318-
examples.exceptionsExample();
322+
System.out.println("---------------------------- retryExceptionsIgnoreExceptionsExample ------------------------------------------");
323+
examples.retryExceptionsIgnoreExceptionsExample();
319324
System.out.println("----------------------------------------------------------------------");
320325

321326
System.out.println("---------------------------- intervalFunction_Random ------------------------------------------");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.reflectoring.resilience4j.retry.exceptions;
2+
3+
public class FlightServiceBaseException extends RuntimeException {
4+
public FlightServiceBaseException(String message) {
5+
super(message);
6+
}
7+
}

resilience4j/retry/client/src/main/java/io/reflectoring/resilience4j/retry/exceptions/RateLimitExceededException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.reflectoring.resilience4j.retry.exceptions;
22

3-
public class RateLimitExceededException extends RuntimeException {
3+
public class RateLimitExceededException extends FlightServiceBaseException {
44
String errorCode;
55

66
public RateLimitExceededException(String message, String errorCode) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.reflectoring.resilience4j.retry.exceptions;
22

3-
public class SeatsUnavailableException extends RuntimeException {
3+
public class SeatsUnavailableException extends FlightServiceBaseException {
44
public SeatsUnavailableException(String message) {
55
super(message);
66
}
7-
}
7+
}

resilience4j/retry/client/src/main/java/io/reflectoring/resilience4j/retry/services/FlightSearchService.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
import io.reflectoring.resilience4j.retry.model.SearchResponse;
66
import io.reflectoring.resilience4j.retry.services.failures.NoFailure;
77
import io.reflectoring.resilience4j.retry.services.failures.PotentialFailure;
8-
import com.fasterxml.jackson.databind.ObjectMapper;
98
import java.io.IOException;
109
import java.time.LocalDateTime;
1110
import java.time.format.DateTimeFormatter;
1211
import java.util.Arrays;
12+
import java.util.Collections;
1313
import java.util.List;
14-
import org.apache.http.HttpResponse;
15-
import org.apache.http.client.HttpClient;
16-
import org.apache.http.client.methods.HttpGet;
17-
import org.apache.http.impl.client.HttpClientBuilder;
18-
import org.apache.http.util.EntityUtils;
1914

2015
public class FlightSearchService {
2116
PotentialFailure potentialFailure = new NoFailure();
2217
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss SSS");
23-
ObjectMapper mapper = new ObjectMapper();
2418

2519
public List<Flight> searchFlights(SearchRequest request) {
2620
System.out.println("Searching for flights; current time = " + LocalDateTime.now().format(formatter));
@@ -38,8 +32,6 @@ public List<Flight> searchFlights(SearchRequest request) {
3832

3933
public List<Flight> searchFlightsThrowingException(SearchRequest request) throws Exception {
4034
System.out.println("Searching for flights; current time = " + LocalDateTime.now().format(formatter));
41-
potentialFailure.occur();
42-
4335
throw new Exception("Exception when searching for flights");
4436
}
4537

@@ -51,10 +43,26 @@ public SearchResponse httpSearchFlights(SearchRequest request) throws IOExceptio
5143
System.out.println("Searching for flights; current time = " + LocalDateTime.now().format(formatter));
5244
potentialFailure.occur();
5345

54-
HttpClient client = HttpClientBuilder.create().build();
55-
String url = "http://localhost:8080/flights/search?from=" + request.getFrom() + "&to=" + request.getTo() + "&date=" + request.getFlightDate();
56-
HttpResponse response = client.execute(new HttpGet(url));
57-
String body = EntityUtils.toString(response.getEntity());
58-
return mapper.readValue(body, SearchResponse.class);
46+
String date = request.getFlightDate();
47+
String from = request.getFrom();
48+
String to = request.getTo();
49+
if (request.getFlightDate().equals("07/25/2020")) { // Simulating an error scenario
50+
System.out.println("Flight data initialization in progress, cannot search at this time");
51+
SearchResponse response = new SearchResponse();
52+
response.setErrorCode("FS-167");
53+
response.setFlights(Collections.emptyList());
54+
return response;
55+
}
56+
57+
List<Flight> flights = Arrays.asList(
58+
new Flight("XY 765", date, from, to),
59+
new Flight("XY 781", date, from, to),
60+
new Flight("XY 732", date, from, to),
61+
new Flight("XY 746", date, from, to)
62+
);
63+
System.out.println("Flight search successful");
64+
SearchResponse response = new SearchResponse();
65+
response.setFlights(flights);
66+
return response;
5967
}
6068
}

0 commit comments

Comments
 (0)