Skip to main content

Deploying your Spring MVC java application on Heroku



In one of my previous posts, we setup a local rest api web application with a spring data jpa, hibernate and a postgreSQL database. That post was Rest Api with Spring, hibernate and Postgres. The aim for setting up that project was to use it to test out REST http integration from Salesforce. However Salesforce cannot integrate with a local service. So we need to deploy this service to an externally exposed server. I decided to expose it on Heroku.

Heroku is a platform as a service cloud provider. You can create your application and deploy it to their platform. They take care of managing the actual server where the application is deployed. Moreover its all on the cloud. You just login to their portal and manage your application. Your source code for the application is maintained in Git. You can made changes in Git and deploy it back to Heroku. Internally it deploys your app to linux servers (they called dynos). If you need to scale your app for heavy traffice, you can increase the number of dynos  from the heroku dashboard itself.

Below I will deploy my java application to Heroku. First steps will be -
  • Setup a heroku account (it is a free account for one web app and one database)
  • From their website, install the heroku toolbelt. This will allow you to work with Heroku from the command prompt.
  • If you are interested to learn more about Heroku, use their getting started guides.
  • I started with one of their getting started guides for Java, this enabled me to setup a java application with connectivity to Postgres
  • They provide a postgres database as part of their free tier. I am going to use that.
  • Next, I will update my POM.xml in the java application to include Maven dependency plugin. Also I am including configuration to include jetty-runner.jar as a dependency.

 
      Once I have jetty-runner.jar in my dependencies, it will get downloaded to my target dependency folder. I can use this jar to execute a java command and bring up my web application in the jetty application server.

Note: in previous post, i had included the maven-jetty-plugin in the POM already due to which I was able to execute mvn jetty:run command. Heroku needs you to follow above approach so you can specify the java command to bring up this webapp in a file called Procfile
  • In the root directory of my project, i am going to setup a file "Procfile". Be careful to not give an extension to it and give the exact name (I learnt that the hard way :) ). This file should contain the java command to execute and bring up your webapp in the jetty server.
  •  Now lets talk about the database. In my previous post, we had configured  hibernate using a Java configuration class DBConfig.java. It was setup to connect to a local postgres database. Now when we deploy our app to Heroku, we will need a postgres database on Heroku. Heroku allows you to create a free heroku database. Lets run below commands to do that.
  • In my project's root directory, to create the heroku app - use the command heroku create
  • this will create the heroku app and give back the details of the blank app that was created for you. It will also add a git remote repository with name "heroku"
  • Lets add a database in this blank heroku app by doing the following - heroku addons: create heroku-postgresql
  • This will add a database for your app on the heroku cloud. It also adds an environment variable "DATABASE_URL" in your environment on the cloud which will have the uri to your database. We will use this to update our DBConfig.java to connect to the new postgres database.
  • Change the DBConfig.java code to use the system environment variable to get the database path, port, user name and password -

 Note: How you are using the System.getenv to get the DATABASE_URL value and then parsing it to create the datasource details.
  • On heroku cloud, you may want to create the database table and put some data there. To connect to your postgres db on the cloud, just use the command - heroku pg:psql . It will connect you to the app's database. Use standard sql commands to build the users table and put any data you want there.
  • Now you are done with all changes. Lets commit all the changes to your git repository first. Post that push all the changes to the remote Heroku repository with the command - git push heroku master
  • When you push it to the heroku repository, you will see that Heroku automatically scans your code, detects the java code, builds it and deploys your app -

  • One last step remains, you need to scale your app to have atleast one process running. This is done by using the command - heroku ps:scale web=1
   Remember web was the name you gave to the process that will get created by running the command you specified in the Procfile of your project.
  • Thats it, your app should be running now. To access your app, you can use the command - heroku open
 Voila, now i have a running  web application on the cloud that I can use to test my Salesforce integrations! Hope you are able to follow these instructions if you are interested in deploying Spring MVC applications to Heroku.

Remember as you are using a free tier application, it will stay up for only 30 mnts and go down due to inactivity. I am sure there are other limits that apply in the free tier. If you are interested in having a real application on the cloud, you can always upgrade to use their services in a better way.

Comments

Popular posts from this blog

Workaround to bypass Salesforce SSO

One of the best practices for implementing Single Sign On for your Salesforce org is always ensure there is a way your System administrator can login via the standard login page side-stepping the SSO configuration.  Reason for this is if ever something goes wrong with your Idp provider's service and the SSO authentication responses are not coming as expected, all your users are unable to login. That is, if you have setup your My domain to prevent logins via standard Salesforce login urls (login.salesforce.com). This includes the System administrator as well. Only if your system administrator can somehow login, then he or she can disable the SSO settings for your domain and allow login via the normal login page as a temporary measure. What do you do in such a situation? Well Salesforce has built a workaround for this which is not well documented anywhere (probably for a good reason :) ). I found out about it from a colleague at work. If your my domain url is - https://Com

Salesforce Big Objects - Key learnings

I remember reading about Salesforce Big Objects before they became available and thought maybe it is an option to backup data present in regular objects. That is, instead of taking a backup via an ETL tool or data loader, maybe this will be an option to backup the data in the Force.com platform itself. Now that it is GA and I am reading more about it, i think the use cases for this are more varied. Some use cases I can think of are –  Archival of key data objects within the platform: You may want to use this option if you dont use any other means to backup key data. Also this may be an attractive option for non-large enterprise customers who dont themselves invest on large data warehouses or data lakes in their enterprise architecture. Ex: customer history (if present in tasks and activities) will become huge over years but this is useful information for reporting & customer analysis. Store key information which is large volume in quantity and also high volume in transa

DBAmp for Salesforce - salesforce integration for SQL Server DBAs

Recently i got the opportunity to explore a tool called DBAmp for integration with Salesforce. I found it to be a very useful tool which will help with any data manipulation requirements in Salesforce. Following are my learnings from the exercise. Hope it helps any of you who may need to work with this tool -  DBAmp is a SQL Server package that can be used to integrate with Salesforce. The site where this software is available is - http://www.forceamp.com/ Overview: It essentially installs on top of an existing SQL Server database and provides an OLE DB connector that can be used to connect to Salesforce. Behind the scenes, it executes API calls against Salesforce for any SQL server command it receives. Thus we can create a connection in SQL server to Salesforce org and pull data into tables in the database just as if we are querying against Salesforce tables directly. Use cases for DBAmap + Salesforce: Many use cases exist for using this tool against Salesforce. Pr