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

Possibility to use io.vertx.core.VertxBuilder inside io.vertx.core.impl.launcher.VertxLifecycleHooks #5388

Closed
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
66 changes: 2 additions & 64 deletions src/main/java/io/vertx/core/Vertx.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.file.FileSystem;
import io.vertx.core.http.*;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxBuilder.VertxBuilderBridge;
import io.vertx.core.impl.VertxImpl;
import io.vertx.core.metrics.Measured;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.shareddata.SharedData;
import io.vertx.core.spi.VerticleFactory;
import io.vertx.core.spi.VertxMetricsFactory;
import io.vertx.core.spi.VertxTracerFactory;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.tracing.TracingOptions;

import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -72,64 +67,7 @@
public interface Vertx extends Measured {

static VertxBuilder builder() {
return new io.vertx.core.VertxBuilder() {
VertxOptions options;
VertxTracerFactory tracerFactory;
VertxMetricsFactory metricsFactory;
ClusterManager clusterManager;
@Override
public VertxBuilder with(VertxOptions options) {
this.options = options;
return this;
}
@Override
public VertxBuilder withTracer(VertxTracerFactory factory) {
this.tracerFactory = factory;
return this;
}
@Override
public VertxBuilder withClusterManager(ClusterManager clusterManager) {
this.clusterManager = clusterManager;
return this;
}
@Override
public VertxBuilder withMetrics(VertxMetricsFactory factory) {
this.metricsFactory = factory;
return this;
}
private io.vertx.core.impl.VertxBuilder internalBuilder() {
VertxOptions opts = options != null ? options : new VertxOptions();
if (clusterManager != null) {
opts.setClusterManager(clusterManager);
}
if (metricsFactory != null) {
MetricsOptions metricsOptions = opts.getMetricsOptions();
if (metricsOptions != null) {
metricsOptions.setFactory(metricsFactory);
} else {
opts.setMetricsOptions(new MetricsOptions().setFactory(metricsFactory));
}
metricsOptions.setEnabled(true);
}
if (tracerFactory != null) {
TracingOptions tracingOptions = opts.getTracingOptions();
if (tracingOptions != null) {
tracingOptions.setFactory(tracerFactory);
} else {
opts.setTracingOptions(new TracingOptions().setFactory(tracerFactory));
}
}
return new io.vertx.core.impl.VertxBuilder(opts).init();
}
@Override
public Vertx build() {
return internalBuilder().vertx();
}
@Override
public Future<Vertx> buildClustered() {
return Future.future(p -> internalBuilder().clusteredVertx(p));
}
};
return new VertxBuilderBridge();
}

/**
Expand Down
94 changes: 84 additions & 10 deletions src/main/java/io/vertx/core/impl/VertxBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@

package io.vertx.core.impl;

import io.vertx.core.*;
import io.vertx.core.impl.transports.EpollTransport;
import io.vertx.core.impl.transports.JDKTransport;
import io.vertx.core.impl.transports.KQueueTransport;
import io.vertx.core.spi.file.FileResolver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.ServiceHelper;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.file.impl.FileResolverImpl;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.impl.transports.EpollTransport;
import io.vertx.core.impl.transports.JDKTransport;
import io.vertx.core.impl.transports.KQueueTransport;
import io.vertx.core.json.JsonObject;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.spi.transport.Transport;
import io.vertx.core.spi.ExecutorServiceFactory;
import io.vertx.core.spi.VertxMetricsFactory;
import io.vertx.core.spi.VertxServiceProvider;
Expand All @@ -30,14 +37,12 @@
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.core.spi.cluster.NodeSelector;
import io.vertx.core.spi.cluster.impl.DefaultNodeSelector;
import io.vertx.core.spi.file.FileResolver;
import io.vertx.core.spi.metrics.VertxMetrics;
import io.vertx.core.spi.tracing.VertxTracer;
import io.vertx.core.spi.transport.Transport;
import io.vertx.core.tracing.TracingOptions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
* Vertx builder for creating vertx instances with SPI overrides.
*
Expand Down Expand Up @@ -421,4 +426,73 @@ static Transport findTransport(boolean preferNative) {
return JDKTransport.INSTANCE;
}
}

public static class VertxBuilderBridge implements io.vertx.core.VertxBuilder {
VertxOptions options;
VertxTracerFactory tracerFactory;
VertxMetricsFactory metricsFactory;
ClusterManager clusterManager;

@Override
public io.vertx.core.VertxBuilder with(VertxOptions options) {
this.options = options;
return this;
}

@Override
public io.vertx.core.VertxBuilder withTracer(VertxTracerFactory factory) {
this.tracerFactory = factory;
return this;
}

@Override
public io.vertx.core.VertxBuilder withClusterManager(ClusterManager clusterManager) {
this.clusterManager = clusterManager;
return this;
}

@Override
public io.vertx.core.VertxBuilder withMetrics(VertxMetricsFactory factory) {
this.metricsFactory = factory;
return this;
}

@SuppressWarnings("deprecation")
public io.vertx.core.impl.VertxBuilder internalBuilder() {
VertxOptions opts = options != null ? options : new VertxOptions();
if (clusterManager != null) {
opts.setClusterManager(clusterManager);
}
if (metricsFactory != null) {
MetricsOptions metricsOptions = opts.getMetricsOptions();
if (metricsOptions != null) {
metricsOptions.setFactory(metricsFactory);
} else {
opts.setMetricsOptions(new MetricsOptions().setFactory(metricsFactory));
}
metricsOptions.setEnabled(true);
}
if (tracerFactory != null) {
TracingOptions tracingOptions = opts.getTracingOptions();
if (tracingOptions != null) {
tracingOptions.setFactory(tracerFactory);
} else {
opts.setTracingOptions(new TracingOptions().setFactory(tracerFactory));
}
}

return new io.vertx.core.impl.VertxBuilder(opts);
}

@Override
public Vertx build() {
return internalBuilder().init().vertx();
}

@Override
public Future<Vertx> buildClustered() {
return Future.future(p -> internalBuilder().init().clusteredVertx(p));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.impl.VertxBuilder;
import io.vertx.core.impl.VertxBuilder.VertxBuilderBridge;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.tracing.VertxTracer;

Expand All @@ -40,9 +41,13 @@ public interface VertxLifecycleHooks {
*
* @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);
final VertxBuilderBridge vertxBuilder = new VertxBuilder.VertxBuilderBridge();
if (config != null)
vertxBuilder.with(new VertxOptions(config));

return vertxBuilder.internalBuilder();
}

/**
Expand Down