Search in sources :

Example 1 with VertxBuilder

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;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) EventBusOptions(io.vertx.core.eventbus.EventBusOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) VertxBuilder(io.vertx.core.impl.VertxBuilder) Vertx(io.vertx.core.Vertx) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult)

Example 2 with VertxBuilder

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();
}
Also used : NodeSelector(io.vertx.core.spi.cluster.NodeSelector) DefaultNodeSelector(io.vertx.core.spi.cluster.impl.DefaultNodeSelector) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) VertxBuilder(io.vertx.core.impl.VertxBuilder) Test(org.junit.Test) VertxTestBase(io.vertx.test.core.VertxTestBase) Collections(java.util.Collections) Promise(io.vertx.core.Promise) DefaultNodeSelector(io.vertx.core.spi.cluster.impl.DefaultNodeSelector) NodeSelector(io.vertx.core.spi.cluster.NodeSelector) DefaultNodeSelector(io.vertx.core.spi.cluster.impl.DefaultNodeSelector) VertxBuilder(io.vertx.core.impl.VertxBuilder) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 3 with VertxBuilder

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);
}
Also used : CompositeFuture(io.vertx.core.CompositeFuture) IntStream(java.util.stream.IntStream) java.util(java.util) NodeSelector(io.vertx.core.spi.cluster.NodeSelector) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxOptions(io.vertx.core.VertxOptions) VertxBuilder(io.vertx.core.impl.VertxBuilder) Test(org.junit.Test) VertxTestBase(io.vertx.test.core.VertxTestBase) Collectors(java.util.stream.Collectors) ConcurrentMap(java.util.concurrent.ConcurrentMap) CompositeFuture(io.vertx.core.CompositeFuture) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) RegistrationUpdateEvent(io.vertx.core.spi.cluster.RegistrationUpdateEvent) JsonObject(io.vertx.core.json.JsonObject) NodeInfo(io.vertx.core.spi.cluster.NodeInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(io.vertx.core.Promise) VertxBuilder(io.vertx.core.impl.VertxBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 4 with VertxBuilder

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();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Transport(io.vertx.core.net.impl.transport.Transport) VertxBuilder(io.vertx.core.impl.VertxBuilder)

Example 5 with VertxBuilder

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();
}
Also used : VertxBuilder(io.vertx.core.impl.VertxBuilder) Vertx(io.vertx.core.Vertx)

Aggregations

VertxBuilder (io.vertx.core.impl.VertxBuilder)6 Vertx (io.vertx.core.Vertx)4 VertxOptions (io.vertx.core.VertxOptions)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Promise (io.vertx.core.Promise)2 JsonObject (io.vertx.core.json.JsonObject)2 NodeSelector (io.vertx.core.spi.cluster.NodeSelector)2 VertxTestBase (io.vertx.test.core.VertxTestBase)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 EventLoopGroup (io.netty.channel.EventLoopGroup)1 AsyncResult (io.vertx.core.AsyncResult)1 CompositeFuture (io.vertx.core.CompositeFuture)1 EventBusOptions (io.vertx.core.eventbus.EventBusOptions)1 VertxThread (io.vertx.core.impl.VertxThread)1 Transport (io.vertx.core.net.impl.transport.Transport)1 VertxThreadFactory (io.vertx.core.spi.VertxThreadFactory)1 ClusterManager (io.vertx.core.spi.cluster.ClusterManager)1 NodeInfo (io.vertx.core.spi.cluster.NodeInfo)1 RegistrationUpdateEvent (io.vertx.core.spi.cluster.RegistrationUpdateEvent)1