Skip to content

Commit

Permalink
Add support for VertxOptionsCustomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier committed Jul 16, 2022
1 parent fee39a6 commit 5e4d11b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
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,10 +32,13 @@

import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.FastThreadLocal;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableInstance;
import io.quarkus.runtime.IOThreadDetector;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.vertx.VertxOptionsCustomizer;
import io.quarkus.vertx.core.runtime.config.AddressResolverConfiguration;
import io.quarkus.vertx.core.runtime.config.ClusterConfiguration;
import io.quarkus.vertx.core.runtime.config.EventBusConfiguration;
Expand Down Expand Up @@ -597,6 +600,16 @@ static class VertxOptionsCustomizer {

VertxOptionsCustomizer(List<Consumer<VertxOptions>> customizers) {
this.customizers = customizers;
if (Arc.container() != null) {
// Append the user customizer if we are in a managed environment
InjectableInstance<io.quarkus.vertx.VertxOptionsCustomizer> instances = Arc.container()
.select(io.quarkus.vertx.VertxOptionsCustomizer.class);
if (!instances.isUnsatisfied()) {
for (io.quarkus.vertx.VertxOptionsCustomizer customizer : instances) {
customizers.add(customizer);
}
}
}
}

VertxOptions customize(VertxOptions options) {
Expand Down

0 comments on commit 5e4d11b

Please sign in to comment.