please suggest how to use vertx Timeout handler for vertx 3.7.1

883 Views Asked by At

i am trying this

router.put(/api).handler(TimeoutHandler.create(100,404)); router.put(/api).blockingHandler(this::handlebusinesslogic); 
handlebusinesslogic{
Thread.sleep(1000);
reponse.setstatuscode(200);
reponse.end();}

still, I see 200 ok response instead of 404 response. is there something missing from the code. is there an alternative way to do this.

is there a way to set a general timeout for all HTTP requests?

2

There are 2 best solutions below

5
Alexey Soshin On

That's because you shouldn't use Thread.sleep() while testing anything in Vert.x
In your case, this will also block the timeout handler, preventing the timeout.

Here's how you should test it:

        Vertx vertx = Vertx.vertx();

        Router router = Router.router(vertx);

        router.route().handler(TimeoutHandler.create(100, 404));
        router.route().handler(event -> {
            // Instead of blocking the thread, set a long timer 
            // that simulates your long ASYNCHRONOUS request
            vertx.setTimer(1000, (l) -> {
                event.response().end("OK!");
            });
        });
        
        vertx.createHttpServer().requestHandler(router).listen(8080);

This example will return 404 as expected.

0
TheAkashKamble On

You can try out this.

  1. setConnectTimeout(int connectTimeout)
  2. setHttp2KeepAliveTimeout(int keepAliveTimeout)
  3. setIdleTimeout(int idleTimeout)

Try changing values for these. #1 should work for your requirement.