Recently I’ve heard that Heroku allows deployment of Java applications in it’s Cedar stack. Having no real software idea I decided I’ll give it a try and just configure SOMETHING to work on Heroku.
I have some kind of crush on ReST (I still want to learn it and practice it) so I’ve decided my first application will be a simple hello world using Jersey (JAX-RS implementation). So I started a project on GitHub and started setting up Heroku CLI.
Setting up Heroku CLI
Heroku is now easy to set up. I remember when it required Ruby env and my first encounter with Ruby was not so great (there was no installer of any sort so it was all manual – and I’m lazy) so I gave up on Heroku back then. But now installing it is a breeze – simply go to Heroku Toolbelt and download version for your platform. I have now set it up both on Linux Mint and Windows 7 and it works great.
Setting up project for Heroku
My project is called recaps – it’s supposed to be yet another ticket management system. But that’s irrelevant for now. The most important thing is that in order for Heroku to discover that our application is a Java application pom.xml file must be present. That’s because Heroku uses Maven 3 to build Java applications.
So to actually begin any work with Heroku you need a simple pom.xml file. In my case I’ve added a separate module for the application, so my main pom looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Then there is the web module, just for the sake of splitting projects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
My first attempt at running my sample project concentrated on setting up Jersey. After checking out docs I decided I’ll use Grizzly2 HTTP server just because it’s very easy to set up. I’ve basically pasted the docs tutorial into main Main class. There were some necessary differences, because for example port of the server is dynamically assigned by Heroku. So after very few changes, the resulting Main class looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
That starts up the server and registers some resources with it.
Some Grizzly tricks
Firstly, the GrizzlyServerFactory.createHttpServer method accepts an URI which has to begin witch schema name – in this case http://. Then it has to specify host name, which at first I set up to the application name on herokuapp.com. This didn’t work, but Heroku told me nicely about it: there is a notification in logs that server should bind to 0.0.0.0, so I changed the URI to http://0.0.0.0.
Secondly, the Jersey example waited for a key press to terminate the server. Unfortunately Heroku printed a message which was then passed to the application somehow and the server was terminated. To resolve this, I wrapped the System.in.read() in an endless while loop.
This of course is not the best solution, but it worked, or so it seemed. After a few hours I checked the logs of the application and they said that the application went from up to down. So I’ve decided to switch from Grizzly to Jetty, but that’s out of topic for this post :)
Before pushing all this to Heroku I also added a Procfile:
After pushing to Heroku the application was build and started, and request to http://growing-dawn-9158.herokuapp.com/helloworld produced some output (in this case a simple ‘Message’ message). Job well done :)
Mistakes I’ve made and learnt from
Firstly, I forgot to add the Maven Dependency plugin, but I resolved that one before pushing to Heroku. Without it configured I couldn’t add dependencies to classpath, which in turn produced ClassNotFound exceptions. It didn’t occur to me at first it was required, but then I looked at Heroku example and fixed it easily.
Secondly, I didn’t know that web dynos time out. After successful deployment I was sure the application was running, but because of time out, logs said the application went down. Because I wasn’t aware of the fact that web dynos time out I suspected that Grizzly was simply interrupted somehow so I’ve decided to move to Jetty. But it happened to Jetty implementation too, so I started digging and I found relevant information :)
I think that Heroku is great. It’s free Java hosting, something that is NOT popular and yet they did it and it works quite nicely. I once tried Google App Engine, but the experience wasn’t great (mind you it was some long time ago) so I’ve decided to give Heroku a chance and because it was actually quite simple to set up the application I think I’ll stick to it for a while and play with the platform – look at all those plugins :)