Java Code Examples for io.vertx.core.net.NetServer#listen()
The following examples show how to use
io.vertx.core.net.NetServer#listen() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TcpServerProvider.java From jetlinks-community with Apache License 2.0 | 6 votes |
private void initTcpServer(VertxTcpServer tcpServer, TcpServerProperties properties) { int instance = Math.max(2, properties.getInstance()); List<NetServer> instances = new ArrayList<>(instance); for (int i = 0; i < instance; i++) { instances.add(vertx.createNetServer(properties.getOptions())); } payloadParserBuilder.build(properties.getParserType(), properties); tcpServer.setParserSupplier(() -> payloadParserBuilder.build(properties.getParserType(), properties)); tcpServer.setServer(instances); tcpServer.setKeepAliveTimeout(properties.getLong("keepAliveTimeout", Duration.ofMinutes(10).toMillis())); for (NetServer netServer : instances) { netServer.listen(properties.createSocketAddress(), result -> { if (result.succeeded()) { log.info("tcp server startup on {}", result.result().actualPort()); } else { log.error("startup tcp server error", result.cause()); } }); } }
Example 2
Source File: TestTcpServer.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testTcpServerNonSSL(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback, @Mocked NetServer netServer) { new Expectations() { { vertx.createNetServer(); result = netServer; netServer.connectHandler((Handler) any); netServer.listen(anyInt, anyString, (Handler) any); } }; URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663"); TcpServer server = new TcpServerForTest(endpointObject); // assert done in Expectations server.init(vertx, "", callback); }
Example 3
Source File: TestTcpServer.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testTcpServerSSL(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback, @Mocked NetServer netServer) { new Expectations() { { vertx.createNetServer((NetServerOptions) any); result = netServer; netServer.connectHandler((Handler) any); netServer.listen(anyInt, anyString, (Handler) any); } }; URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true"); TcpServer server = new TcpServerForTest(endpointObject); // assert done in Expectations server.init(vertx, "", callback); }
Example 4
Source File: ProtonClientTest.java From vertx-proton with Apache License 2.0 | 6 votes |
@Test(timeout = 20000) public void testConnectionDisconnectedDuringCreation(TestContext context) { server.close(); Async connectFailsAsync = context.async(); NetServer netServer = this.vertx.createNetServer(); netServer.connectHandler(netSocket -> { netSocket.pause(); vertx.setTimer(50, x -> { netSocket.close(); }); }); netServer.listen(listenResult -> { context.assertTrue(listenResult.succeeded()); ProtonClient.create(vertx).connect("localhost", netServer.actualPort(), connResult -> { context.assertFalse(connResult.succeeded()); connectFailsAsync.complete(); }); }); connectFailsAsync.awaitSuccess(); }
Example 5
Source File: WebClientTest.java From vertx-web with Apache License 2.0 | 6 votes |
@Repeat(times = 100) @Test public void testTimeoutRequestBeforeSending() throws Exception { NetServer server = vertx.createNetServer(); server.connectHandler(so -> { }); CountDownLatch latch = new CountDownLatch(1); server.listen(8080, "localhost", onSuccess(v -> { latch.countDown(); })); awaitLatch(latch); webClient .get(8080, "localhost", "/") .timeout(1) .send(onFailure(err -> { testComplete(); })); await(); }
Example 6
Source File: ProcessModuleHandleTest.java From okapi with Apache License 2.0 | 5 votes |
@Test public void testPortAlreadyInUse(TestContext context) { final Async async = context.async(); final NetServer ns = vertx.createNetServer().connectHandler( res -> { res.close(); }); ns.listen(9231, res -> { if (res.failed()) { async.complete(); ns.close(); } else { LaunchDescriptor desc = new LaunchDescriptor(); desc.setExec("java " + testModuleArgs); ModuleHandle mh = createModuleHandle(desc, 9231); mh.start(res1 -> { context.assertTrue(res1.failed()); context.assertEquals("port 9231 already in use", res1.cause().getMessage()); ns.close(); // stop is not necessary, but check that we can call it anyway mh.stop(res2 -> { context.assertTrue(res2.succeeded()); async.complete(); }); }); } }); }
Example 7
Source File: TcpServer.java From servicecomb-java-chassis with Apache License 2.0 | 4 votes |
public void init(Vertx vertx, String sslKey, AsyncResultCallback<InetSocketAddress> callback) { NetServer netServer; if (endpointObject.isSslEnabled()) { SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(sslKey, null); SSLOption sslOption; if (factory == null) { sslOption = SSLOption.buildFromYaml(sslKey); } else { sslOption = factory.createSSLOption(); } SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass()); NetServerOptions serverOptions = new NetServerOptions(); VertxTLSBuilder.buildNetServerOptions(sslOption, sslCustom, serverOptions); netServer = vertx.createNetServer(serverOptions); } else { netServer = vertx.createNetServer(); } netServer.connectHandler(netSocket -> { DefaultTcpServerMetrics serverMetrics = (DefaultTcpServerMetrics) ((NetSocketImpl) netSocket).metrics(); DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric(); long connectedCount = endpointMetric.getCurrentConnectionCount(); int connectionLimit = getConnectionLimit(); if (connectedCount > connectionLimit) { netSocket.close(); endpointMetric.onRejectByConnectionLimit(); return; } TcpServerConnection connection = createTcpServerConnection(); connection.init(netSocket); }); netServer.exceptionHandler(e -> { LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e)); }); InetSocketAddress socketAddress = endpointObject.getSocketAddress(); netServer.listen(socketAddress.getPort(), socketAddress.getHostString(), ar -> { if (ar.succeeded()) { callback.success(socketAddress); return; } // 监听失败 String msg = String.format("listen failed, address=%s", socketAddress.toString()); callback.fail(new Exception(msg, ar.cause())); }); }
Example 8
Source File: TestTcpServer.java From servicecomb-java-chassis with Apache License 2.0 | 4 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testConnectionLimit(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback, @Mocked NetServer netServer, @Mocked NetSocketImpl netSocket) { DefaultServerEndpointMetric endpointMetric = new DefaultServerEndpointMetric(null); DefaultTcpServerMetrics tcpServerMetrics = new DefaultTcpServerMetrics(endpointMetric); new MockUp<NetServer>(netServer) { @Mock NetServer connectHandler(Handler<NetSocket> handler) { connectHandler = handler; return netServer; } }; new MockUp<NetSocketImpl>(netSocket) { @Mock void close() { netSocketClosed = true; } }; new Expectations() { { vertx.createNetServer((NetServerOptions) any); result = netServer; netServer.listen(anyInt, anyString, (Handler) any); netSocket.metrics(); result = tcpServerMetrics; } }; URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true"); TcpServer server = new TcpServerForTest(endpointObject) { @Override protected int getConnectionLimit() { return 2; } }; // assert done in Expectations server.init(vertx, "", callback); // no problem endpointMetric.onConnect(); endpointMetric.onConnect(); connectHandler.handle(netSocket); // reject endpointMetric.onConnect(); connectHandler.handle(netSocket); Assert.assertTrue(netSocketClosed); Assert.assertEquals(1, endpointMetric.getRejectByConnectionLimitCount()); }