Deploying WildFly Swarm to Heroku
Use WildFly to add support for Java containers in Heroku without going through painful refactoring.
Join the DZone community and get the full member experience.
Join For FreeThe rise of cloud services like Heroku left Java EE developers in a bit of a pickle because of the lack of support for Java EE containers. Heroku has some helpful documentation on using Java EE libraries without a container, but this is probably too much refactoring for an established Java EE applications.
Wildfly Swarm neatly avoids this problem by combining application and container into a single deployable artifact. Let’s take a look at what we need to add to the REST API example we created in this article in order for it to boot in Heroku.
First, you are going to need a Heroku account and have installed their toolbelt. The toolbelt provides you with a number of convenient features for managing applications in Heroku.
Once installed, you need to run the following command:
heroku create
From your existing GIT repo. This creates an application in Heroku, and sets up a link between your local GIT repo and one hosted by Heroku. The output of this command should look like this:
$ heroku create
Creating app... done, stack is cedar-14
https://serene-dusk-72188.herokuapp.com/ | https://git.heroku.com/serene-dusk-72188.git
You can verify the new link to the GIT repo host by Heroku with this command:
$ git remote -v
heroku https://git.heroku.com/serene-dusk-72188.git (fetch)
heroku https://git.heroku.com/serene-dusk-72188.git (push)
origin https://github.com/mcasperson/swarmdemo4.git (fetch)
origin https://github.com/mcasperson/swarmdemo4.git (push)
Before we can start pushing our application up to Heroku, we need to add a new task to the Gradle build script. This task, called stage, does nothing more than trigger the Gradle build. You can find more details on this Gradle task in the Heroku documentation.
task stage {
dependsOn build
}
Finally, we need a file called Procfile, which Heroku uses to launch the Swarm JAR file. The command we define in this file maps the port exposed by Heroku to Swarm, and configures some memory settings.
I actually found it quite tricky to find memory settings that worked in Heroku. As you can see I had to restrict the Java memory usage quite heavily in order to allow the Swarm application to run with heroku’s memory limits. If you consume too much memory, Heroku will simply kill your app.
web: java -Xmx96m -Xss256k -Dfile.encoding=UTF-8 -XX:MaxMetaspaceSize=48m -Dswarm.http.port=$PORT -jar build/libs/swarmdemo4-swarm.jar
Deploying you app to Heroku is a case of pushing the source code into the Heroku GIT repo. Heroku will then compile and execute the application.
git push heroku master
You can view the console output of your app with this command:
heroku logs --tail
I have deployed the mentioned Swarm application to Heroku with these settings, and you can interact with it by visiting https://serene-dusk-72188.herokuapp.com/helloWorld or https://serene-dusk-72188.herokuapp.com/swagger.
Grab the source code for this application from GitHub.
Opinions expressed by DZone contributors are their own.
Comments