Vert.x - the Node.js for Java in the Cloud

Vert.x - the Node.js for Java world

As of August 2012, information in this blog post is no longer current. OpenShift provides native support for JDK7 (https://openshift.redhat.com/community/blogs/testing-the-new-features-of-java-7-in-the-cloud-with-a-paas). There is also work on new Vert.x Quickstart. This blog post may be however useful for general introduction to the steps necessary to get Vert.x installed, so we keep it public and accessible. 
 
Thanks for understanding.

What is Vert.x

Vert.x is "Effortless asynchronous application development for the modern web and enterprise". You can think of Vert.x as an alternative to Node.js with the difference it runs on top the well known JVM and provides support for many languages. By default the programs can be created in Ruby, Groovy, JavaScript or plain Java.# Vert.x - the Node.js for Java world

As of August 2012, information in this blog post is no longer current. OpenShift provides native support for JDK7 (https://www.openshift.com/blogs/testing-the-new-features-of-java-7-in-the-cloud-with-a-paas). There is also work on new Vert.x Quickstart. This blog post may be however useful for general introduction to the steps necessary to get Vert.x installed, so we keep it public and accessible. 
 
Thanks for understanding.

What is Vert.x

Vert.x is "Effortless asynchronous application development for the modern web and enterprise". You can think of Vert.x as an alternative to Node.js with the difference it runs on top the well known JVM and provides support for many languages. By default the programs can be created in Ruby, Groovy, JavaScript or plain Java. The framework is polyglot from the ground up and allows to mix and match different languages in one application.

Even though the project is still very young, the community is rapidly growing and I expect a bright future for the project. Let's see how easy it is to get vert.x running on OpenShift.

Requirements

To get Vert.x running it's required to have DIY gear created, JDK7 (any possible distribution) and Vert.x itself downloaded.

DIY

Create a DIY gear either using the web interface or using command-line. To do it on the command-line

rhc app create vertx diy-0.1 -l <login name>

once created, you will be presented with a Git repository URL, that looks like

ssh://<some hash>@<application name>-<domain name>.rhcloud.com/~/git/<application name>.git/

from that we need SSH url of the OpenShift application

<some hash>@<application name>-<domain name>.rhcloud.com

JDK7

Vert.x requires Java7 that is not yet available on OpenShift. It should come in a near future with the release of Red Hat Enterprise Linux 6.3. But for now, it's required to deal with that manually.

Let's download JDK7 from Oracle and install it. Go to JDK download page, click "Accept License Agreement" and download this distribution

Linux x64 (64-bit)   77.67 MB      jdk-7u4-linux-x64.tar.gz

once downloaded, upload it to OpenShift using SCP

scp jdk-7u4-linux-x64.gz <SSH url>:~/<application name>/data/jdk.tar.gz

and connect to OpenShift using SSH

ssh <SSH url>

now let's install JDK

cd $OPENSHIFT_DATA_DIR
tar xf jdk.tar.gz
rm jdk.tar.gz
mv jdk1.7.0_04 jdk

JDK is installed and available through ${OPENSHIFT_DATA_DIR}jdk/bin/java.

Vert.x

Installing Vert.x is as easy as installing Java. Run these command while still connected to OpenShift using SSH

cd $OPENSHIFT_DATA_DIR
wget http://vertx.io/downloads/vert.x-1.0.1.final.tar.gz
tar xf vert.x-1.0.1.final.tar.gz
rm vert.x-1.0.1.final.tar.gz
mv vert.x-1.0.1.final vert.x
 
cd vert.x
sh install-rhino.sh

Now there is Vert.x available through ${OPENSHIFT_DATA_DIR}vert.x/bin/vertx.

You can disconnect from OpenShift.

Setting up an application

Vert.x code

In the root of your repository create a simple application. To a file server.js save this content

load('vertx.js');
 
var config = vertx.config;
 
vertx.createHttpServer().requestHandler(function(req) {
  req.response.end("Hello World!");
}).listen(config.port, config.ip);

OpenShift's start hook

Edit .openshift/action_hooks/start

#!/bin/bash
 
PATH="${OPENSHIFT_DATA_DIR}vert.x/bin:${OPENSHIFT_DATA_DIR}jdk/bin:$PATH"
JAVA_HOME="${OPENSHIFT_DATA_DIR}jdk"
 
cd $OPENSHIFT_REPO_DIR
 
echo "{\"ip\":\"${OPENSHIFT_INTERNAL_IP}\",\"port\":\"${OPENSHIFT_INTERNAL_PORT}\"}" > .openshift/vertx.config
 
nohup bash -c "exec 'vertx' 'run' 'server.js' -conf .openshift/vertx.config &> ${OPENSHIFT_DIY_LOG_DIR}vert.x.log" &> /dev/null &
echo $! > .openshift/application.pid

This hook starts up the Vert.x application. It is a bit more complex. First it sets up the paths to make JDK7 and Vert.x available. Then it creates configuration that contains the IP address and port the application is supposed to bind to. The is needed, because Rhino (which is used for JavaScript support in Vert.x) does not provide access to environment variables directly. Then the application is started.

OpenShift's stop hook

Edit .openshift/action_hooks/stop

#!/bin/bash
 
killall java

The stop hook just request all java application to shutdown. Vert.x is well behaved and does it just fine.

Deploy

The application is ready, Vert.x is installed ... it's time to deploy the application.

git add -A .
git commit -m "First commit"
git push origin master

Once the git repository is pushed. Open your browser and navigate to the URL of your application.

Conclusion

Vert.x provides viable alternative to the Node.js framework. Even though Vert.x is event driven as Node.js is, it provides robust support for multitasking and threading utilizing the features of the JVM. Also it is possible to access vast array of libraries already available fot Java and the JVM platform.

Tags:

Great post! If server is writing in java so can you show me the OpenShift's start hook?

Hi, it would really depend on what application/framework you use.

As an inspiration you can use this one

https://github.com/marekjelen/openshift-jetty/blob/master/start.sh

kill cat $OPENSHIFT_REPO_DIR/.openshift/application.pid

and the pidfile should probably go to $OPENSHIFT_RUN_DIR

Unfortunately in the time of writing there this solution did not work, so I had to revert to the killall java.

The directory ant is downloaded from only contains the latest, as they went up a minor version, the install-rhino script breaks currently, something for the vert.x guys to fix but might be usefull for anybody here that tries this

Thanks for the information. When the licensing problem with rhino will be solved and it will be once agin bundled in vertex I intend to write a updated solution.

This walk through didnt say where to save the server.js file. is it in the root of the repo or in the DIY folder where you have the ruby server and the index.html file

Thanks for the comment. This is very true. Article updated.

I try to create a TCP via vertx socket and deploy to openshift

var server = vertx.createNetServer();

When telnet to OPENSHIFT_INTERNAL_IP and OPENSHIFT_INTERNAL_PORT it works OK but how can i access from outside?

Try telnet URL of application and port 80 or 8080 but it do not work as TCP server.

Hi, Locally i use the 1.0.1 version of vert.x and it works ok (while it doesnt on openshift), i upgraded to the 1.1.0 version, which unfortunatley doesnt work (connecting to mongodb instance) both locally and on openshift

Your post is very usefull. Thanks writing it.

I have a problem with my vertx 1.1.0 application that uses websockets. Locally it works fine, but on OpenShift websockets are filtered and the handshake is not done. Is there a config to enable Websockets in OpenShift ?