Skip to content

Running Payara Micro services on Heroku is incredibly straightforward.

There are 2 basic ways of running a Payara Micro service on Heroku:

  1. Create a Fat Jar
  2. Deploy a .War file along with PayaraMicro.jar

I prefer to deploy applications as .War files rather than Fat Jars, so in this example, I'll show how to create a Heroku application and deploy a Payara Micro service to it.

Enabling a Payara Micro Application For Heroku

To enable a Payara Micro application for running on Heroku, there are two simple steps we have to make. The first is to simply bundle the Payara Micro distribution file within the Maven project. (You could create a standalone app and completely use Maven, but I think this is a simpler solution). Download Payara Micro and place it in the \lib folder of the Maven project.

.
|___.gitignore
|___lib
| |___payara-micro-4.1.1.164.jar
|___pom.xml
|___Procfile
|___src
| |___main
| | |___java

The reason for deploying Payara Micro is simply that we need this to launch any service .War files. *Remember, we could use a Fat Jar, if that's your preference.*

Secondly, we need to create a file telling Heroku how to run the service that we are to deploy. This file, Procfile needs to be created at the root of the Maven project structure as shown above. The contents of this file tell Heroku how to start Payara Micro and deploy the application:

web:    java -jar lib/payara-micro-4.1.1.164.jar 
--deploy target/PayaraHeroku.war --port $PORT

There are a few things to say about the Procfile. You can see that this defines a web application that is invoked by java -jar lib/payara-micro-4.1.1.164.jar. There are then a couple of Payara Micro command line options. --deploy tells Payara Micro to deploy the result of the Maven build (target/PayaraHeroku.war in this example). --port $PORT tells Payara Micro to listen on the correct port that Heroku is using

So that's it, there are only 2 small changes needed to a Payara Micro service to enable it to run on Heroku. We need to add the Payara Micro runtime and create a Procfile.

Deploying a Heroku Application

Once you've created a Payara Micro application, it needs to be deployed on Heroku. Deploying a Heroku application is documented in depth on the Heroku Dev Centre. You'll find everything you need to know about deploying Java applications there, so I'll only go over this briefly.

The basic steps are to:

  1. Create a Heroku Application heroku create
  2. Commit your Payara Micro service git add ... && git commit ...
  3. Push the git repository to Heroku git push heroku master
  4. Scale the application accordingly heroku ps:scale web=1

Once the git repository is pushed to Heroku, Heroku will work out that this is a Java application and will start the Maven build. This will create the artefact in the target directory on Heroku which is referenced by the Procfile. Once the build is complete, Heroku will start the Payara Micro service using the options defined within the Procfile.

And that's it! As you can see, there's very little you need to do to be able to get your Payara Micro services running on Heroku.

WildFly Swarm applications can be deployed to Heroku, simply by adding a few files to the deployment. In this post, I'll show the changes that are needed to be made to a WildFly Swarm application and how to then deploy the application to Heroku. I'll show how to deploy my Fruits example WildFly Swarm application to Heroku. The source for this application can be cloned from GitHub at https://github.com/doobrie/swarm-rs

Creating a Heroku Application

Assuming that you've got the Heroku Toolbelt installed, creating a Heroku application is a matter of opening a command prompt/terminal in the root of a WildFly Swarm project (for this example, a Maven project), and executing the heroku create command.

$>heroku create
Creating app... done, stack is cedar-14
https://mighty-temple-xxxxx.herokuapp.com/ | https://git.heroku.com/mighty-temple-xxxxx.git

This creates a Heroku application, adding a git remote, heroku, ready for deploying your application.

Defining the Java Runtime

Heroku can automatically determine that your application is Java based, but needs to be told which version of Java to run. To define this, create a file called system.properties in the root of the project containing a single line indicating the version of Java required.

java.runtime.version=1.8

How To Execute The Application

Heroku uses a Procfile to determine how your application should be executed. This file should also be located in the root of your project. To run a web application, we need to tell Heroku to run a Web Dyno. We also need to tell it what command to run and what port to run on.

In Heroku, the port that applications must listen on is defined by the $PORT environment variable. A default WildFly Swarm application runs on port 8080, but this can be changed by adding the `swarm.http.port` environment variable. To execute a WildFly Swarm application, the Procfile should look something like

web:    java -Dswarm.http.port=$PORT -jar target/swarm-rs-1.0-SNAPSHOT-swarm.jar

Since we're running a WildFly Swarm "fat" Jar, there are no additional class path entries required - all that is needed is the name of the Jar file containing the application, swarm-rs-1.0-SNAPSHOT-swarm.jar in this example.

Deploying The Application

That's all that is required to get going with Heroku and WildFly Swarm. You can see that we've made no changes to the application itself other than adding a system.properties file to determine which Java runtime to use, and a Procfile to define how to start the application.

To deploy the application on Heroku, ensure these files are added and committed to the local Git repository and then push the changes to Heroku.

$>git push heroku master

remote:        [INFO] ------------------------------------------------------------------------
remote:        [INFO] BUILD SUCCESS
remote:        [INFO] ------------------------------------------------------------------------
remote:        [INFO] Total time: 01:14 min
remote:        [INFO] Finished at: 2016-02-07T21:49:57+00:00
remote:        [INFO] Final Memory: 36M/389M
remote:        [INFO] ------------------------------------------------------------------------
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 96.9M
remote: -----> Launching...
remote:        Released v4
remote:        https://mighty-temple-xxxxx.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/mighty-temple-xxxxx.git
 * [new branch]      master -> master

To verify that the application works, browse to the /api/fruit Url of the application. The Url of the application is displayed on the console after pushing to Heroku.

Conclusion

I've given an overview of how to deploy WildFly Swarm applications to Heroku. Hopefully you'll agree that this is a straightforward process that allows you to be quickly up and running on Heroku.