Skip navigation

Whether you're looking to get started with Vert.x or wanting to deploy your favorite Vert.x application to the cloud, there's really no better place to do this then on OpenShift. In this blog I'll go through how simple it is to create a Vert.x application, how to deploy changes, and how to run different types of Vert.x verticles (different languages) all on OpenShift.

 

Note: If you don't have an OpenShift account you can Sign Up (it's free) and go through Getting Started with OpenShift to install the command line tools which I will use to create the application.

 

Create a Vert.x Application

 

Creating a Vert.x application on OpenShift is...well...easy. All that is needed is one simple command and OpenShift will do the rest

rhc app create myapp vertx














 

When this operation completes the application can bee seen by going to (depending on your domain) http://myapp-<domain>.rhcloud.com. And that's it ! The Vert.x application is alive and well in the cloud. Let's take a look at the application that OpenShift created.

 

The Default Application

 

When you create a Vert.x application on OpenShift, it clones it into a local git repository on the local machine with the name of the app, in the case above myapp. If we look inside this directory we'll see a server.js file. This is the default Vert.x application which is defined by the Vert.x OpenShift Cartridge. And this is the application that we see when we access our app URL. If we take a look at this file

var vertx = require('vertx');
var container = require('vertx/container');
var console = require('vertx/console');

var ip = container.env['OPENSHIFT_VERTX_IP'] || '127.0.0.1';
var port = parseInt(container.env['OPENSHIFT_VERTX_PORT'] || 8080);

vertx.createHttpServer().requestHandler(function(req) {
  var file = req.path() === '/' ? 'index.html' : req.path();
  req.response.sendFile('webroot/' + file);
}).listen(port, ip, function(err) {
    if (!err) {
      console.log('Successfully listening on ' + ip + ':' + port);
    } else {
      console.log('Could not bind to ' + ip + ':' + port + '. Error: ' + err);
    }
});














we'll see it's a simple JavaScript app that serves static content from the webroot directory (line 10). It listens on the IP and port based on the OpenShift environment variables (line 5-6). However you'll also notice it will use 127.0.0.1 and 8080 if those environment variables do not exist. This allows the application to be run locally and on OpenShift without any changes.

 

Deploying Changes

 

Deploying changes to OpenShift is pretty simple. In this example we'll change the request handler of the application to write 'Hello' if it encounters a request with /hello as the path (line 2-3)

vertx.createHttpServer().requestHandler(function(req) {
  if (req.path() === '/hello') {
    req.response.end('Hello');
  } else {
    var file = req.path() === '/' ? 'index.html' : req.path();
    req.response.sendFile('webroot/' + file);
  }
....














 

We save our changes and to deploy them we commit them to our local git repo and then push to OpenShift

git commit -am 'First change'
git push














 

After this completes we see our changes are now live by going to http://myapp-<domain>.rhcloud.com/hello and seeing that 'Hello' is now displayed. Next let's show how to replace the existing JavaScript app with a Java one.

 

Running Vert.x Verticles

 

The OpenShift cartridge does it's best to support all the various options that are available when it comes to running Vert.x verticles. The file which controls these options is located in the configuration/vertx.env file of the application. So for this example if we want to run a Java verticle called Server.java instead of the JavaScript one, we would just modify the vertx_app value from

export vertx_app=server.js






















to

export vertx_app=Server.java



















And this can be done for any language. So if you wanted to run a ruby app named foo.rb, just change it to point to this file instead and Vert.x and OpenShift will take care of the rest.


Here's an example Server.java which is basically the same app above except it will say "Hello Java" instead of just "Hello" if you want to test it out. Just add to it to your app directory, commit all changes, and push to OpenShift

wget https://gist.githubusercontent.com/nscavell/9958687/raw/9db1e9f34fac56da3da23c82aac6dba2f023a9a1/Server.java
git add configuration/vertx.env Server.java
git commit -m 'Run Java instead !'
git push





















You can have a look at the Vert.x OpenShift Cartridge page for all the various options the cartridge supports for running Vert.x apps, like running modules, running multiple instances of a module, etc.


What now ?

 

As you can see it's super easy to get developing with Vert.x on OpenShift and there's a lot to explore from here, like adding Mongo and setting up SockJS to do real-time stuff. If this sounds interesting to you, then look for my next blog post where I go through creating a real-time Vert.x application on OpenShift. In the meantime, I encourage you to take the cartridge for a spin and report back any issues or feedback you may have here. Until next time, happy coding !

Filter Blog