📢 Attention Salesforce Certified Trailblazers! Maintain your credentials and link your Trailhead and Webassessor accounts by April 19th. Learn more.

Manage and Scale a Java App on Heroku

View Your Heroku App Logs

Heroku treats logs as streams of time-ordered events aggregated from the output of both your app and the Heroku components that run it. The result is a single channel for all logging events. You can view the logging output from the app you created in the previous unit by running the commandheroku logs --tail.

heroku logs --tail

Example of Heroku application logs

Leave this command running, and visit your application in the browser again by running heroku open in another terminal session (or simply refresh the web page if you still have it open). You see a log message generated each time you visit the page.

Press Ctrl+C to stop streaming the logs.

Inspect Your App’s Processes

Right now, your app is running on a single web dyno. A dyno is a lightweight container that runs the command specified in an app’s Procfile, which is a text file in the root directory of your application. The Procfile is used to explicitly declare what command should be executed to start your app. The Procfile in the example app you deployed is shown here.

web: java -jar target/java-getting-started-1.0.jar

You can check how many web dynos are running using the command heroku ps.

heroku ps

Heroku ps output

By default, your app is deployed on a free dyno. Free dynos sleep after a half hour of inactivity (if they don’t receive any traffic). This causes a delay of a few seconds for the first request upon waking. Subsequent requests perform normally. Free dynos also consume from a monthly, account-level quota of free dyno hours—as long as the quota is not exhausted, all free apps can continue to run.

To avoid dyno sleeping, you can upgrade to a hobby or professional dyno type as described in the Dyno Types article. For example, if you migrate your app to a professional dyno, you can easily scale it by running a command telling Heroku to execute a specific number of dynos, each running your web process type.

Scaling Your App

Scaling an application on Heroku is equivalent to changing the number of dynos that are running. You might want to do this to ensure redundancy (in case one process crashes there is still another that can serve requests), or because you are receiving more traffic than your single dyno can handle (a good problem to have).

First, scale the number of web dynos to zero.

heroku ps:scale web=0

Access the app again by pressing refresh on the web page, or run heroku open to open it in a browser tab. You get an error message because you no longer have any web dynos available to serve requests.

Now scale the app back up again.

heroku ps:scale web=1

To prevent abuse, scaling a non-free application to more than one dyno requires account verification.

Running Administrative Tasks

Some application tasks require running a one-off job from the command-line. You can run these jobs, which are often scripts included in your app, using a one-off dyno using the heroku run command. It can also be used to launch a REPL or console process attached to your local terminal for experimenting in your app’s environment, or executing code that you deployed with your application. For this example, we will use the command heroku run bash.

heroku run bash

Heroku run bash output

If you receive an error, "Error connecting to process", then you may need to configure your firewall.

Don’t forget to type exit to exit the shell and terminate the dyno.

Click Verify Step to proceed to the next step in the project.