Skip to content
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

Add support for VertxOptionsCustomizer #26759

Merged
merged 1 commit into from
Jul 19, 2022
Merged
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
22 changes: 22 additions & 0 deletions docs/src/main/asciidoc/vertx-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1039,3 +1039,25 @@ java.lang.IllegalStateException: Failed to create cache dir
----

Assuming `/tmp/` is writable this can be fixed by setting the `vertx.cacheDirBase` property to point to a directory in `/tmp/` for instance in OpenShift by creating an environment variable `JAVA_OPTS` with the value `-Dvertx.cacheDirBase=/tmp/vertx`.

== Customizing the Vert.x configuration

The configuration of the managed Vert.x instance can be provided using the `application.properties` file, but also using _special beans_.
CDI beans exposing the `io.quarkus.vertx.VertxOptionsCustomizer` interface can be used to customize the Vert.x configuration.
For example, the following customizer change the `tmp` base directory:

[source, java]
----
@ApplicationScoped
public class MyCustomizer implements VertxOptionsCustomizer {

@Override
public void accept(VertxOptions options) {
options.setFileSystemOptions(new FileSystemOptions().setFileCacheDir("target"));
}
}
----

The _customizer_ beans received the `VertxOptions` (coming from the application configuration), and can modify them.


Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.objectweb.asm.Type;

import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -50,6 +51,7 @@
import io.quarkus.deployment.logging.LogCleanupFilterBuildItem;
import io.quarkus.gizmo.Gizmo;
import io.quarkus.netty.deployment.EventLoopSupplierBuildItem;
import io.quarkus.vertx.VertxOptionsCustomizer;
import io.quarkus.vertx.core.runtime.VertxCoreRecorder;
import io.quarkus.vertx.core.runtime.VertxLocalsHelper;
import io.quarkus.vertx.core.runtime.VertxLogDelegateFactory;
Expand Down Expand Up @@ -247,6 +249,11 @@ void registerVerticleClasses(CombinedIndexBuildItem indexBuildItem,
}
}

@BuildStep
void doNotRemoveVertxOptionsCustomizers(BuildProducer<UnremovableBeanBuildItem> unremovable) {
unremovable.produce(UnremovableBeanBuildItem.beanTypes(VertxOptionsCustomizer.class));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
ThreadFactoryBuildItem createVertxThreadFactory(VertxCoreRecorder recorder, LaunchModeBuildItem launchMode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.vertx.customizers;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.assertj.core.api.Assertions;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.VertxOptionsCustomizer;
import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystemOptions;
import io.vertx.mutiny.core.Vertx;

public class VertxOptionsCustomizerTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap
.create(JavaArchive.class).addClasses(MyCustomizer.class));

@Inject
Vertx vertx;

@Inject
MyCustomizer customizer;

@Test
public void testCustomizer() {
Assertions.assertThat(customizer.wasInvoked()).isTrue();
String test = vertx.fileSystem().createTempDirectoryAndAwait("test");
Assertions.assertThat(test).contains("target", "test");
}

@ApplicationScoped
public static class MyCustomizer implements VertxOptionsCustomizer {

volatile boolean invoked;

@Override
public void accept(VertxOptions options) {
invoked = true;
options.setFileSystemOptions(new FileSystemOptions().setFileCacheDir("target"));
}

public boolean wasInvoked() {
return invoked;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.vertx;

import java.util.function.Consumer;

import io.vertx.core.VertxOptions;

/**
* Allows customizing the {@link VertxOptions} used to create the managed {@link io.vertx.core.Vertx} instance.
* <p>
* Beans exposing this interface receive the {@link VertxOptions} computed from the application configuration, and
* extensions customizing the options.
*/
public interface VertxOptionsCustomizer extends Consumer<VertxOptions> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.FastThreadLocal;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.runtime.IOThreadDetector;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.ShutdownContext;
Expand Down Expand Up @@ -597,6 +599,13 @@ static class VertxOptionsCustomizer {

VertxOptionsCustomizer(List<Consumer<VertxOptions>> customizers) {
this.customizers = customizers;
if (Arc.container() != null) {
List<InstanceHandle<io.quarkus.vertx.VertxOptionsCustomizer>> instances = Arc.container()
.listAll(io.quarkus.vertx.VertxOptionsCustomizer.class);
for (InstanceHandle<io.quarkus.vertx.VertxOptionsCustomizer> customizer : instances) {
customizers.add(customizer.get());
}
}
}

VertxOptions customize(VertxOptions options) {
Expand Down