Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ VERSIONS = [

dependencies {
implementation "io.quarkiverse.scala:quarkus-scala3:${VERSIONS.QUARKUS_SCALA3}"
implementation "io.quarkiverse.scala:quarkus-scala3-deployment:${VERSIONS.QUARKUS_SCALA3}"
implementation("org.scala-lang:scala3-compiler_3") {
version {
strictly VERSIONS.SCALA3
Expand Down Expand Up @@ -133,7 +134,12 @@ In your `pom.xml` file, add:
<dependency>
<groupId>io.quarkiverse.scala</groupId>
<artifactId>quarkus-scala3</artifactId>
<version>0.0.1<version>
<version>1.0.0<version>
</dependency>
<dependency>
<groupId>io.quarkiverse.scala</groupId>
<artifactId>quarkus-scala3-deployment</artifactId>
<version>1.0.0</version>
</dependency>
```

Expand Down Expand Up @@ -440,6 +446,54 @@ def mkRoutes(router: Router) =
})
```


### `Future[T]` and `Promise[T]` support in Rest-Endpoints

This extension allows you to return `Future[T]` and `Promise[T]` from your rest-endpoints.

```scala

@Path("/")
class GreetingResource

@GET
@Path("/greet/future")
@Produces(Array(TEXT_PLAIN))
def futureGreeting(): Future[String] =
Future.successful("Hello from the future")
end futureGreeting

@GET
@Path("/greet/promise")
def promiseGreeting(): Promise[String] =
Promise.successful("Hello from the promise")
end promiseGreeting

end GreetingResource
```

If the `Future[T]` or `Promise[T]` fails, the normal exception handling is invoked.

Make sure to have the following dependencies in your `pom.xml` to make it work:

```xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.scala</groupId>
<artifactId>quarkus-scala3</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.scala</groupId>
<artifactId>quarkus-scala3-deployment</artifactId>
<version>${project.version}</version>
</dependency>
```


## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand All @@ -452,6 +506,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/GavinRay97"><img src="https://avatars.githubusercontent.com/u/26604994?v=4?s=100" width="100px;" alt="Gavin Ray"/><br /><sub><b>Gavin Ray</b></sub></a><br /><a href="https://github.com/quarkiverse/quarkus-scala3/commits?author=GavinRay97" title="Code">💻</a> <a href="#maintenance-GavinRay97" title="Maintenance">🚧</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://lesincroyableslivres.fr/"><img src="https://avatars.githubusercontent.com/u/1279749?v=4?s=100" width="100px;" alt="Guillaume Smet"/><br /><sub><b>Guillaume Smet</b></sub></a><br /><a href="https://github.com/quarkiverse/quarkus-scala3/commits?author=gsmet" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/domdorn"><img src="https://avatars.githubusercontent.com/u/100349?v=4?s=100" width="100px;" alt="Dominik Dorn"/><br /><sub><b>Dominik Dorn</b></sub></a><br /><a href="https://github.com/quarkiverse/quarkus-scala3/commits?author=domdorn" title="Code">🚀🍺💻</a></td>
</tr>
</tbody>
</table>
Expand All @@ -462,3 +517,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## TODOs
- correctly generate OpenAPI Spec for methods returning `Future[T]` or `Promise[T]`, e.g. similar to [Quarkus #8499](https://github.com/quarkusio/quarkus/issues/8499)
- Quarkus-Arc has special handling for `CompletionStage[T]`, maybe we should add similar handling for `Future[T]` and `Promise[T]`, see [ActiveRequestContextInterceptor](https://github.com/quarkusio/quarkus/blob/24d3e5262d20fdaa8c056d59f012f8c7b5b1c5c8/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ActivateRequestContextInterceptor.java) ?
73 changes: 73 additions & 0 deletions futures/deployment/pom.xml
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkiverse.scala.scala3.deployment;

import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler;

public class Scala3FutureResponseHandler implements ServerRestHandler {

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());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkiverse.scala.scala3.deployment;

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);
}

}
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());
}
}
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());
}
}
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());
}
}
Loading