Vert.x – the Node.js for Java in the CloudVert.x – the Node.js for Java world

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.

Categories
Java, OpenShift Online
Tags
Comments are closed.