java - 多线程 Vert.x 每秒处理数千个连接

标签 java vert.x

我正在构建一个像服务器一样的信使,我对 vert.x 做了一些这样的更改

vertx.deployVerticle(MainDeployment.class.getCanonicalName(),res -> {
          if (res.succeeded()) {

                Logger.Log("Deployed");

              } else {
                Logger.Log(res.cause().getMessage());
              }
            });

    server.requestHandler(router::accept).listen(port);


 public void createContext(String path,MyHttpHandler handler){
    //handlersMap.put(path.split("\\/")[1], handler);
    if(!path.endsWith("/")){
        path += "/";
    }

    path+="*";

    map.put(path, handler);
}

这是我的 MainDeployment 类

 public class MainDeployment extends AbstractVerticle{



  @Override
  public void start() throws Exception {

      //GUI.display("Sub Verticle Has Deployed");

    // Different ways of deploying verticles

    // Deploy a verticle and don't wait for it to start

   for(Entry<String, MyHttpHandler> entry : MyVertxServer.map.entrySet()){
       MyVertxServer.router.route(entry.getKey()).handler(new Handler<RoutingContext>() {

            @Override
            public void handle(RoutingContext ctx) {

                String[] handlerID = ctx.request().uri().split(ctx.currentRoute().getPath());

                String suffix = handlerID.length > 1 ? handlerID[1] : null;
                entry.getValue().Handle(ctx, new VertxUtils(), suffix);

            }
        });
   }

  }
}

文档说

 Vert.x guarantees that a particular verticle instance is never executed by more than one thread concurrently. This gives you a huge advantage as a developer, since you can program all your code as single threaded

由于每个verticle都有自己的线程并且是单线程的,所以如果我的verticle上传文件,它不能同时处理2个上传,还是我误解了NIO的概念?

最佳答案

我想您应该更深入地研究 Vert.x 文档,以更熟悉支持 API 和设计。

我强烈建议的官方文档如下:

Even though a Vertx instance maintains multiple event loops, any particular handler will never be executed concurrently, and in most cases (with the exception of worker verticles) will always be called using the exact same event loop.

您尝试实现的文件上传过程被视为阻塞操作,因此应该执行:

  • 通过使用阻塞 API Vertx#executeBlocking
  • 或者在可通过 Vertx#createSharedWorkerExecutor 调用获取的工作池内

这两种方式都会导致事件循环线程不会因阻塞操作而停止(我说的是事件循环线程但我们实际上讨论的是所有事件循环线程,因为它们可能不止一个)。

关于java - 多线程 Vert.x 每秒处理数千个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929285/

相关文章:

java - 如何调用带参数的私有(private)方法

java - Eclipse Vert.x : parsing java. 与 jackson 的时间日期

java - 如何在 router.route ("/api/*").handler 中使用协程?

java - 我如何在单元测试中使用 Mockito 或任何 Mocking 框架来模拟 Guice 注入(inject)?

java - 使用 vertx 在 mongoDB 中批量写入

java - JJWT 生成的 token 具有无效签名

Java:为什么这里没有自动装箱?

java - 是否有实现 JCR API 的 ModeShape Java 客户端?

java - 为什么两种技术中的相同代码表现不同

java - Vertx 没有绑定(bind)路由器