eclipse-vertx / vert.x

Vert.x is a tool-kit for building reactive applications on the JVM
http://vertx.io
Other
14.26k stars 2.07k forks source link

VerticleGroup is a list of verticle that share the same context. #3956

Open wang007 opened 3 years ago

wang007 commented 3 years ago

when you haved a list of verticle that hope to run on the same context to avoid data race, you can used VerticleGroup

Use cases

  public class CompositeVerticleGroup extends AbstractVerticleGroup {

    @Override
    public List<Verticle> verticles() {
      return Arrays.asList(new VerticleA(), new VerticleB(), new VerticleC());
    }

    static class VerticleA extends AbstractVerticle {}

    static class VerticleB extends AbstractVerticle {}

    static class VerticleC extends AbstractVerticle {}

  }

 vertx.deployVerticle(CompositeVerticleGroup)

VerticleA, VerticleB, VerticleC and CompositeVerticleGroup will run on the same context.

Contribution

yes, I haved create an pr

tsegismont commented 3 years ago

The Vert.x model is to design independent units of processing as verticles and exchange messages over the eventbus to avoid multithread programming issues.

If you need three components to be invoked on the same Vert.x context, why not deploying a single verticle and creating the components inside?

wang007 commented 3 years ago

As the title says, we want a group of Verticle to run on the same context. This is useful when you have a large number of Verticle. I've already discussed this feature with Vietj on Discord, and VietJ says it is an interesting approach.

wang007 commented 3 years ago

For solving this issue, VerticleGroup is useful https://github.com/vert-x3/vertx-web/issues/1909

vietj commented 3 years ago

I think it would be simpler to allow a verticle to inherit the context of its parent when it is deployed, e.g new DeploymentOptions().setInheritParentContext(true)

This would be rejected when the number of instances is > 1

wang007 commented 3 years ago

@vietj I think they are different, and they can exist together. InheritParentContext has some restrictions, such as whether it is the same Work Pool and the configuration associated with the Work Pool. But the verticleGroup doesn't need to be concerned because these options must be the same.

wang007 commented 3 years ago

VerticleGroup is just a functional class; it doesn't change anything. The user can choose to use it or not, but it works well