Skip to content

Commit

Permalink
Merge pull request quarkusio#18881 from geoand/quarkusio#17145
Browse files Browse the repository at this point in the history
Make REST Client Reactive work with Mutiny retry
  • Loading branch information
michalszynkiewicz authored Jul 21, 2021
2 parents fa6cd08 + d59530a commit 3242590
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jboss.resteasy.reactive.client.impl;

import io.smallrye.mutiny.Uni;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
Expand All @@ -16,7 +18,12 @@ public UniInvoker(InvocationBuilderImpl invocationBuilder) {
@Override
public <R> Uni<R> method(String name, Entity<?> entity, GenericType<R> responseType) {
AsyncInvokerImpl invoker = (AsyncInvokerImpl) invocationBuilder.rx();
return Uni.createFrom().completionStage(invoker.method(name, entity, responseType));
return Uni.createFrom().completionStage(new Supplier<CompletionStage<R>>() {
@Override
public CompletionStage<R> get() {
return invoker.method(name, entity, responseType);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.quarkus.it.rest.client.main.MyResponseExceptionMapper.MyException;
import io.smallrye.mutiny.Uni;
import io.vertx.core.Future;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -85,6 +86,16 @@ void init(@Observes Router router) {
}, t -> fail(rc, t.getMessage()));
});

router.route("/call-client-retry").blockingHandler(rc -> {
String url = rc.getBody().toString();
AppleClient client = RestClientBuilder.newBuilder().baseUri(URI.create(url + "/does-not-exist"))
.build(AppleClient.class);
AtomicInteger count = new AtomicInteger(0);
client.uniSwapApple(new Apple("lobo")).onFailure().retry().until(t -> count.incrementAndGet() <= 3)
.subscribe()
.with(m -> success(rc, count.toString()), t -> success(rc, count.toString()));
});

router.post("/hello").handler(rc -> rc.response().putHeader("content-type", MediaType.TEXT_PLAIN)
.end("Hello, " + (rc.getBodyAsString()).repeat(getCount(rc))));

Expand All @@ -109,6 +120,10 @@ void init(@Observes Router router) {
});
}

private Future<Void> success(RoutingContext rc, String body) {
return rc.response().putHeader("content-type", "text-plain").end(body);
}

private int getCount(io.vertx.ext.web.RoutingContext rc) {
List<String> countQueryParam = rc.queryParam("count");
if (countQueryParam.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.stream.Collectors.counting;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.hamcrest.Matchers.equalTo;

import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -55,6 +56,14 @@ void shouldMakeJsonRequest() {
assertThat(valueByCount).containsOnly(entry("cortland", 3L), entry("lobo", 3L), entry("golden delicious", 3L));
}

@Test
void shouldRetryOnFailure() {
RestAssured.with().body(appleUrl).post("/call-client-retry")
.then()
.statusCode(200)
.body(equalTo("4"));
}

@Test
void shouldMapException() {
RestAssured.with().body(baseUrl).post("/call-client-with-exception-mapper")
Expand Down

0 comments on commit 3242590

Please sign in to comment.