0

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 Answers2

1

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.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • Hi Alexey, Thanks for your response. Is the Timeout for the thread or for just the ASYNC operations done by the thread. – sushma kuchipudi Aug 17 '20 at 18:12
  • The timeout is for the request. You shouldn't work with threads directly, if you use Vert.x, since Vert.x multiplexes them. – Alexey Soshin Aug 17 '20 at 18:26
  • In my code, there is some business logic after sending OK response. I need to put timeout on that. please suggest how to do that – sushma kuchipudi Aug 17 '20 at 18:53
  • @ Alexey, does setMaxWorkerExecuteTime() help me here in anyway? – sushma kuchipudi Aug 17 '20 at 19:49
  • If your business logic is wrapped within non-blocking handlers, you should be fine. `setMaxWorkerExecuteTime` won't help you with that, though. AFAIK it only displays warning if worker is running for too long. – Alexey Soshin Aug 17 '20 at 20:38
0

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.

TheAkashKamble
  • 181
  • 2
  • 17