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());
}
}
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();
}));
}
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);
}
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);
}
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());
}
}
Aggregations