Reza
Reza

Reputation: 339

MultiThreading Vert.x to handling thousands of connections per sec

I'm building up a messenger like server, I have made some changes to vert.x like this

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);
}

and here my MainDeployment class

 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);

            }
        });
   }

  }
}

the documentation says

 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

since every verticle has its own thread and will be single threaded , so if my verticle upload files , it can not process 2 uploads at same time or am i misunderstanding the concept of NIO?

Upvotes: 0

Views: 915

Answers (1)

tmarwen
tmarwen

Reputation: 16394

I guess you should dig deeper through the Vert.x documentation to get more familiar with the backing API and design.

The official documentation, which I highly advice, states the following:

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.

The file upload process that you are trying to implement, is considered a blocking operation thus should be executed:

  • Either through using the blocking API Vertx#executeBlocking
  • Or within a worker pool that can be obtained through Vertx#createSharedWorkerExecutor call

Both way will result in the event-loop thread not being halt with blocking operations (I'm stating the event-loop thread but we are actually talking about all the event-loop threads as they may be more than one).

Upvotes: 1

Related Questions