Search in sources :

Example 1 with Message

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

the class NetTest method setHandlers.

void setHandlers(NetSocket sock) {
    Handler<Message<Buffer>> resumeHandler = m -> sock.resume();
    MessageConsumer reg = vertx.eventBus().<Buffer>consumer("client_resume").handler(resumeHandler);
    sock.closeHandler(v -> reg.unregister());
}
Also used : java.util(java.util) io.vertx.core(io.vertx.core) io.vertx.core.impl(io.vertx.core.impl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cert(io.vertx.test.core.tls.Cert) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) LoggerFactory(io.vertx.core.logging.LoggerFactory) InetAddress(java.net.InetAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) OutputStreamWriter(java.io.OutputStreamWriter) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) Logger(io.vertx.core.logging.Logger) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) BufferedWriter(java.io.BufferedWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Message(io.vertx.core.eventbus.Message) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) X509Certificate(javax.security.cert.X509Certificate) io.vertx.core.net(io.vertx.core.net) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) ClientAuth(io.vertx.core.http.ClientAuth) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) SocketAddressImpl(io.vertx.core.net.impl.SocketAddressImpl) TemporaryFolder(org.junit.rules.TemporaryFolder) Trust(io.vertx.test.core.tls.Trust) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Message(io.vertx.core.eventbus.Message)

Example 2 with Message

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

the class DeploymentTest method testIsolationGroup.

// TODO
// Multi-threaded workers
private void testIsolationGroup(String group1, String group2, int count1, int count2, List<String> isolatedClasses, String verticleID) throws Exception {
    Map<String, Integer> countMap = new ConcurrentHashMap<>();
    vertx.eventBus().<JsonObject>consumer("testcounts").handler((Message<JsonObject> msg) -> {
        countMap.put(msg.body().getString("deploymentID"), msg.body().getInteger("count"));
    });
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<String> deploymentID1 = new AtomicReference<>();
    AtomicReference<String> deploymentID2 = new AtomicReference<>();
    vertx.deployVerticle(verticleID, new DeploymentOptions().setIsolationGroup(group1).setIsolatedClasses(isolatedClasses), ar -> {
        assertTrue(ar.succeeded());
        deploymentID1.set(ar.result());
        assertEquals(0, TestVerticle.instanceCount.get());
        vertx.deployVerticle(verticleID, new DeploymentOptions().setIsolationGroup(group2).setIsolatedClasses(isolatedClasses), ar2 -> {
            assertTrue(ar2.succeeded());
            deploymentID2.set(ar2.result());
            assertEquals(0, TestVerticle.instanceCount.get());
            latch.countDown();
        });
    });
    awaitLatch(latch);
    // Wait until two entries in the map
    waitUntil(() -> countMap.size() == 2);
    assertEquals(count1, countMap.get(deploymentID1.get()).intValue());
    assertEquals(count2, countMap.get(deploymentID2.get()).intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Message(io.vertx.core.eventbus.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with Message

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

the class JSONEventBusTest method testChangesNotVisibleObject3.

@Test
public void testChangesNotVisibleObject3() {
    Map<String, Object> map = new HashMap<>();
    final JsonObject obj = new JsonObject(map);
    eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> {
        vertx.setTimer(1000, id -> {
            assertFalse(msg.body().containsKey("b"));
            testComplete();
        });
    });
    eb.send("foo", obj);
    map.put("b", "uhqdihuqwd");
    await();
}
Also used : Message(io.vertx.core.eventbus.Message) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 4 with Message

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

the class JSONEventBusTest method testChangesNotVisibleObject1.

@Test
public void testChangesNotVisibleObject1() {
    JsonObject obj = new JsonObject();
    eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> {
        assertFalse(msg.body().containsKey("b"));
        testComplete();
    });
    eb.send("foo", obj);
    obj.put("b", "blurrgg");
    await();
}
Also used : Message(io.vertx.core.eventbus.Message) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 5 with Message

use of io.vertx.core.eventbus.Message in project vertx-examples by vert-x3.

the class Combine method start.

@Suspendable
@Override
public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    // Create a couple of consumers on different addresses
    // The adaptor allows handler to be used as a Channel
    HandlerReceiverAdaptor<Message<String>> adaptor1 = streamAdaptor();
    eb.<String>consumer(ADDRESS1).handler(adaptor1);
    HandlerReceiverAdaptor<Message<String>> adaptor2 = streamAdaptor();
    eb.<String>consumer(ADDRESS2).handler(adaptor2);
    // Set up a periodic timer to send messages to these addresses
    vertx.setPeriodic(500, tid -> {
        eb.send(ADDRESS1, "wibble");
        eb.send(ADDRESS2, "flibble");
    });
    ReceivePort<Message<String>> channel1 = adaptor1.receivePort();
    ReceivePort<Message<String>> channel2 = adaptor2.receivePort();
    // Combine them into a single channel
    // Not sure how to avoid this ugly cast with Quasar
    Mix<Message<String>> mix = (Mix<Message<String>>) Channels.mix(channel1, channel2);
    // Take the first ten
    for (int i = 0; i < 10; i++) {
        Message<String> msg = mix.receive();
        System.out.println("got message: " + msg.body());
    }
    System.out.println("done");
}
Also used : Message(io.vertx.core.eventbus.Message) EventBus(io.vertx.core.eventbus.EventBus) Mix(co.paralleluniverse.strands.channels.Mix) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

Message (io.vertx.core.eventbus.Message)53 JsonObject (io.vertx.core.json.JsonObject)37 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)19 Vertx (io.vertx.core.Vertx)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 Before (org.junit.Before)17 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Handler (io.vertx.core.Handler)15 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)14 After (org.junit.After)14 RunWith (org.junit.runner.RunWith)14 NetServerOptions (io.vertx.core.net.NetServerOptions)12 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)12 JsonArray (io.vertx.core.json.JsonArray)11 NetClient (io.vertx.core.net.NetClient)11 NetSocket (io.vertx.core.net.NetSocket)11 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)11