-
Notifications
You must be signed in to change notification settings - Fork 6
Future[T] and Promise[T] as Controller ReturnTypes #51
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
Open
domdorn
wants to merge
6
commits into
quarkiverse:main
Choose a base branch
from
domdorn:futures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d6a441
Support for using `Future[T]` or `Promise[T]` as response type in Res…
domdorn cfd8729
Update README.md
domdorn 53c92b2
Update README.md
domdorn f906733
Update README.md
domdorn 2b393ee
futures: PR Feedback incorporated
domdorn 2c1f73d
PR feedback integrated
domdorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,73 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>io.quarkiverse.scala</groupId> | ||
| <artifactId>quarkus-scala3-futures-parent</artifactId> | ||
| <version>999-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>quarkus-scala3-futures-deployment</artifactId> | ||
| <name>Quarkus Scala3 futures - Deployment</name> | ||
|
|
||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.quarkus</groupId> | ||
| <artifactId>quarkus-arc-deployment</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.quarkiverse.scala</groupId> | ||
| <artifactId>quarkus-scala3-futures</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.quarkus</groupId> | ||
| <artifactId>quarkus-junit5-internal</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>io.quarkus</groupId> | ||
| <artifactId>quarkus-rest-spi-deployment</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.quarkus.resteasy.reactive</groupId> | ||
| <artifactId>resteasy-reactive-processor</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.quarkus</groupId> | ||
| <artifactId>quarkus-rest-server-spi-deployment</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.scala-lang</groupId> | ||
| <artifactId>scala3-library_3</artifactId> | ||
| <scope>compile</scope> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
| <build> | ||
| <sourceDirectory>src/main/scala</sourceDirectory> | ||
| <testSourceDirectory>src/test/scala</testSourceDirectory> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>io.quarkus</groupId> | ||
| <artifactId>quarkus-extension-processor</artifactId> | ||
| <version>${quarkus.version}</version> | ||
| </path> | ||
| </annotationProcessorPaths> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>net.alchim31.maven</groupId> | ||
| <artifactId>scala-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
34 changes: 34 additions & 0 deletions
34
...ain/scala/io/quarkiverse/scala/scala3/futures/deployment/Scala3FutureResponseHandler.java
This file contains hidden or 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,34 @@ | ||
| package io.quarkiverse.scala.scala3.deployment; | ||
domdorn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
| import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
|
||
| public class Scala3FutureResponseHandler implements ServerRestHandler { | ||
domdorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private void handleFuture(ResteasyReactiveRequestContext requestContext, | ||
| scala.concurrent.Future<?> f) { | ||
|
|
||
| requestContext.suspend(); | ||
|
|
||
| f.onComplete(tryValue -> { | ||
| if (tryValue.isSuccess()) { | ||
| requestContext.setResult(tryValue.get()); | ||
| } else { | ||
| requestContext.handleException(tryValue.failed().get(), true); | ||
| } | ||
| requestContext.resume(); | ||
| return null; | ||
| }, scala.concurrent.ExecutionContext.global()); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void handle(ResteasyReactiveRequestContext requestContext) throws Exception { | ||
| if (requestContext.getResult() instanceof scala.concurrent.Future<?> future) { | ||
| handleFuture(requestContext, future); | ||
| } else if (requestContext.getResult() instanceof scala.concurrent.Promise<?> promise) { | ||
| handleFuture(requestContext, promise.future()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
50 changes: 50 additions & 0 deletions
50
...a/io/quarkiverse/scala/scala3/futures/deployment/Scala3FutureReturnTypeMethodScanner.java
This file contains hidden or 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,50 @@ | ||
| package io.quarkiverse.scala.scala3.deployment; | ||
domdorn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.jboss.jandex.ClassInfo; | ||
| import org.jboss.jandex.DotName; | ||
| import org.jboss.jandex.MethodInfo; | ||
| import org.jboss.resteasy.reactive.server.model.FixedHandlerChainCustomizer; | ||
| import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer; | ||
| import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer.Phase; | ||
| import org.jboss.resteasy.reactive.server.processor.scanning.MethodScanner; | ||
|
|
||
| public class Scala3FutureReturnTypeMethodScanner implements MethodScanner { | ||
| private static final DotName FUTURE = DotName.createSimple("scala.concurrent.Future"); | ||
| private static final DotName PROMISE = DotName.createSimple("scala.concurrent.Promise"); | ||
| private static final DotName BLOCKING_ANNOTATION = DotName.createSimple("io.smallrye.common.annotation.Blocking"); | ||
|
|
||
| private void ensureNotBlocking(MethodInfo method) { | ||
| if (method.annotation(BLOCKING_ANNOTATION) != null) { | ||
| String format = String.format("Suspendable @Blocking methods are not supported yet: %s.%s", | ||
| method.declaringClass().name(), method.name()); | ||
| throw new IllegalStateException(format); | ||
| } | ||
| } | ||
|
|
||
| public Scala3FutureReturnTypeMethodScanner() { | ||
| } | ||
|
|
||
| @Override | ||
| public List<HandlerChainCustomizer> scan(MethodInfo method, ClassInfo actualEndpointClass, | ||
| Map<String, Object> methodContext) { | ||
|
|
||
| if (isMethodSignatureAsync(method)) { | ||
| ensureNotBlocking(method); | ||
|
|
||
| return Collections.singletonList(new FixedHandlerChainCustomizer( | ||
| new Scala3FutureResponseHandler(), Phase.AFTER_METHOD_INVOKE)); | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isMethodSignatureAsync(MethodInfo info) { | ||
| DotName name = info.returnType().name(); | ||
| return name.equals(FUTURE) || name.equals(PROMISE); | ||
| } | ||
|
|
||
| } | ||
19 changes: 19 additions & 0 deletions
19
...main/scala/io/quarkiverse/scala/scala3/futures/deployment/Scala3FuturesJavaProcessor.java
This file contains hidden or 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,19 @@ | ||
| package io.quarkiverse.scala.scala3.futures.deployment; | ||
|
|
||
| import io.quarkiverse.scala.scala3.deployment.Scala3FutureReturnTypeMethodScanner; | ||
| import io.quarkus.deployment.annotations.BuildStep; | ||
| import io.quarkus.deployment.builditem.FeatureBuildItem; | ||
| import io.quarkus.resteasy.reactive.server.spi.MethodScannerBuildItem; | ||
|
|
||
| public class Scala3FuturesJavaProcessor { | ||
|
|
||
| @BuildStep | ||
| public FeatureBuildItem feature() { | ||
| return new FeatureBuildItem("scala3-futures"); | ||
| } | ||
|
|
||
| @BuildStep | ||
| public MethodScannerBuildItem registerFuturesRestReturnTypes() { | ||
| return new MethodScannerBuildItem(new Scala3FutureReturnTypeMethodScanner()); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...nt/src/test/scala/io/quarkiverse/scala/scala3/futures/test/Scala3FuturesDevModeTest.scala
This file contains hidden or 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,23 @@ | ||
| package io.quarkiverse.scala.scala3.futures.test | ||
|
|
||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import io.quarkus.test.QuarkusDevModeTest; | ||
|
|
||
| class Scala3FuturesDevModeTest { | ||
|
|
||
| // Start hot reload (DevMode) test with your extension loaded | ||
| @RegisterExtension | ||
| val devModeTest: QuarkusDevModeTest = new QuarkusDevModeTest() | ||
| .setArchiveProducer(() => ShrinkWrap.create(classOf[JavaArchive])) | ||
|
|
||
| @Test | ||
| def writeYourOwnDevModeTest(): Unit = { | ||
| // Write your dev mode tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-hot-reload for more information | ||
| Assertions.assertTrue(true, "Add dev mode assertions to " + getClass().getName()); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...eployment/src/test/scala/io/quarkiverse/scala/scala3/futures/test/Scala3FuturesTest.scala
This file contains hidden or 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,23 @@ | ||
| package io.quarkiverse.scala.scala3.futures.test | ||
|
|
||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import io.quarkus.test.QuarkusUnitTest; | ||
|
|
||
| class Scala3FuturesTest { | ||
|
|
||
| // Start unit test with your extension loaded | ||
| @RegisterExtension | ||
| def unitTest: QuarkusUnitTest = new QuarkusUnitTest() | ||
| .setArchiveProducer(() => ShrinkWrap.create(classOf[JavaArchive])) | ||
|
|
||
| @Test | ||
| def writeYourOwnUnitTest(): Unit = { | ||
| // Write your unit tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-extensions for more information | ||
| Assertions.assertTrue(true, "Add some assertions to " + getClass().getName()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.