Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make REST Client Reactive work with Mutiny retry #18881

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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