Search in sources :

Example 6 with Message

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

the class Consume 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");
    });
    // This runs on an event loop but the event loop is at no time blocked!
    for (int i = 0; i < 10; i++) {
        System.out.println("Thread is " + Thread.currentThread());
        Message<String> received1 = adaptor1.receive();
        System.out.println("got message: " + received1.body());
        Message<String> received2 = adaptor2.receive();
        System.out.println("got message: " + received2.body());
    }
}
Also used : Message(io.vertx.core.eventbus.Message) EventBus(io.vertx.core.eventbus.EventBus) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 7 with Message

use of io.vertx.core.eventbus.Message in project chili-core by codingchili.

the class BusRouterTest method testRouteRecipientFailureLogged.

@Test
public void testRouteRecipientFailureLogged(TestContext test) {
    Async async = test.async();
    String node = UUID.randomUUID().toString();
    mockNode(node, message -> message.fail(0, "fail"));
    handle(node, ((response, status) -> {
        test.assertTrue(recipientFailure.get());
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Message(io.vertx.core.eventbus.Message) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Timeout(io.vertx.ext.unit.junit.Timeout) Test(org.junit.Test) CoreStrings(com.codingchili.core.configuration.CoreStrings) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) ResponseStatus(com.codingchili.core.protocol.ResponseStatus) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ResponseListener(com.codingchili.core.testing.ResponseListener) RequestMock(com.codingchili.core.testing.RequestMock) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) ACCEPTED(com.codingchili.core.protocol.ResponseStatus.ACCEPTED) SystemContext(com.codingchili.core.context.SystemContext) Handler(io.vertx.core.Handler) Configurations.system(com.codingchili.core.files.Configurations.system) CoreContext(com.codingchili.core.context.CoreContext) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 8 with Message

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

the class EventbusBridgeTest method testReplyMessagesOutbound.

@Test
public void testReplyMessagesOutbound() throws Exception {
    // Only allow outbound address, reply message should still get through though
    sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr)));
    CountDownLatch latch = new CountDownLatch(1);
    client.websocket(websocketURI, ws -> {
        JsonObject reg = new JsonObject().put("type", "register").put("address", addr);
        ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(reg.encode(), true));
        ws.handler(buff -> {
            String str = buff.toString();
            JsonObject received = new JsonObject(str);
            Object rec = received.getValue("body");
            assertEquals("foobar", rec);
            // Now send back reply
            JsonObject reply = new JsonObject().put("type", "send").put("address", received.getString("replyAddress")).put("body", "barfoo");
            ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(reply.encode(), true));
        });
        vertx.setTimer(500, tid -> vertx.eventBus().send(addr, "foobar", res -> {
            if (res.succeeded()) {
                assertEquals("barfoo", res.result().body());
                ws.closeHandler(v2 -> latch.countDown());
                ws.close();
            }
        }));
    });
    awaitLatch(latch);
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) Message(io.vertx.core.eventbus.Message) Router(io.vertx.ext.web.Router) Test(org.junit.Test) UUID(java.util.UUID) io.vertx.ext.web.handler.sockjs(io.vertx.ext.web.handler.sockjs) AuthProvider(io.vertx.ext.auth.AuthProvider) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketBase(io.vertx.core.http.WebSocketBase) ShiroAuth(io.vertx.ext.auth.shiro.ShiroAuth) SessionStore(io.vertx.ext.web.sstore.SessionStore) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) ShiroAuthRealmType(io.vertx.ext.auth.shiro.ShiroAuthRealmType) WebTestBase(io.vertx.ext.web.WebTestBase) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with Message

use of io.vertx.core.eventbus.Message in project VX-API-Gateway by EliMirren.

the class ClientVerticle method findAPP.

/**
 * 查看所有应用程序
 *
 * @param rct
 */
public void findAPP(RoutingContext rct) {
    LOG.info(MessageFormat.format("[user : {0}] 执行查询应用...", rct.session().<String>get("userName")));
    HttpServerResponse response = rct.response().putHeader(CONTENT_TYPE, CONTENT_VALUE_JSON_UTF8);
    // 查询应用是否已经启动
    Future<JsonArray> onlineFuture = Future.future();
    // 查询所有APP
    Future<Message<JsonArray>> findAppFuture = Future.future();
    findAppFuture.setHandler(res -> {
        if (res.succeeded()) {
            JsonArray body = res.result().body();
            if (body.size() > 0) {
                onlineFuture.complete(body);
            } else {
                response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, body));
            }
        } else {
            LOG.error(MessageFormat.format("[user : {0}] 执行查询应用-->失败:{1}", res.cause().getMessage(), rct.session().<String>get("userName")));
            response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, res.cause().getMessage()));
        }
    });
    onlineFuture.setHandler(res -> {
        // 拓展原来没有显示是否正在运行的属性,如果后期需要优化,可以加多一层业务层,查看应用是否正在运行在业务层处理
        vertx.eventBus().<JsonArray>send(thisVertxName + VxApiEventBusAddressConstant.DEPLOY_FIND_ONLINE_APP, null, dep -> {
            if (dep.succeeded()) {
                JsonArray body = res.result();
                JsonArray array = new JsonArray();
                JsonArray online = dep.result().body();
                body.forEach(obj -> {
                    JsonObject data = (JsonObject) obj;
                    JsonObject newObj = new JsonObject();
                    String appName = data.getString("appName");
                    newObj.put("appName", appName);
                    newObj.put("describe", data.getString("describe"));
                    newObj.put("time", data.getInstant("time"));
                    newObj.put("scope", data.getInteger("scope"));
                    newObj.put("online", online.contains(appName));
                    array.add(newObj);
                });
                response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, array));
            } else {
                response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, res.cause().getMessage()));
            }
        });
    });
    vertx.eventBus().<JsonArray>send(thisVertxName + VxApiEventBusAddressConstant.FIND_APP, null, findAppFuture);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Message(io.vertx.core.eventbus.Message) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject)

Example 10 with Message

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

the class Examples method streamExample.

public void streamExample(Vertx vertx) {
    EventBus eb = vertx.eventBus();
    HandlerReceiverAdaptor<Message<String>> adaptor = streamAdaptor();
    eb.<String>consumer("some-address").handler(adaptor);
    // Receive 10 messages from the consumer:
    for (int i = 0; i < 10; i++) {
        Message<String> received1 = adaptor.receive();
        System.out.println("got message: " + received1.body());
    }
}
Also used : Message(io.vertx.core.eventbus.Message) EventBus(io.vertx.core.eventbus.EventBus)

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