Search in sources :

Example 11 with DeploymentOptions

use of io.vertx.core.DeploymentOptions in project vert.x by eclipse.

the class EventBusTestBase method testReplyFromWorker.

@Test
public void testReplyFromWorker() throws Exception {
    String expectedBody = TestUtils.randomAlphaString(20);
    startNodes(2);
    CountDownLatch latch = new CountDownLatch(1);
    vertices[0].deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
                msg.reply(expectedBody);
            }).completionHandler(ar -> {
                assertTrue(ar.succeeded());
                latch.countDown();
            });
        }
    }, new DeploymentOptions().setWorker(true));
    awaitLatch(latch);
    vertices[0].eventBus().send(ADDRESS1, "whatever", reply -> {
        assertTrue(reply.succeeded());
        assertEquals(expectedBody, reply.result().body());
        testComplete();
    });
    await();
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Verticle(io.vertx.core.Verticle) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) MessageCodec(io.vertx.core.eventbus.MessageCodec) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) CharsetUtil(io.netty.util.CharsetUtil) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) ReplyException(io.vertx.core.eventbus.ReplyException) DeploymentOptions(io.vertx.core.DeploymentOptions) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 12 with DeploymentOptions

use of io.vertx.core.DeploymentOptions in project vert.x by eclipse.

the class DatagramTest method testUseInMultithreadedWorker.

@Test
public void testUseInMultithreadedWorker() throws Exception {
    class MyVerticle extends AbstractVerticle {

        @Override
        public void start() {
            assertIllegalStateException(() -> peer1 = vertx.createDatagramSocket(new DatagramSocketOptions()));
            testComplete();
        }
    }
    MyVerticle verticle = new MyVerticle();
    vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(true).setMultiThreaded(true));
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) Test(org.junit.Test)

Example 13 with DeploymentOptions

use of io.vertx.core.DeploymentOptions in project vert.x by eclipse.

the class ContextTest method testWorkerExecuteFromIo.

@Test
public void testWorkerExecuteFromIo() throws Exception {
    AtomicReference<ContextInternal> workerContext = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            workerContext.set((ContextInternal) context);
            latch.countDown();
        }
    }, new DeploymentOptions().setWorker(true));
    awaitLatch(latch);
    workerContext.get().nettyEventLoop().execute(() -> {
        assertNull(Vertx.currentContext());
        workerContext.get().executeFromIO(() -> {
            assertSame(workerContext.get(), Vertx.currentContext());
            assertTrue(Context.isOnWorkerThread());
            testComplete();
        });
    });
    await();
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Example 14 with DeploymentOptions

use of io.vertx.core.DeploymentOptions in project vert.x by eclipse.

the class ComplexHATest method deployRandomVerticles.

protected void deployRandomVerticles(Runnable runner) {
    int toDeploy = 0;
    AtomicInteger deployCount = new AtomicInteger();
    List<Integer> numbersToDeploy = new ArrayList<>();
    for (int i = 0; i < aliveNodes.size(); i++) {
        int numToDeploy = random.nextInt(maxVerticlesPerNode + 1);
        numbersToDeploy.add(numToDeploy);
        toDeploy += numToDeploy;
    }
    int index = 0;
    for (int pos : aliveNodes) {
        Vertx v = vertices[pos];
        int numToDeploy = numbersToDeploy.get(index);
        index++;
        for (int j = 0; j < numToDeploy; j++) {
            JsonObject config = new JsonObject();
            config.put("foo", TestUtils.randomAlphaString(100));
            DeploymentOptions options = new DeploymentOptions().setHa(true).setConfig(config);
            String verticleName = "java:io.vertx.test.core.HAVerticle" + (random.nextInt(3) + 1);
            v.deployVerticle(verticleName, options, ar -> {
                assertTrue(ar.succeeded());
                deployCount.incrementAndGet();
            });
        }
    }
    int ttoDeploy = toDeploy;
    eventLoopWaitUntil(() -> ttoDeploy == deployCount.get(), () -> {
        totDeployed += ttoDeploy;
        runner.run();
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JsonObject(io.vertx.core.json.JsonObject) Vertx(io.vertx.core.Vertx)

Example 15 with DeploymentOptions

use of io.vertx.core.DeploymentOptions in project vert.x by eclipse.

the class HAManager method processFailover.

// Process the failover of a deployment
private void processFailover(JsonObject failedVerticle) {
    if (failDuringFailover) {
        throw new VertxException("Oops!");
    }
    // This method must block until the failover is complete - i.e. the verticle is successfully redeployed
    final String verticleName = failedVerticle.getString("verticle_name");
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> err = new AtomicReference<>();
    // Now deploy this verticle on this node
    ContextImpl ctx = vertx.getContext();
    if (ctx != null) {
        // We could be on main thread in which case we don't want to overwrite tccl
        ContextImpl.setContext(null);
    }
    JsonObject options = failedVerticle.getJsonObject("options");
    try {
        doDeployVerticle(verticleName, new DeploymentOptions(options), result -> {
            if (result.succeeded()) {
                log.info("Successfully redeployed verticle " + verticleName + " after failover");
            } else {
                log.error("Failed to redeploy verticle after failover", result.cause());
                err.set(result.cause());
            }
            latch.countDown();
            Throwable t = err.get();
            if (t != null) {
                throw new VertxException(t);
            }
        });
    } finally {
        if (ctx != null) {
            ContextImpl.setContext(ctx);
        }
    }
    try {
        if (!latch.await(120, TimeUnit.SECONDS)) {
            throw new VertxException("Timed out waiting for redeploy on failover");
        }
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) VertxException(io.vertx.core.VertxException) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

DeploymentOptions (io.vertx.core.DeploymentOptions)39 Test (org.junit.Test)25 CountDownLatch (java.util.concurrent.CountDownLatch)17 AbstractVerticle (io.vertx.core.AbstractVerticle)15 JsonObject (io.vertx.core.json.JsonObject)10 Context (io.vertx.core.Context)8 Vertx (io.vertx.core.Vertx)7 VertxInternal (io.vertx.core.impl.VertxInternal)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Future (io.vertx.core.Future)4 HttpClientOptions (io.vertx.core.http.HttpClientOptions)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Verticle (io.vertx.core.Verticle)3 VertxException (io.vertx.core.VertxException)3 Buffer (io.vertx.core.buffer.Buffer)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Collections (java.util.Collections)3 TimeUnit (java.util.concurrent.TimeUnit)3