Monday, 26 December 2016

A Vert.x Perception - Deployment and DI

My deployment model is quite like:

The VertxGuiceMain is the main class which launches the other 3 verticles with the necessary configurations made available with DeploymentOptions JSON object.

Now, the VertxGuiceMain class which itself too is a verticle is being deployed using VerticleFactory implementation of Vert.x along with the help of Launcher.

VertxGuiceMain extends GuiceVertxLauncher which does all the necessary configurations.

We have here a custom implementation of VerticleFactory to launch VertxGuiceMain and there we have used Google Guice Injector and configured Guice modules (Abstract Module implenetations) alongside.

The getModules() method which we have overridden in VertxGuiceMain provides the necessary Guice Module implementations which contains the bindings of the types with the instances.

To get a more detailed idea please refer to codebase at: GITHUB

The VertxGuiceMain constructor annotated with @Inject has the necessary dependencies and those were injected (or autowired in Spring sense) by Guice dependency injection because of :

1) Configuration described above and
2) TheVertxGuiceMain verticle being launched with the Guice injector instance using createVerticle() method of the Verticle Factory.

We have used here Gradle Application Plugin and configured the mainclass to be VertxGuiceMain.

The other three verticles which were deployed from VertxGuiceMain are also being launched using Guice injector instance and so the DIof the dependencies are working as expected.

Please note all throughout we are using the same injector instance.

The VertxGuiceMain class utilizes the lifecycle methods for various configurations which are provided by Launcher class (it implements VertxLifecycleHooks)

I found dependency injection with Google Guice to be very convenient as wiring of the required dependencies are configured within the AbstractModule implementation. For more specific and varied configuration Guice does provide a vivid range of tools like Provider Implementation, Named Annotations etc.

Here,I have used Guice at a very basic level as my intention is to integrate Guice with Vert.x while deploying multiple Verticles.

I found this model for multiple verticle deployment to be very convenient as the master verticle provides a centralized approach for easy maintenance and debugging.

View Subhankar Paul's profile on LinkedIn

No comments:

Post a Comment