Example usage for io.vertx.core.net NetServer exceptionHandler

List of usage examples for io.vertx.core.net NetServer exceptionHandler

Introduction

In this page you can find the example usage for io.vertx.core.net NetServer exceptionHandler.

Prototype

@GenIgnore
@Fluent
NetServer exceptionHandler(Handler<Throwable> handler);

Source Link

Document

Set an exception handler called for socket errors happening before the connection is passed to the #connectHandler , e.g during the TLS handshake.

Usage

From source file:org.apache.servicecomb.foundation.vertx.server.TcpServer.java

License:Apache License

public void init(Vertx vertx, String sslKey, AsyncResultCallback<InetSocketAddress> callback) {
    NetServer netServer;
    if (endpointObject.isSslEnabled()) {
        SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(sslKey, null);
        SSLOption sslOption;/*from   ww  w  . j av  a 2  s. co m*/
        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 -> {
        int connectedCount = connectedCounter.incrementAndGet();
        int connectionLimit = DynamicPropertyFactory.getInstance()
                .getIntProperty("servicecomb.highway.server.connection-limit", Integer.MAX_VALUE).get();
        if (connectedCount > connectionLimit) {
            connectedCounter.decrementAndGet();
            netSocket.close();
            return;
        }

        TcpServerConnection connection = createTcpServerConnection();
        connection.init(netSocket, connectedCounter);
        EventManager.post(new ClientEvent(netSocket.remoteAddress().toString(), ConnectionEvent.Connected,
                TransportType.Highway, connectedCount));
    });
    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()));
    });
}