vert.xvertx-verticle

VertX multiple Worker Instances processing same message


I have a simple Vertx worker vertical with 4 instances for scaling as defined below. When multiple requests come, I was expecting that each worker instances will process individual request concurrently (4 requests at a time).

Vertx vertx = Vertx.vertx();
DeploymentOptions deploymentOptions = new DeploymentOptions()
                                                     .setWorker(true)
                                                     .setInstances(4);
vertx.deployVerticle(MailVertical.class.getName(), deploymentOptions);

some code to plumb incoming mail message to publishing method.

// This is executed once per incoming message    
this.vertx.eventBus().publish("anAddress", messageString);

Vertical Consumer code to log incoming mail message

public class MailVertical extends AbstractVerticle {

private static final Logger logger = LoggerFactory.getLogger(MailVertical.class);

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

    logger.info("Welcome to Vertx: MailVertical.");

    vertx.eventBus().consumer("anAddress", message -> {

        String msg = message.body().toString();
        
        for(int i=0;i<50;i++){
            logger.info(msg);
        }
        try {
            updateStatusInDB(msg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    });
}

However, I am observing that each request is processed by all 4 instances somewhat concurrently i.e. if 1 request come in, total 4 processing events occur = 200 log messages.

...
...
[vert.x-worker-thread-7] INFO com.vertx.mailproject.MailVertical - {"msg":"sample message for app-2123x mail notification","appname":"app-1","msgid":"64fd684b-45a8-48c7-9526-4606d6adc311"}
[vert.x-worker-thread-6] INFO com.vertx.mailproject.MailVertical - Msg: sample message for app-2123x mail notification updated to SENT in DB.
[vert.x-worker-thread-7] INFO com.vertx.mailproject.MailVertical - Msg: sample message for app-2123x mail notification updated to SENT in DB.
[vert.x-worker-thread-4] INFO com.vertx.mailproject.MailVertical - Msg: sample message for app-2123x mail notification updated to SENT in DB.
[vert.x-worker-thread-5] INFO com.vertx.mailproject.MailVertical - Msg: sample message for app-2123x mail notification updated to SENT in DB.

Any suggestion what am I doing wrong here? Or is this expected.

vertx-core = 4.2.3


Solution

  • Yes this is indeed a correct behavior. You are starting 4 worker threads and then registering all of them against the same address "anAddress".

    Publish Subscribe pattern is being used here. With the method publish() all the registered consumer/handler will receive this event. see Publish / subscribe messaging

    If you want only one of worker to receive this event, then use Point-to-point and Request-Response messaging. Basically replace publish() with send()

    But looking at your code, I would suggest instead of using worker verticle use executeBlocking in standard verticle