use of io.vertx.core.net.NetServer in project vertx-micrometer-metrics by vert-x3.
the class VertxNetClientServerMetricsTest method setUp.
@Before
public void setUp(TestContext ctx) {
vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MicrometerMetricsOptions().setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true)).setRegistryName(registryName).setEnabled(true))).exceptionHandler(ctx.exceptionHandler());
// Setup server
Async serverReady = ctx.async();
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start(Future<Void> future) throws Exception {
netServer = vertx.createNetServer();
netServer.connectHandler(socket -> socket.handler(buffer -> socket.write(SERVER_RESPONSE))).listen(9194, "localhost", r -> {
if (r.failed()) {
ctx.fail(r.cause());
} else {
serverReady.complete();
}
});
}
});
serverReady.awaitSuccess();
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class ConnectionBaseTest method testQueueFlushFromEventLoop.
@Test
public void testQueueFlushFromEventLoop() {
NetClient client = vertx.createNetClient();
NetServer server = vertx.createNetServer();
try {
server.connectHandler(so -> {
ConnectionBase conn = (ConnectionBase) so;
ChannelHandlerContext ctx = conn.channelHandlerContext();
ChannelPipeline pipeline = ctx.pipeline();
List<String> order = new ArrayList<>();
Runnable checkOrder = () -> {
if (order.size() == 3) {
vertx.runOnContext(v -> {
assertEquals(Arrays.asList("msg1", "msg2", "flush"), order);
testComplete();
});
}
};
pipeline.addBefore("handler", "myhandler", new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof String) {
String s = (String) msg;
order.add(s);
if ("msg1".equals(s)) {
// Flush a message why there are two messages queued on the connection
conn.flush();
}
checkOrder.run();
} else {
super.write(ctx, msg, promise);
}
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
order.add("flush");
checkOrder.run();
super.flush(ctx);
}
});
executeAsyncTask(() -> {
conn.writeToChannel("msg1");
conn.writeToChannel("msg2");
});
});
server.listen(1234, "localhost", onSuccess(s -> {
client.connect(1234, "localhost", onSuccess(so -> {
}));
}));
await();
} finally {
server.close();
client.close();
}
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class ConnectionBaseTest method testQueueMessageFromEvent.
@Test
public void testQueueMessageFromEvent() {
NetClient client = vertx.createNetClient();
NetServer server = vertx.createNetServer();
try {
server.connectHandler(so -> {
NetSocketInternal conn = (NetSocketInternal) so;
ChannelHandlerContext ctx = conn.channelHandlerContext();
ChannelPipeline pipeline = ctx.pipeline();
List<String> order = new ArrayList<>();
pipeline.addBefore("handler", "myhandler", new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof String) {
String s = (String) msg;
order.add(s);
if ("msg1".equals(s)) {
// Write a message why there are two messages queued on the connection
conn.writeMessage("msg3");
}
if (order.size() == 3) {
vertx.runOnContext(v -> {
assertEquals(Arrays.asList("msg1", "msg2", "msg3"), order);
testComplete();
});
}
} else {
super.write(ctx, msg, promise);
}
}
});
executeAsyncTask(() -> {
conn.writeMessage("msg1");
conn.writeMessage("msg2");
});
});
server.listen(1234, "localhost", onSuccess(s -> {
client.connect(1234, "localhost", onSuccess(so -> {
}));
}));
await();
} finally {
server.close();
client.close();
}
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class StreamsExamples method pipe5.
public void pipe5(Vertx vertx) {
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
server.connectHandler(sock -> {
sock.pipeTo(sock);
}).listen();
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class StreamsExamples method pipe2.
public void pipe2(Vertx vertx) {
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
server.connectHandler(sock -> {
sock.handler(buffer -> {
if (!sock.writeQueueFull()) {
sock.write(buffer);
}
});
}).listen();
}
Aggregations