Tony
Tony

Reputation: 4591

How to know: How many requests is HttpServer-in-Vertx serving

I write a server with Vertx.

I have about 40k users. I can print every request but I don't know how to know how many current requests my server is serving

I used this class: http://vertx.io/docs/apidocs/io/vertx/core/http/HttpServer.html but this is no method to do it

Vertx is great toolkit but it can not show server status, really????

My server code:

public class MyFirstVerticle extends AbstractVerticle {

    private HttpServer httpServer = null;

    @Override
    public void start() throws Exception {
    httpServer = vertx.createHttpServer();

    httpServer.requestHandler(new Handler<HttpServerRequest>() {
        @Override
        public void handle(HttpServerRequest request) {

            String path = request.path();
            System.out.println("incoming request: [" + request.remoteAddress() + "] at " + path);
        }
    });

    httpServer.listen(9999);
    }
}

Upvotes: 1

Views: 1411

Answers (2)

haschibaschi
haschibaschi

Reputation: 2792

Since vert.x 3.x.x http://vertx.io/docs/vertx-dropwizard-metrics is an official component of vert.x

I think what you search is this:
http://vertx.io/docs/vertx-dropwizard-metrics/java/#http-server-metrics

There are several possibilities to view the metrics

Upvotes: 1

Alexey Soshin
Alexey Soshin

Reputation: 17721

Vert.x requests are very short lived, so basing calculations solely on number of requests per second is not optimal. But, for the sake of discussion, you can achieve what you want in the following way:

public class MyFirstVerticle  extends AbstractVerticle {

    private HttpServer httpServer = null;

    // Bad, done for the sake of simplicity
    public final static AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void start() throws Exception {

        httpServer = vertx.createHttpServer();

        httpServer.requestHandler(new Handler<HttpServerRequest>() {

            public void handle(HttpServerRequest request) {

                counter.incrementAndGet();
                String path = request.path();
                System.out.println("incoming request: [" + request.remoteAddress() + "] at " + path);
                request.response().end();

                request.endHandler(new Handler<Void>() {
                    @Override
                    public void handle(Void aVoid) {
                        counter.decrementAndGet();
                    }
                });
            }
        });

        httpServer.listen(9999);
    }
}

Then to test yourself you can try something like that:

public class Main {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        MyFirstVerticle verticle = new MyFirstVerticle();

        vertx.deployVerticle(verticle);
        // Very bad, but our request are very short
        vertx.setPeriodic(1, new Handler<Long>() {
            @Override
            public void handle(Long aLong) {
                // Otherwise will overflow our console
                if (MyFirstVerticle.counter.intValue() > 0) {
                    System.out.println(MyFirstVerticle.counter.intValue());
                }
            }
        });
    }
}

Upvotes: 0

Related Questions