|
| 1 | +package io.vertx.example.reactivex.web.backpressure; |
| 2 | + |
| 3 | +import io.vertx.core.Future; |
| 4 | +import io.vertx.example.util.Runner; |
| 5 | +import io.vertx.reactivex.RxHelper; |
| 6 | +import io.vertx.reactivex.core.AbstractVerticle; |
| 7 | +import io.vertx.reactivex.core.http.HttpServer; |
| 8 | +import io.vertx.reactivex.core.http.HttpServerRequest; |
| 9 | +import io.vertx.reactivex.ext.web.Router; |
| 10 | +import io.vertx.reactivex.ext.web.handler.BodyHandler; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author tomasz.michalak |
| 14 | + */ |
| 15 | +public class Server extends AbstractVerticle { |
| 16 | + |
| 17 | + // Convenience method so you can run it in your IDE |
| 18 | + public static void main(String[] args) throws InterruptedException { |
| 19 | + Runner.runExample(io.vertx.example.reactivex.web.backpressure.Server.class); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public void start(Future<Void> fut) throws Exception { |
| 24 | + |
| 25 | + Router router = Router.router(vertx); |
| 26 | + router.route().handler(BodyHandler.create()); |
| 27 | + router.route().handler(req -> req.response().putHeader("content-type", "text/html") |
| 28 | + .end("<html><body><h1>Hello from vert.x!</h1></body></html>")); |
| 29 | + |
| 30 | + HttpServer server = vertx.createHttpServer(); |
| 31 | + server.requestStream() |
| 32 | + .toFlowable() |
| 33 | + .map(HttpServerRequest::pause) |
| 34 | + .onBackpressureDrop(req -> req.response().setStatusCode(503).end()) |
| 35 | + .observeOn(RxHelper.scheduler(vertx.getDelegate())) |
| 36 | + .subscribe(req -> { |
| 37 | + req.resume(); |
| 38 | + router.accept(req); |
| 39 | + }); |
| 40 | + |
| 41 | + server.rxListen(8080).subscribe(res -> fut.complete(), onError -> fut.fail(onError.getCause())); |
| 42 | + } |
| 43 | + |
| 44 | +} |
0 commit comments