Skip to content

Commit 4824954

Browse files
committed
Move the discovery of VerticleFactory implementations to the VertxBuilder bootstrap.
Motivation: The VerticleManager discovers the list of VerticleFactory in its constructor, forcing the discovery to use Java's ServiceLoader. This should be overridable, to let advanced Vert.x users provide their list of VerticleFactory implementation when boostrapping a Vertx instance. Changes: Move the discovery of VerticleFactory to VertxBuilder. Add a list known of VerticleFactory on the VertxBuilder that is used instead of performing discovery when provided.
1 parent b5b0d58 commit 4824954

4 files changed

Lines changed: 51 additions & 13 deletions

File tree

vertx-core/src/main/java/io/vertx/core/impl/VertxBootstrapImpl.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
import io.vertx.core.impl.transports.NioTransport;
1616
import io.vertx.core.internal.VertxBootstrap;
1717
import io.vertx.core.eventbus.impl.clustered.DefaultNodeSelector;
18+
import io.vertx.core.spi.*;
1819
import io.vertx.core.spi.context.executor.EventExecutorProvider;
1920
import io.vertx.core.spi.file.FileResolver;
2021
import io.vertx.core.file.impl.FileResolverImpl;
2122
import io.vertx.core.internal.logging.Logger;
2223
import io.vertx.core.internal.logging.LoggerFactory;
2324
import io.vertx.core.json.JsonObject;
2425
import io.vertx.core.spi.transport.Transport;
25-
import io.vertx.core.spi.ExecutorServiceFactory;
26-
import io.vertx.core.spi.VertxMetricsFactory;
27-
import io.vertx.core.spi.VertxServiceProvider;
28-
import io.vertx.core.spi.VertxThreadFactory;
29-
import io.vertx.core.spi.VertxTracerFactory;
3026
import io.vertx.core.spi.cluster.ClusterManager;
3127
import io.vertx.core.eventbus.impl.clustered.NodeSelector;
3228
import io.vertx.core.spi.metrics.VertxMetrics;
@@ -60,6 +56,7 @@ public class VertxBootstrapImpl implements VertxBootstrap {
6056
private VertxMetricsFactory metricsFactory;
6157
private VertxMetrics metrics;
6258
private FileResolver fileResolver;
59+
private List<VerticleFactory> verticleFactories;
6360

6461
public VertxBootstrapImpl(JsonObject config) {
6562
this(new VertxOptions(config));
@@ -266,7 +263,7 @@ private VertxImpl instantiateVertx(ClusterManager clusterManager, NodeSelector n
266263

267264
public Vertx vertx() {
268265
VertxImpl vertx = instantiateVertx(null, null);
269-
vertx.init();
266+
vertx.init(verticleFactories);
270267
return vertx;
271268
}
272269

@@ -282,7 +279,25 @@ public Future<Vertx> clusteredVertx() {
282279
nodeSelector = new DefaultNodeSelector();
283280
}
284281
VertxImpl vertx = instantiateVertx(clusterManager, nodeSelector);
285-
return vertx.initClustered(options);
282+
return vertx.initClustered(options, verticleFactories);
283+
}
284+
285+
/**
286+
* @return the verticle factories to use
287+
*/
288+
public List<VerticleFactory> verticleFactories() {
289+
return verticleFactories;
290+
}
291+
292+
/**
293+
* Set the list of {@code VerticleFactory} to use.
294+
*
295+
* @param verticleFactories the verticle factories
296+
* @return the builder instance
297+
*/
298+
public VertxBootstrapImpl verticleFactories(List<VerticleFactory> verticleFactories) {
299+
this.verticleFactories = verticleFactories;
300+
return this;
286301
}
287302

288303
@Override
@@ -295,6 +310,7 @@ public VertxBootstrapImpl init() {
295310
initThreadFactory();
296311
initExecutorServiceFactory();
297312
initFileResolver();
313+
initVerticleFactories();
298314
return this;
299315
}
300316

@@ -352,6 +368,13 @@ private void initExecutorServiceFactory() {
352368
executorServiceFactory = ExecutorServiceFactory.INSTANCE;
353369
}
354370

371+
private void initVerticleFactories() {
372+
if (verticleFactories != null) {
373+
return;
374+
}
375+
verticleFactories = new ArrayList<>(ServiceHelper.loadFactories(VerticleFactory.class));
376+
}
377+
355378
private void checkBeforeInstantiating() {
356379
checkTracing();
357380
checkMetrics();

vertx-core/src/main/java/io/vertx/core/impl/VertxImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,22 @@ private static ThreadFactory virtualThreadFactory() {
244244
this.shadowContext = enableShadowContext;
245245
}
246246

247-
void init() {
247+
void init(List<VerticleFactory> verticleFactories) {
248248
eventBus.start(Promise.promise());
249249
if (metrics != null) {
250250
metrics.vertxCreated(this);
251251
}
252+
verticleManager.init(verticleFactories);
252253
}
253254

254-
Future<Vertx> initClustered(VertxOptions options) {
255+
Future<Vertx> initClustered(VertxOptions options, List<VerticleFactory> verticleFactories) {
255256
nodeSelector.init(clusterManager);
256257
clusterManager.registrationListener(nodeSelector);
257258
clusterManager.init(this);
258259
Promise<Void> initPromise = Promise.promise();
259260
clusterManager.join((res, err) -> {
260261
if (err == null) {
262+
verticleManager.init(verticleFactories);
261263
createHaManager(options, initPromise);
262264
} else {
263265
initPromise.fail(err);

vertx-core/src/main/java/io/vertx/core/impl/verticle/VerticleManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public VerticleManager(VertxInternal vertx, Logger log, DeploymentManager deploy
4848
this.vertx = (VertxImpl) vertx;
4949
this.deploymentManager = deploymentManager;
5050
this.log = log;
51-
loadVerticleFactories();
5251
}
5352

54-
private void loadVerticleFactories() {
55-
Collection<VerticleFactory> factories = ServiceHelper.loadFactories(VerticleFactory.class);
56-
factories.forEach(this::registerVerticleFactory);
53+
public void init(List<VerticleFactory> factories) {
54+
if (factories != null) {
55+
factories.forEach(this::registerVerticleFactory);
56+
}
5757
VerticleFactory defaultFactory = new JavaVerticleFactory();
5858
defaultFactory.init(vertx);
5959
defaultFactories.add(defaultFactory);

vertx-core/src/main/java/io/vertx/core/internal/VertxBootstrap.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ static VertxBootstrap create() {
174174
*/
175175
VertxBootstrap clusterManager(ClusterManager clusterManager);
176176

177+
/**
178+
* @return the verticle factories to use
179+
*/
180+
List<VerticleFactory> verticleFactories();
181+
182+
/**
183+
* Set the list of {@code VerticleFactory} to use.
184+
*
185+
* @param verticleFactories the verticle factories
186+
* @return the builder instance
187+
*/
188+
VertxBootstrap verticleFactories(List<VerticleFactory> verticleFactories);
189+
177190
/**
178191
*
179192
* Initialize the service providers.

0 commit comments

Comments
 (0)