use of io.vertx.core.impl.VertxBuilder in project vert.x by eclipse.
the class BareCommand method startVertx.
/**
* Starts the vert.x instance.
*
* @return the created instance of vert.x
*/
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
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);
}
options = builder.options();
options.setEventBusOptions(eventBusOptions);
beforeStartingVertx(options);
configureFromSystemProperties.set(log);
try {
configureFromSystemProperties(options, VERTX_OPTIONS_PROP_PREFIX);
if (options.getMetricsOptions() != null) {
configureFromSystemProperties(options.getMetricsOptions(), METRICS_OPTIONS_PROP_PREFIX);
}
builder.init();
} finally {
configureFromSystemProperties.set(null);
}
Vertx instance;
if (isClustered()) {
log.info("Starting clustering...");
eventBusOptions = options.getEventBusOptions();
if (!Objects.equals(eventBusOptions.getHost(), EventBusOptions.DEFAULT_CLUSTER_HOST)) {
clusterHost = eventBusOptions.getHost();
}
if (eventBusOptions.getPort() != EventBusOptions.DEFAULT_CLUSTER_PORT) {
clusterPort = eventBusOptions.getPort();
}
if (!Objects.equals(eventBusOptions.getClusterPublicHost(), EventBusOptions.DEFAULT_CLUSTER_PUBLIC_HOST)) {
clusterPublicHost = eventBusOptions.getClusterPublicHost();
}
if (eventBusOptions.getClusterPublicPort() != EventBusOptions.DEFAULT_CLUSTER_PUBLIC_PORT) {
clusterPublicPort = eventBusOptions.getClusterPublicPort();
}
eventBusOptions.setHost(clusterHost).setPort(clusterPort).setClusterPublicHost(clusterPublicHost);
if (clusterPublicPort != -1) {
eventBusOptions.setClusterPublicPort(clusterPublicPort);
}
if (getHA()) {
options.setHAEnabled(true);
if (haGroup != null) {
options.setHAGroup(haGroup);
}
if (quorum != -1) {
options.setQuorumSize(quorum);
}
}
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AsyncResult<Vertx>> result = new AtomicReference<>();
create(builder, ar -> {
result.set(ar);
latch.countDown();
});
try {
if (!latch.await(2, TimeUnit.MINUTES)) {
log.error("Timed out in starting clustered Vert.x");
return null;
}
} catch (InterruptedException e) {
log.error("Thread interrupted in startup");
Thread.currentThread().interrupt();
return null;
}
if (result.get().failed()) {
log.error("Failed to form cluster", result.get().cause());
return null;
}
instance = result.get().result();
} else {
instance = create(builder);
}
addShutdownHook(instance, log, finalAction);
afterStartingVertx(instance);
return instance;
}
use of io.vertx.core.impl.VertxBuilder in project vert.x by eclipse.
the class WriteHandlerLookupFailureTest method test.
@Test
public void test() {
Throwable cause = new Throwable();
VertxOptions options = new VertxOptions();
options.getEventBusOptions().setHost("localhost").setPort(0);
NodeSelector nodeSelector = new DefaultNodeSelector() {
@Override
public void selectForSend(Message<?> message, Promise<String> promise) {
promise.fail(cause);
}
@Override
public void selectForPublish(Message<?> message, Promise<Iterable<String>> promise) {
promise.fail("Not implemented");
}
};
new VertxBuilder(options).init().clusterNodeSelector(nodeSelector).clusteredVertx(onSuccess(node -> {
vertx = node;
MessageProducer<String> sender = vertx.eventBus().sender("foo");
sender.write("the_string", onFailure(err -> {
assertSame(cause, err);
testComplete();
}));
}));
await();
}
use of io.vertx.core.impl.VertxBuilder in project vert.x by eclipse.
the class CustomNodeSelectorTest method test.
@Test
public void test() throws Exception {
CompositeFuture startFuture = IntStream.range(0, 4).mapToObj(i -> {
VertxOptions vertxOptions = getOptions();
vertxOptions.getEventBusOptions().setClusterNodeMetadata(new JsonObject().put("rack", i % 2 == 0 ? "foo" : "bar"));
return vertxOptions;
}).map(options -> {
VertxBuilder factory = new VertxBuilder(options).init().clusterNodeSelector(new CustomNodeSelector());
Promise promise = Promise.promise();
factory.clusteredVertx(promise);
return promise.future();
}).collect(collectingAndThen(toList(), CompositeFuture::all));
CountDownLatch startLatch = new CountDownLatch(1);
startFuture.onComplete(onSuccess(cf -> startLatch.countDown()));
awaitLatch(startLatch);
vertices = startFuture.list();
ConcurrentMap<Integer, Set<String>> received = new ConcurrentHashMap<>();
CountDownLatch latch = new CountDownLatch(8);
CompositeFuture cf = IntStream.range(0, 4).mapToObj(i -> vertices.get(i).eventBus().<String>consumer("test", msg -> {
received.merge(i, Collections.singleton(msg.body()), (s1, s2) -> Stream.concat(s1.stream(), s2.stream()).collect(toSet()));
latch.countDown();
})).map(consumer -> {
Promise promise = Promise.promise();
consumer.completionHandler(promise);
return promise.future();
}).collect(collectingAndThen(toList(), CompositeFuture::all));
Map<Integer, Set<String>> expected = new HashMap<>();
cf.onComplete(onSuccess(v -> {
for (int i = 0; i < 4; i++) {
String s = String.valueOf((char) ('a' + i));
vertices.get(i).eventBus().publish("test", s);
expected.merge(i, Collections.singleton(s), (s1, s2) -> Stream.concat(s1.stream(), s2.stream()).collect(toSet()));
expected.merge((i + 2) % 4, Collections.singleton(s), (s1, s2) -> Stream.concat(s1.stream(), s2.stream()).collect(toSet()));
}
}));
awaitLatch(latch);
assertEquals(expected, received);
}
use of io.vertx.core.impl.VertxBuilder in project vert.x by eclipse.
the class VertxStartFailureTest method failStart.
private Throwable failStart(VertxOptions options) throws Exception {
List<EventLoopGroup> loops = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(1);
Transport transport = new Transport() {
@Override
public EventLoopGroup eventLoopGroup(int type, int nThreads, ThreadFactory threadFactory, int ioRatio) {
EventLoopGroup eventLoop = super.eventLoopGroup(type, nThreads, threadFactory, ioRatio);
loops.add(eventLoop);
return eventLoop;
}
};
AtomicReference<AsyncResult<Vertx>> resultRef = new AtomicReference<>();
new VertxBuilder(options).init().transport(transport).clusteredVertx(ar -> {
resultRef.set(ar);
latch.countDown();
});
awaitLatch(latch);
assertFalse(resultRef.get() == null);
assertTrue(resultRef.get().failed());
loops.forEach(loop -> {
waitUntil(loop::isShutdown);
});
return resultRef.get().cause();
}
use of io.vertx.core.impl.VertxBuilder in project vert.x by eclipse.
the class MessageQueueOnWorkerThreadTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
CustomNodeSelector selector = new CustomNodeSelector();
VertxBuilder factory = new VertxBuilder().init().clusterNodeSelector(selector);
Promise<Vertx> promise = Promise.promise();
factory.clusteredVertx(promise);
vertx = promise.future().toCompletionStage().toCompletableFuture().get();
}
Aggregations