Performance Zone is brought to you in partnership with:

I've been in the world of software development for 14+ years working with most mainstream programming languages at some point. Chad is a DZone MVB and is not an employee of DZone and has posted 38 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started With Vert.x and Java on OS X

05.23.2012
| 6294 views |
  • submit to reddit

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 {
          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:

$ 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.

 

Published at DZone with permission of Chad Lung, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Herry Johnson replied on Tue, 2012/06/12 - 1:03pm

You're repeating yourself, you won't get anywhere by doing that. If you don't understand my answer then you should be researching it. In stead of remaining in the dark and hoping that an IDE can do all the work for you, I suggest you do what we all had to do: weeks of research and experimentation. Get to the point where you actually understand what you're trying to do.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.