Skip to content

Commit

Permalink
Hook for VertxBuilder customization (#5288)
Browse files Browse the repository at this point in the history
Given methods like TracingOptions#setTracer have been deprecated, a new hook can help users configure the Vert.x instance.

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Sep 16, 2024
1 parent 4d0685e commit e369345
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/main/java/io/vertx/core/impl/launcher/VertxLifecycleHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.impl.VertxBuilder;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.tracing.VertxTracer;

/**
* Interface that let sub-classes of launcher to be notified on different events.
Expand All @@ -31,6 +33,18 @@ public interface VertxLifecycleHooks {
*/
void afterConfigParsed(JsonObject config);

/**
* Default implementation for the {@link VertxBuilder} creation.
* <p>
* This can be overridden in order to customize, for example, the tracer, with {@link VertxBuilder#tracer(VertxTracer)}.
*
* @param config the Vert.x options to use, in JSON format
* @return the Vert.x builder instance
*/
default VertxBuilder createVertxBuilder(JsonObject config) {
return config == null ? new VertxBuilder() : new VertxBuilder(config);
}

/**
* Hook for sub-classes of the {@link io.vertx.core.Launcher} class before the vertx instance is started. Options
* can still be updated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,8 @@ public void run(Runnable action) {
protected Vertx startVertx() {
JsonObject optionsJson = getJsonFromFileOrString(vertxOptions, "options");

EventBusOptions eventBusOptions;
VertxBuilder builder;
if (optionsJson == null) {
eventBusOptions = getEventBusOptions();
builder = new VertxBuilder();
} else {
eventBusOptions = getEventBusOptions(optionsJson.getJsonObject("eventBusOptions"));
builder = new VertxBuilder(optionsJson);
}
EventBusOptions eventBusOptions = optionsJson == null ? getEventBusOptions() : getEventBusOptions(optionsJson.getJsonObject("eventBusOptions"));
VertxBuilder builder = createVertxBuilder(optionsJson);
options = builder.options();
options.setEventBusOptions(eventBusOptions);

Expand Down Expand Up @@ -338,6 +331,14 @@ protected void beforeStartingVertx(VertxOptions options) {
}
}

protected VertxBuilder createVertxBuilder(JsonObject config) {
Object main = executionContext.main();
if (main instanceof VertxLifecycleHooks) {
return ((VertxLifecycleHooks) main).createVertxBuilder(config);
}
return config == null ? new VertxBuilder() : new VertxBuilder(config);
}

/**
* @return the event bus options.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.vertx.core.VertxOptions;
import io.vertx.core.cli.CLIException;
import io.vertx.core.cli.annotations.Name;
import io.vertx.core.impl.VertxBuilder;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.launcher.commands.CommandTestBase;
import io.vertx.core.impl.launcher.commands.HttpTestVerticle;
Expand All @@ -38,7 +39,7 @@
*/
public class LauncherExtensibilityTest extends CommandTestBase {

private static AtomicReference<Boolean> spy = new AtomicReference<>();
private static final AtomicReference<Boolean> spy = new AtomicReference<>();

private Vertx vertx;

Expand Down Expand Up @@ -218,8 +219,8 @@ public void afterStartingVertx(Vertx vertx) {
}

@Override
public void beforeStartingVertx(VertxOptions options) {
options.setClusterManager(clusterManager);
public VertxBuilder createVertxBuilder(JsonObject config) {
return super.createVertxBuilder(config).clusterManager(clusterManager);
}
};

Expand Down

0 comments on commit e369345

Please sign in to comment.