Search in sources :

Example 11 with Message

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

the class TestVerticle method testHandlerAdaptor.

@Suspendable
protected void testHandlerAdaptor() 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
    long start = System.currentTimeMillis();
    vertx.setPeriodic(10, tid -> {
        eb.send(ADDRESS1, "wibble");
        eb.send(ADDRESS2, "flibble");
    });
    for (int i = 0; i < 10; i++) {
        Message<String> received1 = adaptor1.receive();
        assertEquals("wibble", received1.body());
        Message<String> received2 = adaptor2.receive();
        assertEquals("flibble", received2.body());
    }
    long end = System.currentTimeMillis();
    assertTrue(end - start >= 100);
    // Try a receive with timeout
    Message<String> received1 = adaptor1.receive(1000);
    assertEquals("wibble", received1.body());
    // And timing out
    HandlerReceiverAdaptor<Message<String>> adaptor3 = streamAdaptor();
    eb.<String>consumer(ADDRESS3).handler(adaptor3);
    Message<String> received3 = adaptor3.receive(100);
    assertNull(received3);
    // Try underlying receivePort
    ReceivePort<Message<String>> channel = adaptor1.receivePort();
    assertNotNull(channel);
    received1 = channel.receive();
    assertEquals("wibble", received1.body());
    complete();
}
Also used : Message(io.vertx.core.eventbus.Message) EventBus(io.vertx.core.eventbus.EventBus) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 12 with Message

use of io.vertx.core.eventbus.Message in project vertx-zero by silentbalanceyh.

the class FutureInvoker method next.

@Override
@SuppressWarnings("unchecked")
public void next(final Object proxy, final Method method, final Message<Envelop> message, final Vertx vertx) {
    // Invoke directly
    final Envelop envelop = message.body();
    // Future<T>
    final Class<?> returnType = method.getReturnType();
    // Get T
    final Class<?> tCls = returnType.getComponentType();
    LOGGER.info(Info.MSG_FUTURE, this.getClass(), returnType, true);
    if (Envelop.class == tCls) {
        // Execute Future<Envelop>
        final Future<Envelop> future = Instance.invoke(proxy, method.getName(), envelop);
        future.compose(item -> TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(item)).setHandler(Ux.toHandler(message));
    } else {
        final Future future = Instance.invoke(proxy, method.getName(), envelop);
        future.compose(item -> TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(Ux.to(item))).compose(item -> Future.succeededFuture(Ux.to(item))).setHandler(Ux.toHandler(message));
    }
}
Also used : TunnelClient(io.vertx.up.micro.ipc.client.TunnelClient) Vertx(io.vertx.core.Vertx) Annal(io.vertx.up.log.Annal) Message(io.vertx.core.eventbus.Message) Envelop(io.vertx.up.atom.Envelop) Ux(io.vertx.up.aiki.Ux) Future(io.vertx.core.Future) Method(java.lang.reflect.Method) Instance(io.vertx.up.tool.mirror.Instance) Envelop(io.vertx.up.atom.Envelop) Future(io.vertx.core.Future)

Example 13 with Message

use of io.vertx.core.eventbus.Message in project vertx-zero by silentbalanceyh.

the class DynamicInvoker method next.

@Override
public void next(final Object proxy, final Method method, final Message<Envelop> message, final Vertx vertx) {
    final Envelop envelop = message.body();
    LOGGER.info(Info.MSG_FUTURE, this.getClass(), method.getReturnType(), true);
    final Envelop returnValue = this.executeMethod(proxy, method, envelop);
    TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(returnValue).compose(item -> Future.succeededFuture(Ux.to(item))).setHandler(Ux.toHandler(message));
}
Also used : ZeroSerializer(io.vertx.up.web.ZeroSerializer) TunnelClient(io.vertx.up.micro.ipc.client.TunnelClient) Values(io.vertx.zero.eon.Values) Vertx(io.vertx.core.Vertx) Annal(io.vertx.up.log.Annal) Message(io.vertx.core.eventbus.Message) Envelop(io.vertx.up.atom.Envelop) Ut(io.vertx.up.tool.Ut) Ux(io.vertx.up.aiki.Ux) Future(io.vertx.core.Future) Method(java.lang.reflect.Method) Instance(io.vertx.up.tool.mirror.Instance) Envelop(io.vertx.up.atom.Envelop)

Example 14 with Message

use of io.vertx.core.eventbus.Message in project narayana by jbosstm.

the class ModuleIntegrationTest method testPing.

@Test
public // public void testPing(TestContext context) {
void testPing() {
    TestContext context = null;
    System.out.printf("in testPing()%n");
    logger.info("in testPing()");
    EventBus eb = vertx.eventBus();
    eb.publish("ping-address", "ping!");
    eb.consumer("ping-address", (Message<JsonObject> reply) -> {
        context.assertEquals("pong! 12", reply.body());
        /*
          If we get here, the test is complete
          You must always call `testComplete()` at the end. Remember that testing is *asynchronous* so
          we cannot assume the test is complete by the time the test method has finished executing like
          in standard synchronous tests
          */
        testComplete();
    });
}
Also used : Message(io.vertx.core.eventbus.Message) TestContext(io.vertx.ext.unit.TestContext) EventBus(io.vertx.core.eventbus.EventBus) Test(org.junit.Test)

Example 15 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)

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