Vertx and Dependency Injection (Guice) Scopes

1,055 views
Skip to first unread message

Jens K

unread,
Oct 19, 2015, 5:42:10 AM10/19/15
to vert.x
Hi,
I'm working on a vertx app using the vertx-guice library listed in vertx-awesome. In my application I have a couple of objects that can be safely shared across several verticles. I marked them as Singleton, but it seems to be that they are initialized two times when I launch vertx with DeploymentOptions.setInstances(2). When I run the example from below, the output is something like

look another instance sandbox.ThreadSafeSingleton@c2c7e5b
look another instance sandbox.ThreadSafeSingleton@74271cea

Is there a way to share objects between verticles using guice DI?

public class Sandbox {
   
public static void main(String[] args){
       
CompletableFuture<Void> future = new CompletableFuture<>();
       
JsonObject config = new JsonObject().put("guice_binder", SimpleModule.class.getName());
       
Vertx.vertx().deployVerticle("java-guice:" + SimpleVericle.class.getName(), new DeploymentOptions().setConfig(config).setInstances(2), result -> {
           
if (result.succeeded()) {
                future
.complete(null);
           
} else {
                result
.cause().printStackTrace();
                future
.completeExceptionally(result.cause());
           
}
       
});
   
}
}


public class SimpleModule extends AbstractModule{


   
@Override
   
protected void configure() {
        bind
(ThreadSafeSingleton.class).asEagerSingleton();
   
}
}


public class SimpleVericle extends AbstractVerticle {


   
@Inject ThreadSafeSingleton threadSafeSingleton;




   
@Override
   
public void start() throws Exception {
       
System.out.println("look another instance "+threadSafeSingleton.toString());
   
}
}


public class ThreadSafeSingleton {
}

Clement Escoffier

unread,
Oct 19, 2015, 6:04:19 AM10/19/15
to ve...@googlegroups.com
Hi,

Did you have a look to https://github.com/englishtown/vertx-guice ? 

Clement
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/13c2ac61-1b28-488e-a5d3-c004d77cad97%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jens K

unread,
Oct 19, 2015, 6:21:53 AM10/19/15
to vert.x
Yes, that is the extension that I'm using and what I meant with "I'm working on a vertx app using the vertx-guice library listed in vertx-awesome.".

Tim Fox

unread,
Oct 19, 2015, 6:26:35 AM10/19/15
to ve...@googlegroups.com
I doubt this is due to Vert.x doing anything strange as Vert.x uses a flat classloader model in the below example.

What happens if you just create two instances of your SimpleVerticle class directly?

Jens K

unread,
Oct 19, 2015, 7:52:40 AM10/19/15
to vert.x
I'm not sure if I understand you correctly: you mean writing something like this

new SimpleVerticle().start();
new SimpleVerticle().start();

? I guess a NPE will be thrown due to a non-initialized ThreadSafeSingleton-instance.

Jens K

unread,
Oct 19, 2015, 8:32:14 AM10/19/15
to vert.x
I think the problem is, that the GuiceVerticleLoader from vertx-guice is creating an injector for each deployed verticle and therefore a singleton is scoped to a verticle. 

jklingsporn

unread,
Oct 20, 2015, 4:55:29 AM10/20/15
to vert.x

Swetha Ramaiah

unread,
Feb 18, 2020, 7:53:12 PM2/18/20
to vert.x
Hello
I am using this vertx-guice library and I have a constructor in my class which extends AbstractModule. How do I go about adding values to constructor in JsonConfig while invoking this DBModule class using guice_binder

class DBModule extends AbstractModule {
 
DBModule(Config config) {
   
// do something
 
}
@Override
   
protected void configure() {
       
//
   
}
}



Thomas SEGISMONT

unread,
Feb 19, 2020, 4:49:26 AM2/19/20
to vert.x
Hi
I'll let someone from the community reply if they are familiar with the vertx-guice library.
But it isn't part of the vertx-stack so if you don't get an answer here you can try to post an issue the vertx-guice repo on GitHub
Regards


--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Sirozha

unread,
Feb 19, 2020, 5:33:23 AM2/19/20
to vert.x

Hello Thomas SEGISMONT
I'm wondering what lib is a part of vertx-stack considering DI?

Thomas SEGISMONT

unread,
Feb 19, 2020, 5:37:50 AM2/19/20
to vert.x
Vert.x does not mandate usage of DI.

There are so community integrations (eg vertx-guice) or we have working examples in the vertx-examples with Spring.

If you're looking for annotation driven, opinionated framework for reactive development with Vert.x, I would recommend to give Quarkus a try.

Le mer. 19 févr. 2020 à 11:33, Sirozha <siro...@gmail.com> a écrit :

Hello Thomas SEGISMONT
I'm wondering what lib is a part of vertx-stack considering DI?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Dan O'Reilly

unread,
Feb 19, 2020, 10:50:14 AM2/19/20
to vert.x
I believe you can use Vertx.currentContext().config() inside your Module to get access to the config you passed to the Verticle. I'm not 100% sure if this is what you're asking, though...If it's not, can you clarify what you're trying to do?

Swetha Ramaiah

unread,
Feb 19, 2020, 1:46:03 PM2/19/20
to ve...@googlegroups.com
Hi 
I found this other implementation that works for my use case

But I was referring to this example here that uses vertx-guice library
https://github.com/ltamrazov/vertx-starter-guide/blob/part-3/src/main/java/com/ltamrazov/vertxstarterguide/ServiceBinder.java

ServiceBinder extends AbstractModule. In my case, I have a constructor for my ServiceBinder(AppConfig appConfig) class.
So when I use the following snippet, I get an error message saying ServiceBinder class could not be instantiated as it expects a constructor.

private static Future<Void> deploy(Vertx vertx, Class verticle, DeploymentOptions opts){
        Future<Void> done = Future.future();
        String deploymentName = "java-guice:" + verticle.getName();
        JsonObject config = new JsonObject()
                .put("guice_binder", ServiceBinder.class.getName());

        opts.setConfig(config);

        vertx.deployVerticle(deploymentName, opts, r -> {
            if(r.succeeded()){
                System.out.println("Successfully deployed verticle: " + deploymentName);
                done.complete();
            }
            else {
                System.out.println("Failed to deploy verticle: " + deploymentName);
                done.fail(r.cause());
            }
        });

        return done;
    }



--
Regards,
Swetha
Reply all
Reply to author
Forward
0 new messages