简体   繁体   English

如何使用WebSocket和异步请求将阻止操作卸载到工作人员Verticle

[英]How to offload blocking operation to a worker Verticle using websockets and async request

I implementing websockets using Vert.x 3. 我使用Vert.x 3实现websockets。

The scenario is simple: opening socket from client doing some 'blocking' work at the vertex verticle worker and when finish response with the answer to the client(via the open socket) 场景很简单:从客户端打开套接字,在顶点垂直工作器上执行一些“阻塞”工作,并在完成对客户端答案的响应后(通过打开的套接字)

Please tell me if I am doing it right: 请告诉我我是否做对了:

Created VertxWebsocketServerVerticle. 创建了VertxWebsocketServerVerticle。 as soon as the websocket is opening and request coming from the client I am using eventBus and passing the message to 当websocket打开并且请求来自客户端时,我正在使用eventBus并将消息传递给

EventBusReceiverVerticle. EventBusReceiverVerticle。 there I am doing blocking operation. 在那里我正在执行阻塞操作。

how I am actually sending back the response back to VertxWebsocketServerVerticle and sending it back to the client? 我如何实际上将响应发送回VertxWebsocketServerVerticle并将其发送回客户端?

code: 码:

Main class: 主班:

 public static void main(String[] args) throws InterruptedException {
        Vertx vertx = Vertx.vertx();
       vertx.deployVerticle(new EventBusReceiverVerticle("R1"),new DeploymentOptions().setWorker(true));
        vertx.deployVerticle(new VertxWebsocketServerVerticle());
}

VertxWebsocketServerVerticle: VertxWebsocketServerVerticle:

public class VertxWebsocketServerVerticle extends AbstractVerticle {


    public void start() {
        vertx.createHttpServer().websocketHandler(webSocketHandler -> {

            System.out.println("Connected!");
            Buffer buff = Buffer.buffer().appendInt(12).appendString("foo");
            webSocketHandler.writeFinalBinaryFrame(buff);
            webSocketHandler.handler(buffer -> {
                String inputString = buffer.getString(0, buffer.length());
                System.out.println("inputString=" + inputString);
                vertx.executeBlocking(future -> {
                    vertx.eventBus().send("anAddress", inputString, event -> System.out.printf("got back from reply"));
                    future.complete();
                }, res -> {
                    if (res.succeeded()) {
                        webSocketHandler.writeFinalTextFrame("output=" + inputString + "_result");
                    }
                });

            });
        }).listen(8080);
    }


    @Override
    public void stop() throws Exception {
        super.stop();
    }
}

EventBusReceiverVerticle : EventBusReceiverVerticle:

public class EventBusReceiverVerticle extends AbstractVerticle {

        private String name = null;

        public EventBusReceiverVerticle(String name) {
            this.name = name;
        }

        public void start(Future<Void> startFuture) {
            vertx.eventBus().consumer("anAddress", message -> {
                System.out.println(this.name +
                        " received message: " +
                        message.body());
                try {
                    //doing some looong work..
                    Thread.sleep(10000);
                    System.out.printf("finished waiting\n");
                    startFuture.complete();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

I always get: 我总是得到:

WARNING: Message reply handler timed out as no reply was received - it will be removed

github project at: https://github.com/IdanFridman/VertxAndWebSockets thank you, ray. github项目位于: https : //github.com/IdanFridman/VertxAndWebSockets谢谢雷。

Since you are blocking your websocket handler until it receives a reply for the sent message to the EventBus , which will not, in fact, be received until the set up delay of 10s laps, you certainly will get warning since the reply handler of the event bus will timeout -> Message sent but no response received before the timeout delay. 由于您一直在阻止websocket处理程序,直到它收到对发送到EventBus的已发送消息的答复为止,实际上,直到设置的延迟10秒 ,才会收到该消息,因此您肯定会收到警告,因为事件的答复处理程序总线将超时 - >消息发送,但没有在超时延迟之前接收到的响应。

Actually I don't know if you are just experimenting the Vert.x toolkit or you are trying to fulfill some requirement, but certainly you have to adapt your code to match in the Vert.x spirit: 实际上,我不知道您是否只是在试验Vert.x工具包,还是想满足某些要求,但是当然您必须调整代码以符合Vert.x精神:

  • First you should better not block until a message is received in your websocket handler, keep in mind that everything is asynchrounous when it comes to Vert.x . 首先,最好不要阻塞,直到在Websocket处理程序中收到消息为止,请记住,谈到Vert.x时,一切都是异步的。
  • In order to sleep for some time, use the Vert.x way and not the Thread.sleep(delay) , ie vertx.setTimer(...) . 为了睡眠一段时间,请使用Vert.x方式,而不要使用Thread.sleep(delay) ,即vertx.setTimer(...)

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

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