Handling requests and calling the next handler

Vert.x-Web``route一个HTTP Request到一个与之匹配的route,它会向该routehandler传递一个RoutingContext实例.

如果在当前handler里,你不想结束response, 那么你应该调用下一个相匹配的route继续处理该请求.

你没有必要在当前handler执行完之前调用下一个route, 你可以稍后再做这件事.

  1. Route route1 = router.route("/some/path/").handler(routingContext -> {
  2. HttpServerResponse response = routingContext.response();
  3. // enable chunked responses because we will be adding data as
  4. // we execute over other handlers. This is only required once and
  5. // only if several handlers do output.
  6. response.setChunked(true);
  7. response.write("route1\n");
  8. // Call the next matching route after a 5 second delay
  9. routingContext.vertx().setTimer(5000, tid -> routingContext.next());
  10. });
  11. Route route2 = router.route("/some/path/").handler(routingContext -> {
  12. HttpServerResponse response = routingContext.response();
  13. response.write("route2\n");
  14. // Call the next matching route after a 5 second delay
  15. routingContext.vertx().setTimer(5000, tid -> routingContext.next());
  16. });
  17. Route route3 = router.route("/some/path/").handler(routingContext -> {
  18. HttpServerResponse response = routingContext.response();
  19. response.write("route3");
  20. // Now end the response
  21. routingContext.response().end();
  22. });

在上面的例子中, route1被写到response的5秒钟之后,route2被写到response,在过了5秒钟之后,route3被写到response,然后结束掉response.

注意这一切的发生都是非阻塞的,