Archive

Posts Tagged ‘Hello World’

Episode 1 : Google App Engine Java – Development Setup

September 22, 2009 16 comments

This blog post will be focused on getting your developer environment right for developing/deploying your Google App Engine for Java (GAEJ from now) application in your IDE. The version of Google App Engine that I shall be covering is 1.2.5 (Released on 09/03/09)– the very latest (at least at the time of this writing).

Before we begin, let me make a few assumptions:

  • You are a Java developer who is familiar with developing basic Java Web applications. At most you need to know Servlets/JSP and the typical WAR file structure. Nothing more!
  • You are comfortable with installing the JDK, Eclipse IDE and downloading / setting up plugins for Eclipse.

There are numerous resources on the web that address the book in addition to books, so please feel free to look them up before you proceed. As much as I would like to cover all details, I think I will need to keep this blog focused more on Google App Engine stuff for Java. So let’s get going…

Sign up for Google App Engine

You need to first sign up for the Google App Engine Account. This is easy and is a breeze in case you already have a Google Account (Gmail, Apps, etc) in which case it is best that you use that straight away. Go to http://appengine.google.com and simply login with your account. Even if you do not have an account, you can sign up for a Google Account, a link for which is provided on the page.

Whether you already have a Google Account or have signed up for a new one, do login first. Once you are logged in, you will be presented with a list of applications that you are currently running on the Google App Engine under the account which we logged in with. Google provides a total of 10 applications that you can deploy under a single account. Please be aware that as of this date, there is no way to delete an application and use that slot for another application — so use with care.

Here is a screen shot of my account page at the Google App Engine after login:
post1-1
It shows that I have 4 applications registered on Google App Engine and have 6 more to go. You might have no applications listed if you are logging in for the first time and/or have not created any application.

Note: If you are registering your 1st application, the process is as follows:

1. Click on Create an Application.

2. Once you have selected a name, then you will be prompted for your Mobile Number. Enter your mobile number (with country code, etc).

3. Google will send you a SMS with an code. You will be prompted on the next screen to enter the code that you received on SMS.  Enter that to confirm the process.

This is only required for registering your 1st application and once the SMS verification process is complete, you will not be prompted again when you create your 2nd application and so on.

Registering your Application

Cool, let’s create an application then but before that we must tell your application to show its ID. So let us talk about Identity first.

Identity, Identity, Identity….

Yes, everyone needs to show an ID. And the same applies to your application too that is hosted in the cloud.

Let me explain that a little. As you know Google App Engine is a PaaS (Platform as a Service), which means that you can develop and deploy your application (following some specification) into their infrastructure (in the cloud). Now what this means is that while it would be nice for you to get an application name of your choice, it is not feasible in a hosted environment where you are sharing all resources with other applications too.

By default, your application needs to be unique within this so called name space:

YOUR_APPLICATION_ID.appspot.com

where YOUR_APPLICATION_ID can be any name that you wish for your application, that has not already been taken by someone else. So before you start with an application in mind, it pays to plan it out a little first and that includes what name you would like for your application and then registering it as quickly as possible so that you have it.

Note: Please keep in mind that what I am discussing here is the basic facilities provided by Google. It is perfectly possible for you to purchase a domain name and then point it to the URL where your application is hosted in the Google App Engine cloud. But we will not discuss all that know and will keep the discussion to what is provided straight of the bat.

OK. So you have thought of the application name and wish to register it. So since we are going to write a “Hello World” sort of an application — we should go ahead and create an application identifier first.

Let us actually do that before moving on. Assuming that you are logged in as described earlier, click on the Create An Application button. This will bring up a page as shown below:
post1-2

This page will allow you to select an Application Identifier that we have been talking about. Go ahead. Try out a few names like myhelloworld, myfirstapp, etc and see which one is available. Give any string for the Application Title and go with the default options for the Account options (we can skip it for now). Click on the Save button. If all is well, the application identifier will be created and your first application is registered. Congratulations! But we have a lot to do including writing the application.

Please make sure that you note down your Application Identifier since this will be used later on to verify your entire setup. OK, let me not scare you — you can always go back to the Google AppEngine account and see the applications listed 🙂

Do check out the excellent monitoring portal that Google App Engine provides to view/monitor a number of things about your application. Just click on the registered application in the application list and you will be lead to the portal where you can check the logs/quotas and configure several other stuff. As this blog develops, I will cover more of this but for now, just browse around and familiarize yourself.

Downloading, Installing and setting up the Eclipse IDE for GAEJ

We will need the following software to be present on your development machine. I am assuming that you are comfortable with downloading and setting up of the software from public sites.

  1. Java Development Kit 1.6.x : Please ensure that the JDK version is setup appropriately on your machine.
  2. Eclipse 3.3 / 3.4 / 3.5 : Choose any Eclipse IDE version that you want. I am using the latest version 3.5 Galileo Release.
  3. GAEJ Plug-in: Go to the following page: http://code.google.com/eclipse/docs/getting_started.html This page contains links to the GAEJ plugin for Eclipse download/installation. I list below the update site URL for the GAEJ Eclipse plugin from that page:

    Depending on the version of Eclipse that you have, I reproduce from the Google site the links for installing the plugin depending on the version of Eclipse that you have. Please follow the instructions on the page, it is straightforward and I do not wish to replicate it here.

Once you have finished the setup and restarted your Eclipse IDE, you should see the following 3 icons in the toolbar of the Eclipse IDE.
post1-3

If not, please refer to the troubleshooting options that are described in each of the above links.

Writing our first GAEJ application

So far we have signed up for Google App Engine, registered our Application Id and setup the GAEJ plug-in for Eclipse. We are ready to write our application now. The Gods always tell us to start any new thing on an auspicious note by crying out “Hello World” and we shall stick to the tradition.

As we shall see things unfold, GAEJ plug-in for Eclipse makes things very simple. It does all the hard work after we give it some inputs and then it generates an entire web application for us along with sample servlet that pretty much does just that i.e. says “Hello World”. What we will be writing no code, you say? Yes. All this code is generated for us and is pretty much a template that we can take and put our application logic in there.

So let us move forward and generate our first project now (steps along with explanation follow):

1. Launch your Eclipse (if not started).

2. Either click on File –> New –> Other or press Ctrl-N to create a new project. Select Google and then Web Application project. Alternately, you could also click on the New Web Application Project Icon [part of GAEJ Eclipse Plugin] in the Toolbar:

3. This will bring up a New Web Application Project page as shown below:

post1-4

Enter the details as given below:

* Project Name : MyFirstGAEJProject. You can select any name that you like.
* Package : com.gaej.firstproject. You can select any name that you like.
* Deselect the Use Google Web Toolkit option.

4. Click on Finish. This will generate an entire project for you which is ready to be deployed.

5. But before that, let us look at the files that have been generated. I reproduce for you the screen from my Package Explorer as shown below:

post1-5

6. If you are familiar with Java Web applications, this should be straightforward stuff with directories like WEB-INF, WEB-INF\classes, WEB-INF\lib, etc. But let us cover it in brief:

  • src : This contains the source files. You will find that it has generated a sample file name MyFirstGAEJProjectSerlvet.java for you. It has been created as per the package hierarchy that you selected. You can keep all your source files over here as you develop more.
  • war : This is the war file structure i.e. Web Application Archive. This if you recollect will be deployed into the Servlet Container of the App Engine that will eventually host your Java Web Application. In this directory, you have the following:
    • WEB-INF\lib: All appropriate JAR files that provide the Java SDK libraries along with Google Java APIs for the App Engine have been linked correctly into the project build path and the necessary JAR files are also present in the WEB-INF\lib folder
    • index.html : This is the first page served. You can actually customize it to any that you want. All your additional html files/resources could go in here.
    • logging.properties: If you are familiar with Java logging, you should be able to make sense of this file and tweak your logging preferences over here.
    • web.xml : This is a standard web application configuration file. You will find the Servlet entries present here for the sample servlet that is generated for us.
    • appengine-web.xml : This is a Google App Engine specific configuration file. And the main element here of interest to us is the application element that will contain the Application ID of the application that we registered for Google and which will be pointing to a hosted version of this WAR. We will set this up at the time of deploying the application, which we will cover later.

7. MyFirstGAEJProjectServlet : Let us cover this in brief, which can also act as a refresher for writing and setting up a servlet configuration in case you are little rusty (happens to the best of us!).

The servlet code is straightforward as shown below[Omitting package and import declarations]:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
 resp.getWriter().println("Hello, world");
 }
 }

It extends the javax.servlet.http.HttpServlet class and implements the GET method. You could parse out the request parameters if needed (in this case there are none of interest). All that the servlet does is to get a handle to the response stream and then mention its content type and send across the text Hello World to the client.

Now, we need to set the servlet url and its java class in the web.xml configuration file. Open up the web.xml file and you will find the appropriate entries as shown below:

<servlet>
<servlet-name>MyFirstGAEJProject</servlet-name>
 <servlet-class>com.gaej.firstproject.MyFirstGAEJProjectServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>MyFirstGAEJProject</servlet-name>
 <url-pattern>/myfirstgaejproject</url-pattern>
 </servlet-mapping>

And finally, if you open index.html, you will find a link that references the servlet as shown below:

<td><a href="myfirstgaejproject"/>MyFirstGAEJProject</td>

so that when the index.html page is served, you will see a Hyperlink named MyFirstGAEJProject and on clicking it will invoke the MyFirsGAEProject servlet implemented in com.gaej.firstproject.MyFirstGAEJProjectServlet which will then print out Hello World.

Testing it locally

GAEJ Eclipse plug-in ships with a local server, where you can deploy your application and test it out. Ensure that port 8080 is free on your machine and that no other web server is running on it.

To test the application, all you need to do is the following:

1. Select the project and then Run As –> Web Application

post1-6

2. This will compile/deploy your application the local server and the server will be started on port 8080. You will see the server starting in the console window as shown below:

post1-7

3. If all is well, you will get the message that the server is running at http://localhost:8080 as shown above.

4. Once that notification is shown, launch your browser and navigate to the url : http://localhost:8080. This will bring up a default JSP page that is generated for you with the servlet URL in it as shown below:
post1-8

5. Click on the link and you will see your servlet getting invoked and telling you “Hello World”.

post1-9

Now that things work, we need to deploy this application into the Google App Engine infrastructure. So lets go to that.

Deploying it into the Cloud

To deploy the application in the cloud, simply click on the Deploy App Engine Project Icon available in Toolbar[part of GAEJ Eclipse Plugin]. This will bring up a dialog where you will need to provide your Google Account credentials (username and password). Do not click on Deploy yet.
post1-10

Click on the App Engine Project settings link in the above dialog. This will bring up a properties page as shown where you need to entire the Application ID. Remember the Application ID that we discussed earlier and you registered one for you. Well that is the Application Id that you need enter over here. Enter that and click OK as shown below [Remember to enter your Application ID before clicking OK]:

post1-11

This will lead you back to the Deploy Project to Google App Engine dialog box. Now simply click Deploy. This will connect to Google App Engine and deploy your application and register it with the Application Id that you used. A snapshot of the Progress dialog on my machine is shown below:

post1-12

You will a lot of activity in your Console bar (a snapshot is shown) and at the end of it you should see a message saying that the application is available.

post1-13

You can deploy your application several times as you make changes. There is a certain quota limit to how many times you can deploy it in a day and you can read that up but for the moment, you can rest assured that the number is quite huge.

Congratulations! You now have your own application hosted on the Google App Engine. It is already available to the world and that is what we will verify in the next section.

Testing the hosted/online version of our application

Your application is available under the url : http://YOUR_APPLICATION_ID.appspot.com. So if you Application ID is myapp, the url is http://myapp.appspot.com. Go ahead and access your URL. 🙂

Technically it is even possible to have different versions running, but for the purposes of this article we will limit it to the latest version that you deploy and which is available under the url that I have mentioned above.

Next Steps

So we have ended our mini journey for now in which we did the following:

  • Registered for Google App Engine
  • Registered our first application
  • Setup the Developer Environment for developing and deploying GAEJ Java applications
  • Used the Google Eclipse Plug-in to develop our first GAEJ application
  • Tested and deployed our first GAEJ application

Thanks for joining in. I hope that this gives just about enough push to have your environment all set up correctly along with the complete end to end experience.

The next episode will contain details on a great feature that has got recently added to GAEJ and which is XMPP Support. I have used that to build the Mumbai Blood Bank Locator Agent. Go give it a try! And stay tuned for the next episode that will cover in detail on you can build your own Agent 007.

Advertisements
Categories: Uncategorized Tags: ,