use of io.vertx.core.eventbus.Message in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testUnRegister.
@Test
public void testUnRegister(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
final String address = "test";
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
// 2 replies will arrive:
// 1). message published to test
// 2). err of NO_HANDLERS because of consumer for 'test' is unregistered.
final AtomicBoolean unregistered = new AtomicBoolean(false);
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
if (unregistered.get()) {
// consumer on 'test' has been unregistered, send message will fail.
context.assertEquals("err", frame.getString("type"));
context.assertEquals("#backtrack", frame.getString("address"));
context.assertEquals("NO_HANDLERS", frame.getString("failureType"));
context.assertEquals("No handlers for address test", frame.getString("message"));
client.close();
async.complete();
} else {
// got message, then unregister the handler
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals(false, frame.getBoolean("send"));
context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
unregistered.compareAndSet(false, true);
FrameHelper.sendFrame("unregister", address, null, socket);
FrameHelper.sendFrame("send", address, "#backtrack", new JsonObject().put("value", "This will fail anyway!"), socket);
}
});
socket.handler(parser);
FrameHelper.sendFrame("register", address, null, socket);
FrameHelper.sendFrame("publish", address, new JsonObject().put("value", "Vert.x"), socket);
}));
}
use of io.vertx.core.eventbus.Message in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testReplyFromClient.
@Test
public void testReplyFromClient(TestContext context) {
// Send a request from java and get a response from the client
NetClient client = vertx.createNetClient();
final Async async = context.async();
final String address = "test";
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
if ("message".equals(frame.getString("type"))) {
context.assertEquals(true, frame.getBoolean("send"));
context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
FrameHelper.sendFrame("send", frame.getString("replyAddress"), new JsonObject().put("value", "You got it"), socket);
}
});
socket.handler(parser);
FrameHelper.sendFrame("register", address, null, socket);
// There is now way to know that the register actually happened, wait a bit before sending.
vertx.setTimer(500L, timerId -> {
vertx.eventBus().<JsonObject>request(address, new JsonObject().put("value", "Vert.x"), respMessage -> {
context.assertTrue(respMessage.succeeded());
context.assertEquals("You got it", respMessage.result().body().getString("value"));
client.close();
async.complete();
});
});
}));
}
use of io.vertx.core.eventbus.Message in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeEventTest method testSendVoidMessageTrustAll.
@Test
public void testSendVoidMessageTrustAll(TestContext context) {
NetClient client = vertx.createNetClient(new NetClientOptions().setSsl(true).setTrustAll(true).setKeyStoreOptions(sslKeyPairCerts.getClientKeyStore()));
final Async async = context.async();
vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
client.close();
async.complete();
});
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
FrameHelper.sendFrame("send", "test", new JsonObject().put("value", "vert.x"), socket);
});
}
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");
}
}
use of io.vertx.core.eventbus.Message in project VX-API-Gateway by EliMirren.
the class VxApiApplication method addRoute.
/**
* 添加一个路由
*
* @param msg
*/
public void addRoute(Message<JsonObject> msg) {
JsonObject body = msg.body().getJsonObject("api");
VxApisDTO dto = VxApisDTO.fromJson(body);
if (dto != null) {
VxApis api = new VxApis(dto);
// 是否代理启动API到当前应用
boolean otherRouteAdd = msg.body().getBoolean("elseRouteToThis", false);
if (otherRouteAdd) {
// 服务器的类型1=http,2=https,3=webSocket
int type = msg.body().getInteger("serverType", 0);
if (type == 1) {
addHttpRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else if (type == 2) {
addHttpsRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
res.cause().printStackTrace();
}
});
} else {
msg.fail(500, "不存在的服务");
}
} else {
// 本应用添加API,既属于自己的网关应用添加API
if (httpRouter != null && httpsRouter != null) {
Future<Boolean> httpFuture = Future.future(http -> addHttpRouter(api, http));
Future<Boolean> httpsFuture = Future.<Boolean>future(https -> addHttpsRouter(api, https));
CompositeFuture.all(httpFuture, httpsFuture).setHandler(res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else if (httpRouter != null) {
addHttpRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else if (httpsRouter != null) {
addHttpsRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else {
msg.fail(404, "找不到的服务器可以加载API");
}
}
} else {
msg.fail(1400, "API参数不能为null,请检查APIDTO需要实例化的JSON编写是否正确");
}
}
Aggregations