简体   繁体   English

Vertx 事件总线拦截器阻塞请求调用

[英]Vertx event bus interceptor blocking the request call

I am trying to send data over event bus when got the call from client and response back to client.当收到来自客户端的呼叫并响应回客户端时,我正在尝试通过事件总线发送数据。

Everything is working fine until i add the interceptor on event bus..一切正常,直到我在事件总线上添加拦截器..

Here is the Code:-这是代码:-

public class TestVerticle extends AbstractVerticle {

    @Override
    public void start(Promise<Void> promise) {

        Router router = Router.router(vertx);

        vertx.exceptionHandler(globalExceptionHandler -> {
            System.out.println("Exception not handled in application  : " + globalExceptionHandler.getCause());
        });

        vertx.eventBus().consumer("test", handler -> {
            System.out.println("Message receive form event bus : " + handler.body().toString());
            handler.reply(handler.body().toString());
        });

        router.get().handler(this::rootHandler);
        vertx.createHttpServer().requestHandler(router).listen(8080, resultHandler -> {
            if (resultHandler.succeeded()) {
                promise.complete();
            } else {
                promise.fail(resultHandler.cause());
            }
        });

        vertx.eventBus().addOutboundInterceptor(handler -> {
            System.out.println("Outbound data : "+handler.body().toString());
        });

//        vertx.eventBus().addInboundInterceptor(handler -> {
//            System.out.println("Inbound data : " + handler.body().toString());
//        });
    }

    private void rootHandler(RoutingContext routingContext) {
        JsonObject msg = new JsonObject().put("path", routingContext.request().path());
        vertx.eventBus().request("test","Hello from Application",reply -> this.replyHandler(routingContext,reply));
    }

    private void replyHandler(RoutingContext ctx, AsyncResult<Message<Object>> reply) {
        HttpServerResponse response = ctx.response()
                .putHeader("Content-Type", "application/json");
        System.out.println("Reply from event bus : " +reply.result().body().toString());
        if (reply.succeeded()) {
            response.setStatusCode(200)
                    .setStatusMessage("OK")
                    .end(reply.result().body().toString());
        } else {
            response.setStatusCode(500)
                    .setStatusMessage("Server Error")
                    .end(new JsonObject().put("error", reply.cause().getLocalizedMessage()).encodePrettily());
        }
    }

    public static void main(String[] args) {
        Vertx.vertx().deployVerticle(new TestVerticle(), deploymenHandler -> {
            if (deploymenHandler.succeeded()) {
                System.out.println("verticle deplyed");
            } else {
                System.out.println("failed");
            }
        });

    }

    @Override
    public void stop(Promise<Void> promise) {
        System.out.println("Exiting verticle");
        promise.complete()
    }
}

One more doubt is when I stop the application from IDE the Stop method is not called, but If I undeploy this verticle from another verticle that is working fine.还有一个疑问是,当我从 IDE 停止应用程序时,不会调用 Stop 方法,但是如果我从另一个工作正常的 Verticle 取消部署该 Verticle。

When you are using eventbus interceptors you will have to call the next() method in the handler otherwise you will get stuck there.当您使用事件总线拦截器时,您必须在处理程序中调用next()方法,否则您将被卡在那里。

vertx.eventBus().addOutboundInterceptor(handler -> {
  System.out.println("Outbound data : "+handler.body().toString());
  handler.next();
});

vertx.eventBus().addInboundInterceptor(handler -> {
  System.out.println("Inbound data : "+handler.body().toString());
  handler.next();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM