Search in sources :

Example 36 with Message

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

the class JSONEventBusTest method testChangesNotVisibleArray2.

@Test
public void testChangesNotVisibleArray2() {
    final JsonArray obj = new JsonArray();
    eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> msg.body().add("blah"));
    eb.send("foo", obj);
    vertx.setTimer(1000, id -> {
        assertEquals(0, obj.size());
        testComplete();
    });
    await();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test)

Example 37 with Message

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

the class EventBusService method processRequestMessage.

private void processRequestMessage(final Message<JsonObject> msg) {
    if (log.isTraceEnabled()) {
        log.trace("received request message: {}", msg.body().encodePrettily());
    }
    final EventBusMessage request = EventBusMessage.fromJson(msg.body());
    processRequest(request).recover(t -> {
        log.debug("cannot process request [operation: {}]: {}", request.getOperation(), t.getMessage());
        final int status = Optional.of(t).map(cause -> {
            if (cause instanceof ServiceInvocationException) {
                return ((ServiceInvocationException) cause).getErrorCode();
            } else {
                return null;
            }
        }).orElse(HttpURLConnection.HTTP_INTERNAL_ERROR);
        return Future.succeededFuture(request.getResponse(status));
    }).map(response -> {
        if (response.getReplyToAddress() == null) {
            log.debug("sending response as direct reply to request [operation: {}]", request.getOperation());
            msg.reply(response.toJson());
        } else if (response.hasResponseProperties()) {
            log.debug("sending response [operation: {}, reply-to: {}]", request.getOperation(), request.getReplyToAddress());
            vertx.eventBus().send(request.getReplyToAddress(), response.toJson());
        } else {
            log.warn("discarding response lacking correlation ID or operation");
        }
        return null;
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Message(io.vertx.core.eventbus.Message) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) ConfigurationSupportingVerticle(org.eclipse.hono.util.ConfigurationSupportingVerticle) Future(io.vertx.core.Future) Objects(java.util.Objects) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException)

Example 38 with Message

use of io.vertx.core.eventbus.Message in project vertx-swagger by bobxwang.

the class Application method demo.

/**
 * a demo method about how to using vertx
 *
 * @param applicationContext
 */
private void demo(ApplicationContext applicationContext) {
    final Vertx vertx = applicationContext.getBean(Vertx.class);
    vertx.setPeriodic(1000, id -> {
        // This handler will get called every second
        System.out.println("timer fired!" + new Date().toString());
    });
    vertx.setTimer(1000, id -> {
        // this handler will called after one second, just once
        System.out.println("And one second later this is printed");
    });
    // 拦截所有消息
    vertx.eventBus().addInterceptor(event -> {
        Message message = event.message();
        logger.info(message);
        event.next();
    });
    FileResolver fileResolver = new FileResolver(vertx);
    File file = fileResolver.resolveFile("webjars/bycdao-ui/cdao/DUI.js");
    if (file != null) {
        System.out.println(file.getAbsoluteFile());
    } else {
        System.out.println("null");
    }
}
Also used : Message(io.vertx.core.eventbus.Message) Vertx(io.vertx.core.Vertx) FileResolver(io.vertx.core.impl.FileResolver) File(java.io.File) Date(java.util.Date)

Example 39 with Message

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

the class AsyncInvoker method next.

@Override
@SuppressWarnings("unchecked")
public void next(final Object proxy, final Method method, final Message<Envelop> message, final Vertx vertx) {
    final Envelop envelop = message.body();
    // Get type of parameter first element
    final Class<?> argType = method.getParameterTypes()[Values.IDX];
    // Deserialization from message bus.
    final Class<?> returnType = method.getReturnType();
    LOGGER.info(Info.MSG_FUTURE, this.getClass(), returnType, true);
    // Get T
    final Class<?> tCls = returnType.getComponentType();
    if (Envelop.class == tCls) {
        // Input type is Envelop, input directly
        final Future<Envelop> result = Instance.invoke(proxy, method.getName(), envelop);
        result.compose(item -> TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(item)).setHandler(Ux.toHandler(message));
    } else {
        final Object reference = envelop.data();
        final Object arguments = Ut.deserialize(Ut.toString(reference), argType);
        final Future future = Instance.invoke(proxy, method.getName(), arguments);
        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) 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) Future(io.vertx.core.Future)

Example 40 with Message

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

the class SyncInvoker 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 result = Instance.invoke(proxy, method.getName(), envelop);
    TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(result).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)

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