Reputation: 1
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?
Upvotes: 0
Views: 1043
Reputation: 191
You can try out this.
Try changing values for these. #1 should work for your requirement.
Upvotes: 0
Reputation: 17701
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.
Upvotes: 1