How can I get a reference to my deployed verticle after deploying it with Vertx?
Asked Answered
V

4

6

I have a service called TestService which extends AbstractVerticle:

public class TestService extends AbstractVerticle {
  @Override
  public void start() throws Exception {
    //Do things
  }
}

I then deploy that verticle with vertx like this:

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(TestService.class.getName());

How can I get a reference to my deployed TestService after vertx instantiates it?

Varipapa answered 19/9, 2016 at 18:44 Comment(0)
P
9

You should use an alternative method for deployment:

vertx.deployVerticle(TestService.class.getName(), deployment -> {
  if (deployment.succeeded()) {
    // here is your ID
    String deploymentId = deployment.result();
  } else {
    // deployment failed...
  }
});

If you're just interested in listing all deployed verticles then you can just request the list of ids:

vertx.deploymentIDs()
Patriarchate answered 20/9, 2016 at 7:9 Comment(1)
is there a way to get reference to the vertical object itself when its deployed? Like: TestService ts = vertx.byId("..");Casein
N
4

I know this question is old however it may be useful to someone to see an example of how to do this.

You will often see examples for deployment like this from vertx-examples
this follows as asynchronous micro service framework, however its really easy to get the reference as the method 'deployVerticle' (see line 29 in the link) will take an instance as shown in the simple example below, and u can get a reference in the call back as shown.

example in Kotlin easily translate to java

MyVert: io.vertx.core.AbstractVerticle() {  
    override fun start() {  
    // init  
     } 
    fun someFunction() {
    }
}  

fun main() {
 val vertx = Vertx.vertx()
 val myVert = MyVert()

 vertx.deployVerticle(myVert) {
    if(it.succeeded()  ) {
        myVert.someFunction()
    }
    else { println(it.cause().localizedMessage)}   }
}
Numerator answered 19/1, 2019 at 21:28 Comment(0)
H
1

you can get all deployed verticles in current vertx instance by this way

Set<String> strings = vertx.deploymentIDs();
strings
    .stream()
    .map(id -> ((VertxImpl)vertx.getDelegate()).getDeployment(id))
    .forEach(deployment -> System.out.println(deployment.verticleIdentifier() + " " + deployment.isChild() ));
Hornmad answered 17/11, 2021 at 20:53 Comment(0)
P
0

Looks like the vertx API does not allow you to retrieve the Verticle objects once they are deployed. Maybe because verticles can be distributed over multiple JVM.

I needed to do it for unit tests though and I came up with this.
This is unreliable since you rely on VertxImpl (it can break at any vertx version upgrade). But I prefer this over changing production code to be able to test it.

private static <T extends Verticle> List<T> retrieveVerticles(Vertx vertx, Class<T> verticleClass) {
    VertxImpl vertxImpl = (VertxImpl) vertx;
    return  vertxImpl.deploymentIDs().stream().
            map(vertxImpl::getDeployment).
            map(Deployment::getVerticles).
            flatMap(Set::stream).
            filter(verticleClass::isInstance).
            map(verticleClass::cast).
            collect(Collectors.toList());
}

Usage example:

vertx.deployVerticle(new MainVerticle());
// some MyCustomVerticle instances are deployed from the MainVerticle.start
// you can't reach the MyCustomVerticle objects from there
// so the trick is to rely on VertxImpl
List<MyCustomVerticle> deployedVerticles = retrieveVerticles(vertx, MyCustomVerticle.class);
Palindrome answered 5/8, 2022 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.