Getting started with Vert.x and Java on OS X

Post to Twitter

Vert.x is a fairly new project that has already reached 1.0 status and has a growing community. According to their website Vert.x is: Effortless asynchronous application development for the modern web and enterprise. If your familiar with Node.js then you will most likely feel right at home with Vert.x. Vert.x also allows you to use a favorite language like Java, Ruby, Groovy, or JavaScript with support for Scala and Python coming in the future. Today I’ll go over a very simple demo on how to try out Vert.x using Java.


Vert.x currently uses Apache Ant and I was curious to start using Vert.x but without having to setup Ant, etc. What follows are the steps I took to get the WebSockets example working on my OS X machine without using Ant.

First off, you need Java JDK 1.7 installed and that means if your on OS X then you need to be running Lion 10.7.2+. See my previous post on how to install the JDK 1.7 on OS X.

At some point it looks like the Vert.x artifacts will be on Maven Central, but until then you can download the binaries here. Once you’ve extracted the files to a convenient location on your hard drive set your path to point to the Vert.x bin folder. My .bash_profile file looks like this in my case (if you don’t have .bash_profile check for a file called .profile):

export PATH=/Users/chad3925/vert.x-1.0.final/bin:$PATH

From the Terminal you should be able to then run the following command:

$ vertx version

Using some of the example code I modified it slightly:

package com.giantflyingsaucer.websocketsexample;

import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.ServerWebSocket;
import org.vertx.java.deploy.Verticle;


public class WebsocketsExample extends Verticle {

  public void start() {
    vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() {
      public void handle(final ServerWebSocket ws) {
        if (ws.path.equals("/myapp")) {
          ws.dataHandler(new Handler<Buffer>() {
            public void handle(Buffer data) {
              ws.writeTextFrame(data.toString()); // Echo it back
            }
          });
        } else {
          ws.reject();
        }
      }
    }).requestHandler(new Handler<HttpServerRequest>() {
      public void handle(HttpServerRequest req) {
        if (req.path.equals("/")) req.response.sendFile("ws.html"); // Serve the html
      }
    }).listen(8080);
  }
}

Grab a copy of the ws.html file from the GitHub repo and save it to where you plan to keep your resulting compiled binaries.

I used Netbeans 7.1 to create a new Maven application called WebsocketsExample and then manually brought in the dependancies (since they are not yet on Central when this article was written). My pom.xml looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.giantflyingsaucer</groupId>
  <artifactId>WebsocketsExample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>WebsocketsExample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.vertx</groupId>
      <artifactId>vertx-core</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.vertx</groupId>
      <artifactId>vertx-platform</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty</artifactId>
      <version>3.4.2.Final</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-lgpl</artifactId>
      <version>1.9.7</version>
    </dependency>
  </dependencies>
</project>

Note: Not all the dependancies in the above file are required for this example, I added some for additional future experimentation.

Do a clean build on the project and then drop back into the Terminal. Depending on your setup the following will differ but this is how I ran the example (from the Maven created target folder):

$ vertx run com.giantflyingsaucer.websocketsexample.WebsocketsExample -cp classes

Go to the following address in a web browser that supports websockets: http://localhost:8080

At this point if all went well you should have a server running that will talk to the web browser with websockets. You can find plenty more examples including ones for Javascript, Ruby, etc. here: https://github.com/purplefox/vert.x/tree/master/src/examples

Make sure to check out the Vert.x Google Group as well as their blog. They have some initial benchmarks posted there and are quite impressive so far.

Post to Twitter

This entry was posted in Java, Online Games, Vertx. Bookmark the permalink.

3 Responses to Getting started with Vert.x and Java on OS X

  1. Pingback: Questa settimana in Grails (2012-20) - luca-canducci.com - Il blog di Luca Canducci: notizie, tips e nuove tecnologie dal mondo dell’IT.

  2. Pingback: An Army of Solipsists » Blog Archive » This Week in Grails (2012-20)

  3. Nice intro! Thanks for sharing.

    I’ve recently created a smilar entry for Maven/Eclipse – Includes eclipse launcher and an internal repo (until deps are available in a public maven repo).

    http://riaconnection.wordpress.com/2012/07/19/maven-2-base-project-for-vert-x-io/

Comments are closed.