Over a million developers have joined DZone.

Getting Started With Vert.x and Java on OS X

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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:

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 {
    }).requestHandler(new Handler<HttpServerRequest>() {
      public void handle(HttpServerRequest req) {
        if (req.path.equals("/")) req.response.sendFile("ws.html"); // Serve the html

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">

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:

$ vertx run com.giantflyingsaucer.websocketsexample.WebsocketsExample -cp ~/NetBeansProjects/WebsocketsExample/target/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.


The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!


Published at DZone with permission of Chad Lung, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}