Following up on his last article, “CRUD with TomEE, MicroProfile, and REST”, Hayri Cicek deploys the TomEE MicroProfile application on Heroku, a cloud provider.

Heroku is a cloud platform as a service (PaaS) supporting several programming languages and I’ve been using it for several years without any problems. Visit https://signup.heroku.com/dc and create a free account.

TomEE configuration

Inside projects root directory create the Procfile and add the following:

web: java -DadditionalSystemProperties=-Dhttp.port=$PORT -jar target/blog-exec.jar

The Procfile is used to specify commands executed by the app’s Heroku containers, which they call “dynos”. Dynos are isolated, virtualized Linux containers designed to execute code based on commands the user specifies.

Inside /src/main/tomee/conf/ create server.xml file and make it look like the following:

<?xml version='1.0' encoding='utf-8'?>
<Server port="8085" shutdown="SHUTDOWN">
  <Listener className="org.apache.tomee.catalina.ServerListener"/>
  <Listener className="org.apache.catalina.security.SecurityListener"/>

  <Service name="Catalina">
    <Connector port="${http.port}" protocol="HTTP/1.1"/>
    <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost" appBase="webapps"/>
    </Engine>
  </Service>
</Server>

By default, TomEE will start at port 8080 but Heroku binds its HTTP server to the port defined by the $PORT environment variable, and we are setting that port via the ${http.port} system property.

Your project directory should look like this:

tree .
.
├── Procfile
├── blog.iml
├── pom.xml
├── readme.md
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── superbiz
    │   │           └── blog
    │   │               ├── entities
    │   │               │   └── Post.java
    │   │               ├── repositories
    │   │               │   └── PostRepository.java
    │   │               └── resources
    │   │                   ├── BlogRestApplication.java
    │   │                   └── PostResource.java
    │   ├── resources
    │   │   ├── META-INF
    │   │   │   ├── microprofile-config.properties
    │   │   │   └── persistence.xml
    │   │   └── publicKey.pem
    │   ├── tomee
    │   │   └── conf
    │   │       └── server.xml
    │   └── webapp
    │       ├── WEB-INF
    │       │   ├── beans.xml
    │       │   └── resources.xml
    │       └── index.html
    └── test
        └── java

Install Heroku CLI

On macOS run the following command in your terminal:

$ brew install heroku/brew/heroku

If you are using Linux or Windows visit Heroku set up page to download the installer for your platform.

Create an app on Heroku

Now that we have installed Heroku we can log in and create an app. In your terminal type the following command with email and password you used when creating Heroku account:

$ heroku login

We will now create our Heroku application by using the following command.

$ heroku create

This command will create an empty application on Heroku. If we would like to we could also specify a name for our application like this:

$ heroku create run-tomee-on-heroku

Our application is using Postgresql as the backend database, so we will install the Heroku Postgresql addon.

$ heroku addons:create heroku-postgresql

To see the URL for the database use the following command:

$ heroku run echo \$JDBC_DATABASE_URL

Now update the resources.xml file inside /src/main/webapp/WEB-INF/ with the database URL environment variables that Heroku provides. Your resources.xml file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<tomee>
    <Resource id="jdbc/my_blog_datasource" type="javax.sql.DataSource">
        JdbcDriver = org.postgresql.Driver
        JdbcUrl = ${JDBC_DATABASE_URL}
        UserName = ${JDBC_DATABASE_USERNAME}
        Password = ${JDBC_DATABASE_PASSWORD}
        jtaManaged = true
    </Resource>
</tomee>

Deploy your application on Heroku

We are done with the configurations and it’s time to deploy the code by using the following git commands:

$ git init
$ git add .
$ git commit -m 'first deploy on heroku'
$ git push heroku master

Now it’s time to see if everything is working, your application URL will be {appname}.herokuapp.com/data/posts

Create a new blog post by using the following curl command:

$ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"title":"My First Blog Post","body":"Welcome to my first blog post, this blog runs on TomEE Application Server and is deployed to Heroku",
        "author": "Hayri Cicek"}' \
        https://run-tomee-on-heroku.herokuapp.com/data/posts

Now you should have one blog post:

$ curl -v https://run-tomee-on-heroku.herokuapp.com/data/posts

Our application is now running in the Heroku cloud and is ready for further development!

Hayri Cicek

Hayri Cicek

Hayri Cicek is author to the upcoming ebook Building an API Backend with MicroProfile. A passionate open source contributor and software developer from Sweden, Hayri's book is open source and hosted on Github and everybody can contribute.

Leave a Reply