Skip to content
Open
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
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Configuring Scala Jackson and the addon-on "Enum" module for JSON support](#configuring-scala-jackson-and-the-addon-on-enum-module-for-json-support)
- [Scala DSL for rest-assured (similar to Kotlin DSL)](#scala-dsl-for-rest-assured-similar-to-kotlin-dsl)
- [Functional HTTP routes (Vert.x handlers)](#functional-http-routes-vertx-handlers)
- [Quarkus - Scala3 - Futures](#quarkus---scala3---futures)

## Introduction

Expand Down Expand Up @@ -133,7 +134,7 @@ 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>
```

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

# Quarkus - Scala3 - Futures

# `Future[T]` and `Promise[T]` support in REST endpoints

The `quarkus-scala3-futures` 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 dependency 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-futures</artifactId>
<version>${project.version}</version>
</dependency>
```


## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand All @@ -462,3 +507,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)
- ArC (Quarkus' CDI implementation) 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) ?
68 changes: 68 additions & 0 deletions futures/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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>${project.groupId}</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>


</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,19 @@
package io.quarkiverse.scala.scala3.futures.deployment;

import io.quarkiverse.scala.scala3.futures.runtime.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());
}
}
127 changes: 127 additions & 0 deletions futures/integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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-integration-tests</artifactId>
<name>Quarkus Scala3 futures - Integration Tests</name>

<properties>
<skipITs>false</skipITs>
</properties>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.scala</groupId>
<artifactId>quarkus-scala3-futures</artifactId>
<version>${project.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.quarkiverse.scala</groupId>-->
<!-- <artifactId>quarkus-scala3-futures-deployment</artifactId>-->
<!-- <version>${project.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-compiler_3</artifactId>
<version>${scala.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>${scala.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>

<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<skipITs>false</skipITs>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
</profiles>
</project>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkiverse.scala.scala3.futures.it

import jakarta.ws.rs.GET
import jakarta.ws.rs.Path
import jakarta.ws.rs.Produces
import jakarta.ws.rs.core.MediaType.TEXT_PLAIN

import scala.concurrent.Future
import scala.concurrent.Promise
import scala.concurrent.ExecutionContext.Implicits.global

@Path("")
class Scala3FuturesResource {

@GET
@Path("/hello")
def hello(): String = "Hello from Scala 3.4.1"

@GET
@Path("/simple-future")
@Produces(Array(TEXT_PLAIN))
def simpleFuture: Future[String] = for {
_ <- Future { Thread.sleep(2000L) }
s <- Future.successful("Hello from a Future in Scala 3.4.1")
} yield s

@GET
@Path("simple-promise")
@Produces(Array(TEXT_PLAIN))
def simplePromise: Promise[String] = Promise.successful("Promise returned")


@GET
@Path("future-failure")
@Produces(Array(TEXT_PLAIN))
def futureFailure: Future[String] = Future.failed(new RuntimeException("Future failed"))

}
Loading