Vert.x and Guice

1,200 views
Skip to first unread message

Morten Jensen

unread,
Aug 17, 2013, 4:39:04 AM8/17/13
to ve...@googlegroups.com
Hello,

I am new to Vert.x and I'm trying to get up to speed with the framework, which looks really good and ticks a most of the boxes for what we need. I have looked through the group and can see a few questions pertaining to dependency injection using Guice. But haven't found anything conclusive on the actual use of it with Vert.x

I am wondering if anyone has successfully used Guice with Vert.x? If so, would it be possible to give me some guidance on implementation?

Thanks in advance for your assistance.

Best regards,
Morten

Doug Pedrick

unread,
Aug 17, 2013, 5:19:40 AM8/17/13
to ve...@googlegroups.com
Hi Morten -

I have successfully integrated Vert.x with Guice for an application that is in production.  It took a fair amount of trial-and-error to get everything wired up correctly but is working well now.  Note that I have it running in 1.3.1 - not sure what has to change for 2.0.

The basic approach was to implement a VerticleFactory specific to creating Vert.x modules that need to have fields injected by Guice.  That also required an addition to the conf/langs.properties file to tell the runtime about the new factory.  The factory has a method, createVerticle, that Vert.x delegates to when the verticle/module is created.  It's inside of there that I instantiated the module and injected necessary dependencies.  Depending on what kinds of dependencies you have, this part can be straightforward or it can be tricky.  In my case I had to use some reflection sorcery to get at private fields within the VerticleManager class.  Very unfortunate, but necessary in order to get this wired up correctly.

I can probably try to rip out the proprietary pieces in the code and post a generic solution if that would help.

Cheers,
Doug

Tim Fox

unread,
Aug 17, 2013, 5:21:26 AM8/17/13
to ve...@googlegroups.com
On 17/08/13 10:19, Doug Pedrick wrote:
> Hi Morten -
>
> I have successfully integrated Vert.x with Guice for an application that is
> in production. It took a fair amount of trial-and-error to get everything
> wired up correctly but is working well now. Note that I have it running in
> 1.3.1 - not sure what has to change for 2.0.
>
> The basic approach was to implement a VerticleFactory specific to creating
> Vert.x modules that need to have fields injected by Guice. That also
> required an addition to the conf/langs.properties file to tell the runtime
> about the new factory. The factory has a method, createVerticle, that
> Vert.x delegates to when the verticle/module is created. It's inside of
> there that I instantiated the module and injected necessary dependencies.
> Depending on what kinds of dependencies you have, this part can be
> straightforward or it can be tricky. In my case I had to use some
> reflection sorcery to get at private fields within the VerticleManager
> class. Very unfortunate, but necessary in order to get this wired up
> correctly.
>
> I can probably try to rip out the proprietary pieces in the code and post a
> generic solution if that would help.

This would be great to package up as a module for Vert.x 2.0 and put it
in the module registry so people can get going with it straight away :)

Norman Maurer

unread,
Aug 17, 2013, 6:45:45 AM8/17/13
to ve...@googlegroups.com
Agree... This would be a very nice module
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.

Morten Jensen

unread,
Aug 17, 2013, 11:53:22 AM8/17/13
to ve...@googlegroups.com
Hi Doug,

Thanks for your very quick response, much appreciated.

I must admit that I'm new to the world of DI - in the past I would have been a prolific user of factories. Guice seems really nice and simple (like vert.x!) for our needs but it's a bit daunting for me to try out what you have already achieved with Guice. I would +1 on Tim and Norman's suggestion to make a module of your framework if you get the chance.

Btw. I'm currently using vert.x 2.0 - but not to worry; I could downgrade to kick the tyres. The sorts of dependencies we have are currently mostly db related, i.e. Cassandra and possibly MySQL DAOs.

Again, thanks a lot for your response!

Best regards,
Morten

Adrian Gonzalez

unread,
Aug 19, 2013, 9:25:23 AM8/19/13
to ve...@googlegroups.com
Hi,


I chose to use HK2 as it is the DI framework used with Jersey.  It is the same basic idea as Guice, instead of an HK2VerticleFactory you could have a GuiceVerticleFactory.

Tim Fox

unread,
Aug 19, 2013, 12:40:28 PM8/19/13
to ve...@googlegroups.com
Any chance you can push it and register it with the module reg?

Morten Jensen

unread,
Aug 19, 2013, 3:04:01 PM8/19/13
to ve...@googlegroups.com
Hi Adrian,

Thanks for your response, much appreciated. I wasn't even aware of HK2 until your response - I might have a more in-depth look at that. I have in the meantime started using Guice with Vert.x. Not DI for the Verticles themselves but from start() where I pass config to an AbstractModule implementation via constructor and use the Guice injector to pass me back a given implementation of an interface. Works really well because I have all the config and injection stuff (which in turn can be config-based) in one class.

Next step would be to have a look at what's required for this GuiceVerticleFactory... But first I need to get my head around Guice a little bit more ;-)

Thanks.
Morten

Adrian Gonzalez

unread,
Aug 20, 2013, 5:46:51 PM8/20/13
to ve...@googlegroups.com
I have submitted it to the module reg: com.englishtown~vertx-mod-hk2~1.0.1-final

Adrian Gonzalez

unread,
Aug 20, 2013, 6:47:02 PM8/20/13
to ve...@googlegroups.com
I actually started with a guice CDI module before changing over to HK2.  I have pushed the Guice module to github: https://github.com/englishtown/vertx-mod-guice.

The module registry seems to be down now, I will submit it when it is back up.

Hope this helps,
-Adrian

Norman Maurer

unread,
Aug 21, 2013, 12:13:26 AM8/21/13
to ve...@googlegroups.com
Pretty cool.. Thanks
--

Morten Jensen

unread,
Aug 21, 2013, 3:14:52 AM8/21/13
to ve...@googlegroups.com
Hi Adrian,

That's great, thanks a lot! I will have a look over the coming days!

Thanks.
Morten

Tim Fox

unread,
Aug 21, 2013, 3:26:27 AM8/21/13
to ve...@googlegroups.com
On 20/08/13 23:47, Adrian Gonzalez wrote:
> I actually started with a guice CDI module before changing over to HK2. I
> have pushed the Guice module to
> github: https://github.com/englishtown/vertx-mod-guice.
>
> The module registry seems to be down now, I will submit it when it is back
> up.

It should be back up now

Morten Jensen

unread,
Aug 24, 2013, 5:02:28 PM8/24/13
to ve...@googlegroups.com
Hi Adrian,

I had a chance to look at this. However, I cannot seem to get it to work. The GuiceVerticleFactory.java is loaded and runs but always emits:

SEVERE: Class com.englishtown.vertx.guice.BootstrapBinder does not implement Binder (com.google.inject.Module?).

This appears to emanate from the factory:

if (obj instanceof Module) {
...

Here's my (empty) BootstrapBinder:

package com.englishtown.vertx.guice;

import com.google.inject.AbstractModule;

public class BootstrapBinder extends AbstractModule {

  @Override
  protected void configure()  {

  }
}


For reference I use vertx 2.0.1.

Thanks.
Morten

On Tuesday, 20 August 2013 23:47:02 UTC+1, Adrian Gonzalez wrote:

Morten Jensen

unread,
Aug 25, 2013, 4:47:52 AM8/25/13
to ve...@googlegroups.com
Hi again,

Aha, I figured it out... The com.englishtown~vertx-mod-guice~1.0.0-final project pulls in the guice, aopalliance and inject modules into the module when packaged. I also had a dependency on the same libs and as a result they existed twice in the classpath. By changing the maven dependency scope to provided in the vertx-mod-guice project the libs are no longer included when module is packaged and as a result the Binder now works. I may try the opposite (my project with provided dependency scope) for cleaner packaging.

Again, thanks!

Morten

Adrian Gonzalez

unread,
Aug 25, 2013, 6:45:00 PM8/25/13
to ve...@googlegroups.com
Glad you figured it out.  Let me know how it goes or if you have any suggestions.

Adrian Gonzalez

unread,
Aug 25, 2013, 6:47:00 PM8/25/13
to ve...@googlegroups.com
When I use the guice or hk2 modules, I mark the javax.inject dependency as provided in my project.

Pedro Vilaça

unread,
Jan 31, 2014, 9:57:59 AM1/31/14
to ve...@googlegroups.com
Hi guys,

We're using AspectJ to intercept some method invocations to emit some metrics. The pointcuts are already implemented but now we need to publish the events through the event bus. But we don't have access to the event bus inside our aspects. So, I thought that we can try to use a dependency injection mechanism to inject the event bus.

What do you think? Any idea about the implementation?

Thanks,
Pedro

Adrian Gonzalez

unread,
Jan 31, 2014, 12:00:02 PM1/31/14
to ve...@googlegroups.com
For anyone else reading this, there is a PR with some additional discussion related to this: https://github.com/englishtown/vertx-mod-guice/pull/1
Reply all
Reply to author
Forward
0 new messages