<img height="1" width="1" src="https://www.facebook.com/tr?id=1076094119157733&amp;ev=PageView &amp;noscript=1">

Getting started with vert.x

Posted by Jan Machacek on Wed, May 30, 2012

vert.x is an exciting project with tagline that promises "Effortless asynchronous application development for the modern web and enterprise". As this is simply an introduction to vert.x and small preview of what its Scala API might look like, I will only tempt your interest by saying that vert.x implement multi-reactor pattern: in other words, it maintains multiple event loops; each event loop thread runs you run your code in verticles. (If you are familiar with akka, you might imagine the verticles to be the actor instances.)

Right. Let's get the beast going with Java server and Scala client; and let's use NetSockets to get them talking to each other. First, we must download & build vert.x. Clone my Scala support from https://github.com/janm399/vert.x/tree/scala. Once cloned, you will need to make sure your machine has all the required tools to build vert.x; you'll need:

I won't go into the details of how to install JDK 1.7 and Ant; for all non-Ruby people, you will need to have Ruby installed and then execute sudo gem install yard. Once java runs JDK 1.7, ant runs Apache Ant and you have the yard gem, you're ready to build vert.x.

In the directory you cloned vert.x to, simply run ant dist. This will assemble the vert.x distribution to target/dist-build/vert.x-version. As the final step before we can run any vert.x servers and clients, you must run the install-rhino.sh script, which will grab the latest Rhino's JavaScript libraries for Java.


Right. So you've built vert.x; time to try out the polyglot NetSocket-based code. We will run the EchoServer implemented in Java and we'll run the EchoClient in Scala. Head to the src/examples/java and run ant to build the example code in Java; then navigate to src/examples/scala and run ant again to build the Scala example code. Now that all is compiled, we can use the vertx-dev script in src/examples. So, let's run the two applications.

In src/examples/java, run ../vertx-dev run org.vertx.java.examples.echo.EchoServer -cp classes. This will start (a single instance of) the EchoServer.
Next, in another shell, in src/examples/scala, run ../vertx-dev run org.vertx.scala.examples.echo.EchoClient -cp classes, which will start (a single instance of) the EchoClient.


Wonderful--we have a single instance of the server & client. Now, let's see the true power of vert.x and let's run 8 instances of the server and client. All we need to do is to include the -instances 8 on the command line when we start the server and the client.

So, modifying the instructions form above, in src/examples/java, run ../vertx-dev run org.vertx.java.examples.echo.EchoServer -cp classes -instances 8. This will start eight instances the EchoServer.
Next, in another shell, in src/examples/scala, run ../vertx-dev run org.vertx.scala.examples.echo.EchoClient -cp classes -instances 8, which will start eight instances of the EchoClient.

You will notice that the input and output will be intermingled between the instances. That is interesting, but what's even more interesting is that we successfully integrated Java server and Scala client; running in different JVMs. Even better, we successfully scaled the application by running 8 instances of the server and client.

The code

Before I let you go to try it out for real on your computers, let's take a look at the Scala client. It is very early days for the Scala support in vert.x, I will keep improving the convenience of using the sometimes clunky Java APIs. Anyway, here it is:

class EchoClient extends Verticle with NetSockets {

  def start() {
    vertx.createNetClient.connect(1234, "localhost", {
      socket: NetSocket =&gt;

      socket.dataHandler(new Handler[Buffer] {
        def handle(buffer: Buffer) {
          System.out.println("Net client receiving: " + buffer)

      for (i < - 0 until 10) {
        val str = "hello" + i + "\n"
        println("Net client sending: " + str)
        socket.write(new Buffer(str))

That's all I have

... for this post. I will keep improving the Scala APIs and I will report all improvements here. Now, all you intrepid developers, get the source code, get it working; if you get lost, ping me on @honzam399 or RTFM!

Posts by Topic

see all

Subscribe to Email Updates