Running Vert.x Applications on Heroku

tl;dr:  heroku create –stack cedar –buildpack https://github.com/tomaslin/heroku-buildpack-vertx-jdk7.git

Vert.x is an event driven framework on the JVM combined with a distributed event bus. It’s basically positioning itself as a polyglot version of Node.js on steroids. It supports Groovy, Ruby, JavaScript and Java.

To learn more, visit the Vert.x website or watch this presentation during last year’s grails exchange by Peter Ledbrook.

Heroku is the best cloud platform for deploying developer projects.

In this post, I will show you how to use a custom buildpack to deploy and run Vert.x applications on Heroku.

Prerequisites

  • You have Heroku set up and configured properly to deploy Java applications. If you don’t, follow the first three steps of this guide to get you started.
  • You have git installed.

Your Vert.x App in the cloud

1. Start with a Vertx application.

Create a new folder in your file system, the name doesn’t really matter but I’ll call mine vertxsample.

Go into your vertxsample directory and create a new file called server.groovy.

Add the following content to server.groovy,


def server = vertx.createHttpServer()

server.requestHandler { request ->

request.response.putHeader("Content-Type", "text/plain")
request.response.end('Hello Heroku')

}.listen( 8080 )

If you have vert.x installed locally, you should be able to test that this works properly by calling

 vertx run server.groovy 

which should display ‘Hello Heroku’ when you navigate to localhost:8080 with your browser.

2. Modify the application to use the Heroku environment.

To run on Heroku, we need to make one change to the file, which is to bind it to the PORT environment variable and use the address ‘0.0.0.0’ instead of localhost.

The code in server.groovy changes from:

.listen( 8080 )

to:

.listen( System.getenv('PORT') as int, '0.0.0.0' )

3. Check into Git.

At this point, your application is ready to be deployed. To do so, we need to first add it to a git repository. Open a console and type:

git init

git add .

git commit -am "initial commit"

This would have created a repository ready to be pushed into Heroku.

4. Create a Heroku Cedar stack with the Vertx Buildpack

Heroku provides the ability to use custom buildpacks to set up an environment. Let’s create a server on heroku with vert.x already installed.

In a terminal window, type:

heroku create --stack cedar --buildpack https://github.com/tomaslin/heroku-buildpack-vertx-jdk7.git

The buildpack referenced here will install OpenJDK7 and Vert.x so it is ready to use.

You should be able to push your application into the cloud.


git push heroku master

Your application should deploy correctly. You can monitor the process of your build by typing using the heroku log command.

After your process has started, you can browse your new Vert.x powered site by calling heroku open.

Notes:

  • The JDK referenced in the buildpack used an older version of OpenJDK ( as it was really just copied from this buildpack ). You might want to install some other version that is more up to date.
  • Currently, the build pack detects your application as a vert.x project by looking for a file called server.groovy. This logic should be fleshed out more for other languages.
  • Currently, the Vert.x build is copied from github. This can be improved substantially by adding it to an Amazon s3 account.
  • You can specify your own processes by using a procfile.
  • Heroku expects a process to be bound to a given port within 60 seconds to determine a successful build. If you have listening sockets or other tasks, it might make sense to put them into worker threads and have a basic server on the main port.
  • The JDK and Vert.x are only loaded remotely once. On subsequent deployments, it will use a cached version on the server.

12 thoughts on “Running Vert.x Applications on Heroku

  1. Per Wiklander

    So I could either have a wrapper Verticle that loads all my workers and then starts an HTTP server and run that on the free plan (1 dyno) or start my worker verticles (or busmods) in separate worker dynos?

    Reply
    1. Tomas Lin Post author

      To be honest, I’m not quite sure. The spike here was to get JDK and Vert.x running in Cloud Foundry, but haven’t really looked at how best this architecture fits within Heroku and Foreman. If anyone has a brilliant idea around this, would like to find out / co-explore.

      Reply
  2. johnrellis

    Nice. I’m curious, where would you see vertx fitting in. Would it be a replacement for frameworks like grails (taking it as an example), particularly when using nosql DB’s?? Or do you see it as complimenting grails in some way??

    nodejs seems really geared towards creating restful based webservices, maybe that is its niche on the JVM??

    I’m still learning this async server stuff so I am still trying to figure out where it fits in.

    Just curious, really, looks pretty FREAKING sweet🙂

    Reply
  3. Jochen

    Any idea what could have gone wrong?
    I’m still trying to understand Heroku, so excuse my ignorance
    2012-09-19T06:13:12+00:00 heroku[web.1]: Stopping process with SIGKILL
    2012-09-19T06:13:14+00:00 heroku[web.1]: Process exited with status 137
    2012-09-19T06:13:14+00:00 heroku[web.1]: State changed from starting to crashed
    2012-09-19T06:13:14+00:00 heroku[web.1]: State changed from crashed to starting
    2012-09-19T06:13:17+00:00 heroku[web.1]: Starting process with command `vertx run app.js`
    2012-09-19T06:13:19+00:00 app[web.1]: Cannot find verticle org.vertx.mods.MongoPersistor
    2012-09-19T06:13:19+00:00 app[web.1]: Cannot find verticle org.vertx.mods.AuthManager
    2012-09-19T06:13:19+00:00 app[web.1]: Cannot find verticle org.vertx.mods.WebServer

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s