|
2 | 2 |
|
3 | 3 | import static java.time.temporal.ChronoUnit.SECONDS; |
4 | 4 |
|
| 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; |
5 | 14 | import io.reflectoring.resilience4j.retry.exceptions.RateLimitExceededException; |
6 | 15 | import io.reflectoring.resilience4j.retry.exceptions.SeatsUnavailableException; |
7 | 16 | import io.reflectoring.resilience4j.retry.model.BookingRequest; |
|
14 | 23 | import io.reflectoring.resilience4j.retry.services.failures.FailHalfTheTime; |
15 | 24 | import io.reflectoring.resilience4j.retry.services.failures.FailNTimes; |
16 | 25 | 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; |
26 | 26 | import io.vavr.CheckedFunction0; |
27 | 27 | import java.time.Duration; |
28 | 28 | import java.util.Collections; |
|
36 | 36 | import java.util.function.Predicate; |
37 | 37 | import java.util.function.Supplier; |
38 | 38 | import java.util.stream.StreamSupport; |
39 | | -import lombok.extern.java.Log; |
40 | 39 |
|
41 | | -@Log |
42 | 40 | public class Examples { |
43 | 41 |
|
44 | 42 | void generalUsagePattern() { |
@@ -131,38 +129,45 @@ void predicateExample() { |
131 | 129 | } |
132 | 130 | } |
133 | 131 |
|
134 | | - void exceptionsExample() { |
| 132 | + void retryExceptionsIgnoreExceptionsExample() { |
135 | 133 | 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(); |
141 | 139 | RetryRegistry registry = RetryRegistry.of(config); |
142 | 140 | Retry retry = registry.retry("flightBookService", config); |
143 | 141 |
|
144 | 142 | 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"); |
146 | 144 | // rate limit exception |
147 | 145 | service.setPotentialFailure(new RateLimitFailNTimes(2)); |
148 | 146 | Flight flight = new Flight("XY 213", "07/30/2020", "NYC", "LAX"); |
149 | 147 | BookingRequest request = new BookingRequest(UUID.randomUUID().toString(), flight, 2, "C"); |
150 | 148 | Supplier<BookingResponse> bookingResponseSupplier = () -> service.bookFlight(request); |
151 | 149 | Supplier<BookingResponse> bookingResponse = Retry.decorateSupplier(retry, bookingResponseSupplier); |
152 | 150 |
|
153 | | - System.out.println(bookingResponse.get()); |
| 151 | + try { |
| 152 | + System.out.println(bookingResponse.get()); |
| 153 | + } |
| 154 | + catch (RateLimitExceededException rle) { |
| 155 | + rle.printStackTrace(); |
| 156 | + } |
154 | 157 |
|
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); |
160 | 165 |
|
161 | 166 | try { |
162 | | - System.out.println(bookingResponse2.get()); |
| 167 | + System.out.println(bookingResponse2.apply()); |
163 | 168 | } |
164 | | - catch (RuntimeException re) { |
165 | | - re.printStackTrace(); |
| 169 | + catch (Throwable e) { |
| 170 | + e.printStackTrace(); |
166 | 171 | } |
167 | 172 | } |
168 | 173 |
|
@@ -314,8 +319,8 @@ public static void main(String[] args) { |
314 | 319 | examples.predicateExample(); |
315 | 320 | System.out.println("----------------------------------------------------------------------"); |
316 | 321 |
|
317 | | - System.out.println("---------------------------- exceptionsExample ------------------------------------------"); |
318 | | - examples.exceptionsExample(); |
| 322 | + System.out.println("---------------------------- retryExceptionsIgnoreExceptionsExample ------------------------------------------"); |
| 323 | + examples.retryExceptionsIgnoreExceptionsExample(); |
319 | 324 | System.out.println("----------------------------------------------------------------------"); |
320 | 325 |
|
321 | 326 | System.out.println("---------------------------- intervalFunction_Random ------------------------------------------"); |
|
0 commit comments