In a Nutshell: Deploying a Java webapp to Heroku

This post walks you through getting started with Heroku and deploying a simple Spring MVC web app.

  • Follow the Getting Started guide to get your Heroku Toolbelt setup
  • Login to Heroku from the commandline with ‘heroku login’
  • Since Heroku does not provide it’s own app server, you configure your pom.xml to pull in a dependency on a container, like Jetty (this is from the Getting Started with Spring MVC guide)
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals><goal>copy</goal></goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.mortbay.jetty</groupId>
                        <artifactId>jetty-runner</artifactId>
                        <version>7.4.5.v20110725</version>
                        <destFileName>jetty-runner.jar</destFileName>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

The Heroku Getting Started with Java instructions give some steps on how to run your app locally by running Java from the command line and passing a classpath pointing to your target dir, but this seems odd since Jetty is able to run from Mavan against your created war file or even the code compiled to your target dir using the ‘mvn jetty:run’ command. Add the following to the above <plugins> section to enable and use this approach instead:

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>

  • Heroku uses a file named ‘Procfile’ in the root of your project to tell Heroku how to run your code. Add this file, and inside it add this one line
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
  • Create your new app on Heroku with ‘heroku create –stack cedar’
  • Add and commit your code if you haven’t already, then push to your heroku remote git:
git add . git commit -m "commit comment" git push heroku master

At this point Heroku should build you app remotely and start it up.

If you see this error about the jetty-runner.jar missing, then you forgot to add the plugin part to your pom.xml to copy the jetty jar to the target/dependency dir:

Unable to access jarfile target/dependency/jetty-runner.jar

Red Hat release OpenShift PaaS as opensource, and ability to run your own PaaS with OpenShift Origin

Yesterday Red Hat announced at the Open Cloud Conference in Sunnyvale, CA, the release of OpenShift as open source, and the ability to take OpenShift and use it to run your own PaaS.

What does this mean? Firstly, you can take the source for OpenShift and view or modify it (under the Apache 2 license) as you please. Secondly, if you have a need to run your systems using a private cloud model locally or hosted using your own hosting provider, you can take OpenShift and deploy it where ever you need, and still take advantage of the ability to dynamically provision/deploy apps and services using the OpenShift toolset.

This is very similar to the approach VMWare have taken with Micro Cloud Foundry, which is also open source, and also available so you can run the PaaS yourself.

Troubleshooting your app on Red Hat’s OpenShift

My hosted server seems to not be responding or I’m getting a blank page instead of seeing my app:

  • Try tailing your server log and then hit your app to see if you’re getting errors:
rhc-tail-files -l your_account_id -a your_app_name
  • Give your account password and/or ssh passphrase when prompted

If you’re seeing exceptions from your app, this should give you a clue what’g going wrong.
If you’re seeing a a line in the log like this:

2012/04/03 13:08:59,601 INFO  [org.jboss.as.jpa] (MSC service thread 1-2) JBAS011403: Stopping Persistence Unit Service 'ROOT.war#persistenceUnit'
2012/04/03 13:09:00,152 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015877: Stopped deployment ROOT.war in 20354ms

… then for some reason your app was stopped, and needs to be restarted manually. There are some ongoing issues in the OpenShift environment currently that Red Hat is investigating where apps are left in a stopped state (possibly related t your deployment taking too long and timing out).

Can I browse through my log files on the server?

ssh://long_string_here@your_app_name-your_domain_name.rhcloud.com/~/git/your_app_name.git/

… then the url to ssh to your server is this part:

long_string_here@your_app_name-your_domain_name.rhcloud.com

Ssh like this:

ssh long_string_here@your_app_name-your_domain_name.rhcloud.com

… enter your ssh passphrase when prompted. Your log files are in your_app_name/logs/ – the current log is server.log and there are copies of each log for each previous day. You can use common utils like vi/less/more/cat etc to view your files.

How do I check on the status of my app?

  • rhc-ctl-app -l your_acount_id -a your_app_name -c status

… this will show the status, either RUNNING, STOPPED, or if there is maintenance currently in progress you may see this message:

MESSAGES:
OpenShift is currently being upgraded, some services may be unavailable.

 

How do I manually restart or start my app?

  • rhc-ctl-app -l your_acount_id -a your_app_name -c restart | start