Skip to content

Commit

Permalink
Vert.x: support Kotlin suspend functions in event bus consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Jun 10, 2024
1 parent eb0d569 commit ea2c6a4
Show file tree
Hide file tree
Showing 25 changed files with 788 additions and 8 deletions.
15 changes: 15 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,11 @@
<artifactId>quarkus-vertx-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-deployment-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-latebound-mdc-provider</artifactId>
Expand Down Expand Up @@ -1952,6 +1957,16 @@
<artifactId>quarkus-vertx-http-dev-ui-resources</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin-deployment</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
Expand Down
13 changes: 13 additions & 0 deletions devtools/bom-descriptor-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-virtual-threads</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2955,6 +2955,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-virtual-threads-deployment</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions docs/src/main/asciidoc/kotlin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ The following extensions provide support for Kotlin Coroutines by allowing the u
|`quarkus-smallrye-fault-tolerance`
|Support is provided for the declarative annotation-based API

|`quarkus-vertx`
|Support is provided for `@ConsumeEvent` methods

Check warning on line 505 in docs/src/main/asciidoc/kotlin.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Spelling] Use correct American English spelling. Did you really mean 'coroutines'? Raw Output: {"message": "[Quarkus.Spelling] Use correct American English spelling. Did you really mean 'coroutines'?", "location": {"path": "docs/src/main/asciidoc/kotlin.adoc", "range": {"start": {"line": 505, "column": 9}}}, "severity": "WARNING"}

|===

=== Kotlin coroutines and Mutiny

Check warning on line 509 in docs/src/main/asciidoc/kotlin.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Kotlin coroutines and Mutiny'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Kotlin coroutines and Mutiny'.", "location": {"path": "docs/src/main/asciidoc/kotlin.adoc", "range": {"start": {"line": 509, "column": 5}}}, "severity": "INFO"}

Check warning on line 509 in docs/src/main/asciidoc/kotlin.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Spelling] Use correct American English spelling. Did you really mean 'coroutines'? Raw Output: {"message": "[Quarkus.Spelling] Use correct American English spelling. Did you really mean 'coroutines'?", "location": {"path": "docs/src/main/asciidoc/kotlin.adoc", "range": {"start": {"line": 509, "column": 12}}}, "severity": "WARNING"}
Expand Down
5 changes: 5 additions & 0 deletions extensions/kotlin/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>kotlin-compiler</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin-deployment</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 6 additions & 0 deletions extensions/kotlin/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,11 @@
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-jdk8</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
26 changes: 26 additions & 0 deletions extensions/vertx/deployment-spi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-vertx-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-vertx-deployment-spi</artifactId>
<name>Quarkus - Vert.x - Deployment - SPI</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus.arc</groupId>
<artifactId>arc-processor</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.vertx.deployment.spi;

import java.util.function.BiConsumer;

import org.jboss.jandex.MethodInfo;

import io.quarkus.arc.processor.InvokerBuilder;
import io.quarkus.builder.item.MultiBuildItem;

/**
* This build item should be considered private and should not be used outside of core Quarkus.
* It can be changed without notice.
*/
public final class EventConsumerInvokerCustomizerBuildItem extends MultiBuildItem {
private final BiConsumer<MethodInfo, InvokerBuilder> invokerCustomizer;

public EventConsumerInvokerCustomizerBuildItem(BiConsumer<MethodInfo, InvokerBuilder> invokerCustomizer) {
this.invokerCustomizer = invokerCustomizer;
}

public BiConsumer<MethodInfo, InvokerBuilder> getInvokerCustomizer() {
return invokerCustomizer;
}
}
4 changes: 4 additions & 0 deletions extensions/vertx/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-deployment-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.arc.processor.InvokerBuilder;
import io.quarkus.arc.processor.InvokerInfo;
import io.quarkus.arc.processor.KotlinUtils;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
Expand All @@ -59,6 +60,7 @@
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.vertx.ConsumeEvent;
import io.quarkus.vertx.core.deployment.CoreVertxBuildItem;
import io.quarkus.vertx.deployment.spi.EventConsumerInvokerCustomizerBuildItem;
import io.quarkus.vertx.runtime.EventConsumerInfo;
import io.quarkus.vertx.runtime.VertxEventBusConsumerRecorder;
import io.quarkus.vertx.runtime.VertxProducer;
Expand Down Expand Up @@ -137,6 +139,7 @@ public UnremovableBeanBuildItem unremovableBeans() {
void collectEventConsumers(
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
InvokerFactoryBuildItem invokerFactory,
List<EventConsumerInvokerCustomizerBuildItem> invokerCustomizers,
BuildProducer<EventConsumerBusinessMethodItem> messageConsumerBusinessMethods,
BuildProducer<BeanConfiguratorBuildItem> errors) {
// We need to collect all business methods annotated with @ConsumeEvent first
Expand All @@ -149,8 +152,13 @@ void collectEventConsumers(
AnnotationInstance consumeEvent = annotationStore.getAnnotation(method, CONSUME_EVENT);
if (consumeEvent != null) {
// Validate method params and return type
int parametersCount = method.parametersCount();
if (KotlinUtils.isKotlinSuspendMethod(method)) {
parametersCount--;
}

List<Type> params = method.parameterTypes();
if (params.size() == 2) {
if (parametersCount == 2) {
if (!isMessageHeaders(params.get(0).name())) {
// If there are two parameters, the first must be message headers.
throw new IllegalStateException(String.format(
Expand All @@ -161,12 +169,13 @@ void collectEventConsumers(
"An event consumer business method with two parameters must not accept io.vertx.core.eventbus.Message or io.vertx.mutiny.core.eventbus.Message: %s [method: %s, bean:%s]",
params, method, bean));
}
} else if (params.size() != 1) {
} else if (parametersCount != 1) {
throw new IllegalStateException(String.format(
"An event consumer business method must accept exactly one parameter: %s [method: %s, bean:%s]",
params, method, bean));
}
if (method.returnType().kind() != Kind.VOID && VertxConstants.isMessage(params.get(0).name())) {
if (method.returnType().kind() != Kind.VOID && VertxConstants.isMessage(params.get(0).name())
&& !KotlinUtils.isKotlinSuspendMethod(method)) {
throw new IllegalStateException(String.format(
"An event consumer business method that accepts io.vertx.core.eventbus.Message or io.vertx.mutiny.core.eventbus.Message must return void [method: %s, bean:%s]",
method, bean));
Expand All @@ -181,16 +190,16 @@ void collectEventConsumers(
InvokerBuilder builder = invokerFactory.createInvoker(bean, method)
.withInstanceLookup();

if (method.parametersCount() == 1 && method.parameterType(0).name().equals(MESSAGE)) {
if (parametersCount == 1 && method.parameterType(0).name().equals(MESSAGE)) {
// io.vertx.core.eventbus.Message
// no transformation required
} else if (method.parametersCount() == 1 && method.parameterType(0).name().equals(MUTINY_MESSAGE)) {
} else if (parametersCount == 1 && method.parameterType(0).name().equals(MUTINY_MESSAGE)) {
// io.vertx.mutiny.core.eventbus.Message
builder.withArgumentTransformer(0, io.vertx.mutiny.core.eventbus.Message.class, "newInstance");
} else if (method.parametersCount() == 1) {
} else if (parametersCount == 1) {
// parameter is payload
builder.withArgumentTransformer(0, io.vertx.core.eventbus.Message.class, "body");
} else if (method.parametersCount() == 2 && method.parameterType(0).name().equals(MUTINY_MESSAGE_HEADERS)) {
} else if (parametersCount == 2 && method.parameterType(0).name().equals(MUTINY_MESSAGE_HEADERS)) {
// if the method expects Mutiny MultiMap, wrap the Vert.x MultiMap
builder.withArgumentTransformer(0, io.vertx.mutiny.core.MultiMap.class, "newInstance");
}
Expand All @@ -199,11 +208,16 @@ void collectEventConsumers(
builder.withReturnValueTransformer(Uni.class, "subscribeAsCompletionStage");
}

// the rest of Kotlin suspend function support is in the `vertx-kotlin` extension
for (EventConsumerInvokerCustomizerBuildItem invokerCustomizer : invokerCustomizers) {
invokerCustomizer.getInvokerCustomizer().accept(method, builder);
}

InvokerInfo invoker = builder.build();

messageConsumerBusinessMethods.produce(new EventConsumerBusinessMethodItem(bean, consumeEvent,
method.hasAnnotation(Blocking.class), method.hasAnnotation(RunOnVirtualThread.class),
params.size() == 2, invoker));
parametersCount == 2, invoker));
LOGGER.debugf("Found event consumer business method %s declared on %s", method, bean);
}
}
Expand Down
52 changes: 52 additions & 0 deletions extensions/vertx/kotlin/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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">
<parent>
<artifactId>quarkus-vertx-kotlin-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-vertx-kotlin-deployment</artifactId>
<name>Quarkus - Vert.x - Kotlin - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-kotlin</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-deployment-spi</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.vertx.kotlin.deployment;

import java.util.function.BiConsumer;

import org.jboss.jandex.MethodInfo;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.processor.InvokerBuilder;
import io.quarkus.arc.processor.KotlinUtils;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.vertx.deployment.spi.EventConsumerInvokerCustomizerBuildItem;
import io.quarkus.vertx.kotlin.runtime.ApplicationCoroutineScope;
import io.quarkus.vertx.kotlin.runtime.CoroutineInvoker;

public class VertxKotlinProcessor {
private static final String KOTLIN_COROUTINE_SCOPE = "kotlinx.coroutines.CoroutineScope";

@BuildStep
void produceCoroutineScope(BuildProducer<AdditionalBeanBuildItem> additionalBean) {
if (!QuarkusClassLoader.isClassPresentAtRuntime(KOTLIN_COROUTINE_SCOPE)) {
return;
}

additionalBean.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(ApplicationCoroutineScope.class)
.setUnremovable()
.build());
}

@BuildStep
void produceInvokerCustomizerForSuspendConsumeEventMethods(
BuildProducer<EventConsumerInvokerCustomizerBuildItem> customizers) {
if (!QuarkusClassLoader.isClassPresentAtRuntime(KOTLIN_COROUTINE_SCOPE)) {
return;
}

customizers.produce(new EventConsumerInvokerCustomizerBuildItem(new BiConsumer<MethodInfo, InvokerBuilder>() {
@Override
public void accept(MethodInfo method, InvokerBuilder invokerBuilder) {
if (KotlinUtils.isKotlinSuspendMethod(method)) {
invokerBuilder.withInvocationWrapper(CoroutineInvoker.class, "inNewCoroutine");
}
}
}));
}
}
19 changes: 19 additions & 0 deletions extensions/vertx/kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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">
<parent>
<artifactId>quarkus-vertx-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-vertx-kotlin-parent</artifactId>
<name>Quarkus - Vert.x - Kotlin</name>
<packaging>pom</packaging>
<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>
Loading

0 comments on commit ea2c6a4

Please sign in to comment.