forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support blocking exception mappers in REST Client Reactive
Fix quarkusio#30312
- Loading branch information
Showing
11 changed files
with
363 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...ment/src/test/java/io/quarkus/rest/client/reactive/error/BlockingExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package io.quarkus.rest.client.reactive.error; | ||
|
||
import static io.quarkus.rest.client.reactive.RestClientTestUtil.setUrlForClass; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.InputStream; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.UriBuilder; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; | ||
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.resteasy.reactive.common.core.BlockingNotAllowedException; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.ClientExceptionMapper; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.vertx.core.Context; | ||
|
||
public class BlockingExceptionMapperTest { | ||
|
||
private static final AtomicBoolean EVENT_LOOP_THREAD_USED = new AtomicBoolean(); | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(Client.class, | ||
ClientUsingNotBlockingExceptionMapper.class, | ||
ClientUsingBlockingExceptionMapper.class, | ||
ClientUsingBlockingExceptionMapperWithAnnotation.class, | ||
NotBlockingExceptionMapper.class, | ||
BlockingExceptionMapper.class, | ||
Resource.class) | ||
.addAsResource( | ||
new StringAsset(setUrlForClass(ClientUsingNotBlockingExceptionMapper.class) + "\n" | ||
+ setUrlForClass(ClientUsingBlockingExceptionMapper.class) + "\n" | ||
+ setUrlForClass(ClientUsingBlockingExceptionMapperWithAnnotation.class) + "\n"), | ||
"application.properties")); | ||
|
||
public static final String ERROR_MESSAGE = "The entity was not found"; | ||
|
||
@RestClient | ||
ClientUsingNotBlockingExceptionMapper clientUsingNotBlockingExceptionMapper; | ||
|
||
@RestClient | ||
ClientUsingBlockingExceptionMapper clientUsingBlockingExceptionMapper; | ||
|
||
@RestClient | ||
ClientUsingBlockingExceptionMapperWithAnnotation clientUsingBlockingExceptionMapperWithAnnotation; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
EVENT_LOOP_THREAD_USED.set(false); | ||
} | ||
|
||
@Test | ||
public void shouldUseEventLoopByDefault() { | ||
assertThrows(BlockingNotAllowedException.class, clientUsingNotBlockingExceptionMapper::get); | ||
assertThat(EVENT_LOOP_THREAD_USED.get()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void shouldUseWorkerThreadIfExceptionMapperIsAnnotatedWithBlocking() { | ||
RuntimeException exception = assertThrows(RuntimeException.class, clientUsingBlockingExceptionMapper::get); | ||
assertThat(EVENT_LOOP_THREAD_USED.get()).isFalse(); | ||
assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE); | ||
} | ||
|
||
@Test | ||
public void shouldUseWorkerThreadIfExceptionMapperIsAnnotatedWithBlockingAndUsingClientExceptionMapper() { | ||
RuntimeException exception = assertThrows(RuntimeException.class, | ||
clientUsingBlockingExceptionMapperWithAnnotation::get); | ||
assertThat(EVENT_LOOP_THREAD_USED.get()).isFalse(); | ||
assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE); | ||
} | ||
|
||
@Test | ||
public void shouldUseWorkerThreadUsingProgrammaticApproach() { | ||
var client = RestClientBuilder.newBuilder() | ||
.baseUri(UriBuilder.fromUri("http://localhost:8081").build()) | ||
.register(BlockingExceptionMapper.class) | ||
.build(Client.class); | ||
|
||
RuntimeException exception = assertThrows(RuntimeException.class, client::get); | ||
assertThat(EVENT_LOOP_THREAD_USED.get()).isFalse(); | ||
assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE); | ||
} | ||
|
||
@Path("/error") | ||
@RegisterRestClient | ||
public interface Client { | ||
@GET | ||
InputStream get(); | ||
} | ||
|
||
@Path("/error") | ||
@RegisterRestClient | ||
@RegisterProvider(NotBlockingExceptionMapper.class) | ||
public interface ClientUsingNotBlockingExceptionMapper { | ||
@GET | ||
InputStream get(); | ||
} | ||
|
||
@Path("/error") | ||
@RegisterRestClient | ||
@RegisterProvider(BlockingExceptionMapper.class) | ||
public interface ClientUsingBlockingExceptionMapper { | ||
@GET | ||
InputStream get(); | ||
} | ||
|
||
@Path("/error") | ||
@RegisterRestClient | ||
public interface ClientUsingBlockingExceptionMapperWithAnnotation { | ||
@GET | ||
InputStream get(); | ||
|
||
@Blocking | ||
@ClientExceptionMapper | ||
static RuntimeException map(Response response) { | ||
EVENT_LOOP_THREAD_USED.set(Context.isOnEventLoopThread()); | ||
return new RuntimeException(response.readEntity(String.class)); | ||
} | ||
} | ||
|
||
public static class NotBlockingExceptionMapper implements ResponseExceptionMapper<Exception> { | ||
@Override | ||
public Exception toThrowable(Response response) { | ||
EVENT_LOOP_THREAD_USED.set(Context.isOnEventLoopThread()); | ||
// Reading InputStream in the Event Loop throws the BlockingNotAllowedException exception | ||
response.readEntity(String.class); | ||
return null; | ||
} | ||
} | ||
|
||
@Blocking | ||
public static class BlockingExceptionMapper implements ResponseExceptionMapper<Exception> { | ||
@Override | ||
public Exception toThrowable(Response response) { | ||
EVENT_LOOP_THREAD_USED.set(Context.isOnEventLoopThread()); | ||
return new RuntimeException(response.readEntity(String.class)); | ||
} | ||
} | ||
|
||
@Path("/error") | ||
public static class Resource { | ||
@GET | ||
public Response returnError() { | ||
return Response.status(500).entity(ERROR_MESSAGE).build(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...e/src/main/java/io/quarkus/rest/client/reactive/runtime/ClientBlockingResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkus.rest.client.reactive.runtime; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.ws.rs.client.ClientRequestContext; | ||
import jakarta.ws.rs.client.ClientResponseContext; | ||
import jakarta.ws.rs.client.ClientResponseFilter; | ||
|
||
import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl; | ||
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext; | ||
|
||
import io.quarkus.runtime.ExecutorRecorder; | ||
import io.vertx.core.Context; | ||
|
||
public class ClientBlockingResponseFilter implements ClientResponseFilter { | ||
|
||
private volatile Executor executor; | ||
private final Supplier<Executor> supplier = () -> ExecutorRecorder.getCurrent(); | ||
|
||
@Override | ||
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) | ||
throws IOException { | ||
if (!Context.isOnEventLoopThread()) { | ||
return; //already dispatched | ||
} | ||
|
||
if (executor == null) { | ||
executor = supplier.get(); | ||
} | ||
|
||
RestClientRequestContext restClientContext = ((ClientRequestContextImpl) requestContext).getRestClientRequestContext(); | ||
restClientContext.suspend(); | ||
restClientContext.resume(executor); | ||
} | ||
} |
Oops, something went wrong.