Embed Vert.x eventbus into your Spring Application with ease.

So, Vert.x 2.0+ now makes it so easy to embed Vert.x into any application. In our case, we wanted to embed Vert.x in a Spring MVC Rest Web Application, so that we can send messages from the Web Application to the eventbus to be picked up by our Vert.x application. All these messages are to be sent from the server side, which is why we did not simply choose to use Vert.x in our client side JavaScript, which is also very easy to do. We actually wanted the sending calls to be done by the back end Services, not even the Controllers of the web app. You know server side services that are re-usable. 😉

Anyway, using Vert.x documentation (Yes, you can learn this from the docs too), I found that it was extremely easy.

Biggest challenge was making sure the correct Vert.x jars are in your classpath of your web application. If you are using Maven, we simply added


        <!-- Vert.x jars to be included-->
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-core</artifactId>
          <version>${vertx.version}</version>
          <exclusions>
              <exclusion>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
              </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-platform</artifactId>
          <version>${vertx.version}</version>
        </dependency>

Those dependencies will pull in other dependencies too. Here is the mvn dependency:tree output for those two dependencies

+- io.vertx:vertx-core:jar:2.0.0-final:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] | +- com.hazelcast:hazelcast:jar:2.5:provided (version managed from 2.6; scope managed from compile)
[INFO] | \- io.netty:netty-all:jar:4.0.2.Final:compile
[INFO] \- io.vertx:vertx-platform:jar:2.0.0-final:compile

Then for getting the EventBus in our code, we wanted to be able to simply @Autowire into any class that we wanted. So we used an @Configuration class that instantiates Vertx and gets the EventBus from it as an @Bean.

Simple as


@Configuration
@PropertySource(value="classpath:vertx.properties")
public class OurAppConfiguration.class {

  private static final String VERTX_HOSTNAME = "vertx.hostname";

  @Autowired
  Environment environment;

  @Bean
  public EventBus eventBus() {
    Vertx vertx = VertxFactory.newVertx(environment.getProperty(VERTX_HOSTNAME));
    return vertx.eventBus();
  }
}

So as you can see we have a vertx.properties file with one property in it. You can, of course, add more properties, like port number and such. But just remember to add that to the call to VertxFactory.newVertx. As we do in the first line of the @Bean method. And just return vertx.eventBus(). Easy as pie.

Remember if you want this EventBus to work with another Vert.x application running somewhere else within your network, to run that one with -cluster, so that clustering works (If you need clustering)

To test this out, we wrote a simple JUnit Test with Spring integrated into it. Here is the code.


// YOU MUST BE RUNNING other Vert.x application with -cluster to run this test.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=[OurAppConfiguration.class])
class TestVertxEventBus {

  @Autowired
  EventBus eventBus

  @Test
  public void testSendingMessageOnEventBusToOtherVertxApplicationHelloWorld() {
    CountDownLatch startSignal = new CountDownLatch(1);
    eventBus.send("test.address", sendMessage, new Handler<Message<String>>(){
        void handle(Message<String> message) {
          assertNotNull(message)
          String response = message.body()
          assertEquals("Well Hello back", response)
          startSignal.countDown()
        }
      }
    )
    startSignal.await()
  }
}

I had to alter some of the code because it was specific to our application, which I am not at liberty to post. But you should be able to make an extremely simple Verticle that listens to the eventBus at “test.address” and responds back. Simple echo, or request-reply works. Vert.x has that in the examples right up front.

So I am not going to post one here. I’ll just say, if you don’t know how to write a real simple Verticle, then don’t try to embed Vert.x in another application just yet. While it is really easy to do, it is still and advanced Vert.x topic

As always, don’t even try to post spam that tries to look like a real response. It is too easy to see and they will never get onto this page.

Be Sociable, Share!

One Response to Embed Vert.x eventbus into your Spring Application with ease.

  1. Kenneth says:

    Thanks for the walkthrough. If you get an IllegalStateException: No ClusterManagerFactory instances found on classpath …

    …you need to configure Hazelcast. This is done by making sure Hazelcast is on classpath and that vertx.clusterManagerFactory is configured to use Hazelcast.

    1) If using maven, add:

    io.vertx
    vertx-hazelcast
    ${vertx.version}

    2) Explicitly set the vertx.clusterManagerFactory system property:
    System.setProperty(“vertx.clusterManagerFactory”, HazelcastClusterManagerFactory.class.getCanonicalName());

    Ref:
    https://groups.google.com/forum/#!msg/vertx/DgFaHV93t3U/EyHv6ZTlcUIJ

Leave a Reply