Skip to content

Commit 4a756df

Browse files
authored
Merge pull request #6061 from eclipse-vertx/service-loading-controrl-5.0
Service loading control
2 parents 242107b + 4824954 commit 4a756df

5 files changed

Lines changed: 117 additions & 30 deletions

File tree

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

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@
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;
3329
import io.vertx.core.spi.tracing.VertxTracer;
3430

31+
import java.util.ArrayList;
3532
import java.util.Collection;
3633
import java.util.List;
3734

@@ -51,13 +48,15 @@ public class VertxBootstrapImpl implements VertxBootstrap {
5148
private EventExecutorProvider eventExecutorProvider;
5249
private ClusterManager clusterManager;
5350
private NodeSelector clusterNodeSelector;
51+
private List<VertxServiceProvider> serviceProviders;
5452
private VertxTracerFactory tracerFactory;
5553
private VertxTracer tracer;
5654
private VertxThreadFactory threadFactory;
5755
private ExecutorServiceFactory executorServiceFactory;
5856
private VertxMetricsFactory metricsFactory;
5957
private VertxMetrics metrics;
6058
private FileResolver fileResolver;
59+
private List<VerticleFactory> verticleFactories;
6160

6261
public VertxBootstrapImpl(JsonObject config) {
6362
this(new VertxOptions(config));
@@ -85,7 +84,7 @@ public VertxBootstrap options(VertxOptions options) {
8584
}
8685

8786
@Override
88-
public VertxBootstrap enableShadowContext(boolean option) {
87+
public VertxBootstrapImpl enableShadowContext(boolean option) {
8988
this.enableShadowContext = option;
9089
return this;
9190
}
@@ -98,7 +97,7 @@ public JsonObject config() {
9897
}
9998

10099
@Override
101-
public VertxBootstrap eventExecutorProvider(EventExecutorProvider provider) {
100+
public VertxBootstrapImpl eventExecutorProvider(EventExecutorProvider provider) {
102101
this.eventExecutorProvider = provider;
103102
return this;
104103
}
@@ -163,6 +162,25 @@ public VertxBootstrapImpl tracerFactory(VertxTracerFactory factory) {
163162
return this;
164163
}
165164

165+
/**
166+
* @return the list of service providers to use
167+
*/
168+
public List<VertxServiceProvider> serviceProviders() {
169+
return serviceProviders;
170+
}
171+
172+
/**
173+
* Set the list of service providers to use, when the list is {@code null}, Java's service loader
174+
* mechanism will be used to discover service providers.
175+
*
176+
* @param providers the service providers
177+
* @return this builder instance
178+
*/
179+
public VertxBootstrapImpl serviceProviders(List<VertxServiceProvider> providers) {
180+
this.serviceProviders = providers;
181+
return this;
182+
}
183+
166184
public VertxTracer tracer() {
167185
return tracer;
168186
}
@@ -245,7 +263,7 @@ private VertxImpl instantiateVertx(ClusterManager clusterManager, NodeSelector n
245263

246264
public Vertx vertx() {
247265
VertxImpl vertx = instantiateVertx(null, null);
248-
vertx.init();
266+
vertx.init(verticleFactories);
249267
return vertx;
250268
}
251269

@@ -261,27 +279,48 @@ public Future<Vertx> clusteredVertx() {
261279
nodeSelector = new DefaultNodeSelector();
262280
}
263281
VertxImpl vertx = instantiateVertx(clusterManager, nodeSelector);
264-
return vertx.initClustered(options);
282+
return vertx.initClustered(options, verticleFactories);
265283
}
266284

267285
/**
268-
* Initialize the service providers.
269-
* @return this builder instance
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
270297
*/
298+
public VertxBootstrapImpl verticleFactories(List<VerticleFactory> verticleFactories) {
299+
this.verticleFactories = verticleFactories;
300+
return this;
301+
}
302+
303+
@Override
271304
public VertxBootstrapImpl init() {
305+
List<VertxServiceProvider> providers = new ArrayList<>();
272306
initTransport();
273307
initMetrics();
274308
initTracing();
275-
List<VertxServiceProvider> providers = ServiceHelper.loadFactories(VertxServiceProvider.class);
276309
initProviders(providers);
277310
initThreadFactory();
278311
initExecutorServiceFactory();
279312
initFileResolver();
313+
initVerticleFactories();
280314
return this;
281315
}
282316

283-
private void initProviders(Collection<VertxServiceProvider> providers) {
284-
for (VertxServiceProvider provider : providers) {
317+
private void initProviders(Collection<VertxServiceProvider> toInit) {
318+
List<VertxServiceProvider> serviceProviders = this.serviceProviders;
319+
if (serviceProviders == null) {
320+
serviceProviders = new ArrayList<>(ServiceHelper.loadFactories(VertxServiceProvider.class));
321+
}
322+
toInit.addAll(serviceProviders);
323+
for (VertxServiceProvider provider : toInit) {
285324
if (provider instanceof VertxMetricsFactory && (options.getMetricsOptions() == null || !options.getMetricsOptions().isEnabled())) {
286325
continue;
287326
} else if (provider instanceof VertxTracerFactory && (options.getTracingOptions() == null)) {
@@ -329,6 +368,13 @@ private void initExecutorServiceFactory() {
329368
executorServiceFactory = ExecutorServiceFactory.INSTANCE;
330369
}
331370

371+
private void initVerticleFactories() {
372+
if (verticleFactories != null) {
373+
return;
374+
}
375+
verticleFactories = new ArrayList<>(ServiceHelper.loadFactories(VerticleFactory.class));
376+
}
377+
332378
private void checkBeforeInstantiating() {
333379
checkTracing();
334380
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: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
import io.vertx.core.Vertx;
1515
import io.vertx.core.VertxOptions;
1616
import io.vertx.core.impl.VertxBootstrapImpl;
17-
import io.vertx.core.spi.ExecutorServiceFactory;
18-
import io.vertx.core.spi.VertxMetricsFactory;
19-
import io.vertx.core.spi.VertxThreadFactory;
20-
import io.vertx.core.spi.VertxTracerFactory;
17+
import io.vertx.core.spi.*;
2118
import io.vertx.core.spi.cluster.ClusterManager;
2219
import io.vertx.core.spi.context.executor.EventExecutorProvider;
2320
import io.vertx.core.spi.file.FileResolver;
2421
import io.vertx.core.spi.transport.Transport;
2522

23+
import java.util.List;
24+
2625
/**
2726
* Vertx bootstrap for creating vertx instances with SPI overrides.
2827
*
@@ -72,8 +71,22 @@ static VertxBootstrap create() {
7271
EventExecutorProvider eventExecutorProvider();
7372

7473
/**
75-
* @return the {@code FileResolver} instance to use
74+
* @return the list of service providers to use
75+
*/
76+
List<VertxServiceProvider> serviceProviders();
77+
78+
/**
79+
* Set the list of service providers to use, when the list is {@code null}, Java's service loader
80+
* mechanism will be used to discover service providers.
81+
*
82+
* @param providers the service providers
83+
* @return this builder instance
7684
*/
85+
VertxBootstrap serviceProviders(List<VertxServiceProvider> providers);
86+
87+
/**
88+
* @return the {@code FileResolver} instance to use
89+
*/
7790
FileResolver fileResolver();
7891

7992
/**
@@ -146,7 +159,7 @@ static VertxBootstrap create() {
146159
* @param transport the transport
147160
* @return this builder instance
148161
*/
149-
VertxBootstrapImpl transport(Transport transport);
162+
VertxBootstrap transport(Transport transport);
150163

151164
/**
152165
* @return the cluster manager to use
@@ -162,6 +175,20 @@ static VertxBootstrap create() {
162175
VertxBootstrap clusterManager(ClusterManager clusterManager);
163176

164177
/**
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+
190+
/**
191+
*
165192
* Initialize the service providers.
166193
*
167194
* @return this builder instance

vertx-core/src/test/java/io/vertx/tests/vertx/VertxBootstrapTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
import io.vertx.core.internal.VertxInternal;
1818
import io.vertx.core.metrics.MetricsOptions;
1919
import io.vertx.core.impl.transports.NioTransport;
20+
import io.vertx.core.spi.*;
2021
import io.vertx.core.spi.transport.Transport;
21-
import io.vertx.core.spi.ExecutorServiceFactory;
22-
import io.vertx.core.spi.VertxMetricsFactory;
23-
import io.vertx.core.spi.VertxThreadFactory;
24-
import io.vertx.core.spi.VertxTracerFactory;
2522
import io.vertx.core.spi.cluster.ClusterManager;
2623
import io.vertx.core.tracing.TracingOptions;
2724
import io.vertx.test.fakecluster.FakeClusterManager;
@@ -38,11 +35,13 @@
3835
import java.net.URL;
3936
import java.net.URLClassLoader;
4037
import java.nio.file.Files;
38+
import java.util.Collections;
4139
import java.util.concurrent.CompletableFuture;
4240
import java.util.concurrent.ExecutorService;
4341
import java.util.concurrent.Executors;
4442
import java.util.concurrent.ThreadFactory;
4543
import java.util.concurrent.TimeUnit;
44+
import java.util.concurrent.atomic.AtomicInteger;
4645

4746
import static org.junit.Assert.*;
4847

@@ -181,6 +180,19 @@ public VertxThread newVertxThread(Runnable target, String name, boolean worker,
181180
.close().await();
182181
}
183182

183+
@Test
184+
public void testExplicitServiceProviders() {
185+
AtomicInteger initialized = new AtomicInteger();
186+
VertxServiceProvider provider = builder -> initialized.incrementAndGet();
187+
VertxBootstrap factory = VertxBootstrap.create();
188+
Vertx vertx = factory
189+
.serviceProviders(Collections.singletonList(provider))
190+
.init()
191+
.vertx();
192+
vertx.close();
193+
assertEquals(1, initialized.get());
194+
}
195+
184196
private class CustomExecutorServiceFactory implements ExecutorServiceFactory {
185197

186198
@Override

0 commit comments

Comments
 (0)