This quickstart will get you going with Java apps on the Heroku Cedar stack using the Gradle build system and Jetty embedded web server.
Sample code is available on github along with this article. Edits and enhancements are welcome. Just fork the repository, make your changes and send us a pull request.
Prerequisites
Basic Java knowledge, including an installed version of the JVM.
Basic Gradle knowledge, including an installed version of Gradle (1.0-milestone-5 or later).
Your application must run on the OpenJDK version 6.
We'll start by setting up your local workstation with the Heroku command-line client and the Git revision control system; and then logging into Heroku to upload your ssh public key. If you've used Heroku before and already have a working local setup, skip to the next section.
Once installed, you'll have access to the heroku command from your command shell. Log in using the email address and password you used when creating your Heroku account:
:::term
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Press enter at the prompt to upload your existing ssh key or create a new one, used for pushing code later on.
Write Your App
We will be creating a completely standard Java application that serves web requests using the Servlet API and the embedded Jetty web server.
First create a class that implements a simple Servlet. For the purpose of this example, we'll also create the main method in this class.
src/main/java/HelloWorld.java
:::java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;
public class HelloWorld extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().print("Hello from Java!\n");
}
public static void main(String[] args) throws Exception{
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new HelloWorld()),"/*");
server.start();
server.join();
}
}
Set up the build
Create a Gradle build file in your project root. The stage task will be run by Heroku to build your app:
Prevent build artifacts from going into revision control by creating this file:
.gitignore
:::term
build
.gradle
Build Your App
Build your app locally:
:::term
$ gradle stage
Declare Process Types With Foreman/Procfile
To run your web process, you need to declare what command to use. We'll use Procfile to declare how our web process type is run. The application plugin takes care of generating a run script, build/install/app/bin/app, which we'll use to start the web app.
Here's what the Procfile looks like:
:::term
web: ./build/install/app/bin/app
Now that you have a Procfile, you can start your application with Foreman:
Your app will come up on port 5000, the default port set by Foreman. Test that it's working with curl or a web browser, then Ctrl-C to exit.
Store Your App in Git
We now have the three major components of our app: build configuration and dependencies in build.gradle, process types in Procfile, and our application source in src/main/java/HelloWorld.java. Let's put it into Git:
:::term
$ heroku create -s cedar
Creating evening-sky-2099... done, stack is cedar
http://evening-sky-2099.herokuapp.com/ | [email protected]:evening-sky-2099.git
Deploy your code:
:::term
$ git push heroku master
Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (10/10), 1.59 KiB, done.
Total 10 (delta 2), reused 0 (delta 0)
-----> Heroku receiving push
-----> Java/Gradle app detected
-----> Installing gradle-1.0-milestone-5..... done
(Use the Gradle Wrapper if you want to use a different gradle version)
-----> executing gradle -I /tmp/asdre342qfad/opt/init.gradle stage
:compileJava
Download http://s3pository.heroku.com/jvm/org/eclipse/jetty/jetty-servlet/7.5.3.v20111011/jetty-servlet-7.5.3.v20111
...
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:installApp
BUILD SUCCESSFUL
Total time: 20.534 secs
-----> Discovering process types
Procfile declares types -> web
-----> Compiled slug size is 936K
-----> Launching... done, v5
http://blazing-planet-4614.herokuapp.com deployed to Heroku
Now, let's check the state of the app's processes:
:::term
$ heroku ps
Process State Command
------------ ------------------ ------------------------------
web.1 up for 1m ./build/install/app/bin/app
The web process is up. Review the logs for more information:
:::term
$ heroku logs
2011-10-13T18:06:23+00:00 heroku[api]: Deploy 7c70d13 by [email protected]
2011-10-13T18:06:23+00:00 heroku[api]: Release v5 created by [email protected]
2011-10-13T18:06:24+00:00 heroku[slugc]: Slug compilation finished
2011-10-13T18:06:43+00:00 heroku[web.1]: Unidling
2011-10-13T18:06:43+00:00 heroku[web.1]: State changed from down to created
2011-10-13T18:06:43+00:00 heroku[web.1]: State changed from created to starting
2011-10-13T18:06:44+00:00 heroku[web.1]: Starting process with command `./build/install/app/bin/app`
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.198:INFO:oejs.Server:jetty-7.5.3.v20111011
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.258:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.293:INFO:oejs.AbstractConnector:Started [email protected]:27922 STARTING
2011-10-13T18:06:46+00:00 heroku[web.1]: State changed from starting to up
Looks good. We can now visit the app with heroku open.
请发表评论