Example usage for io.vertx.core.http HttpServer exceptionHandler

List of usage examples for io.vertx.core.http HttpServer exceptionHandler

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServer exceptionHandler.

Prototype

@Fluent
HttpServer exceptionHandler(Handler<Throwable> handler);

Source Link

Document

Set an exception handler called for socket errors happening before the HTTP connection is established, e.g during the TLS handshake.

Usage

From source file:org.apache.servicecomb.transport.rest.vertx.RestServerVerticle.java

License:Apache License

@Override
public void start(Future<Void> startFuture) throws Exception {
    try {//ww w . j  av  a  2s  . c  om
        super.start();
        // ????????
        if (endpointObject == null) {
            LOGGER.warn("rest listen address is not configured, will not start.");
            startFuture.complete();
            return;
        }
        Router mainRouter = Router.router(vertx);
        mountGlobalRestFailureHandler(mainRouter);
        mountAccessLogHandler(mainRouter);
        mountCorsHandler(mainRouter);
        initDispatcher(mainRouter);
        HttpServer httpServer = createHttpServer();
        httpServer.requestHandler(mainRouter::accept);
        httpServer.connectionHandler(connection -> {
            int connectedCount = connectedCounter.incrementAndGet();
            int connectionLimit = DynamicPropertyFactory.getInstance()
                    .getIntProperty("servicecomb.rest.server.connection-limit", Integer.MAX_VALUE).get();
            if (connectedCount > connectionLimit) {
                connectedCounter.decrementAndGet();
                connection.close();
            } else {
                EventManager.post(new ClientEvent(connection.remoteAddress().toString(),
                        ConnectionEvent.Connected, TransportType.Rest, connectedCount));
                connection.closeHandler(event -> EventManager
                        .post(new ClientEvent(connection.remoteAddress().toString(), ConnectionEvent.Closed,
                                TransportType.Rest, connectedCounter.decrementAndGet())));
            }
        });
        httpServer.exceptionHandler(e -> {
            LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
        });
        startListen(httpServer, startFuture);
    } catch (Throwable e) {
        // vert.x got some states that not print error and execute call back in VertexUtils.blockDeploy, we add a log our self.
        LOGGER.error("", e);
        throw e;
    }
}