A few months back speculations were high amongst the developer community
about the fate of Java after the suspected takeover of Sun Microsystems by IBM,
that was not to be, and instead it was Oracle that was successful at it. Amidst
speculations, came the announcement that Google has made Java available as the
programming language for the Google App Engine. Since the launch of this App
Engine, Google has only provided support to Python as a programming language,
but Java had been the most requested feature for App Engine by the developer
community. By moving beyond Python and embracing Java, Google has opened up
avenues to develop Java based web applications that would run on Google's Cloud
offering. Providing Java support to the Google App Engine also paves the way for
other programming languages that run on Java virtual machines, for instance
languages like JavaScript, Groovy or JRuby. In this article, we look at the App
Engine Java overview and how you can develop and deploy an application on the
Cloud.
Direct Hit! |
Applies To: Adv Java developers USP: Create Java based apps to run on cloud Primary Link: code.google.com/ appengine/docs/java/overview.html Keywords: Google App Engine |
Overview
The Google App Engine Java environment provides a Java 6 JVM, a Java
Servlets interface, and also supports standard interfaces for data store and
services like Java Data Objects, Java Persistence API, JavaMail, Jcache, etc.
The App Engine uses the Java Servlet standard for web applications, whereby the
user or developer provides the application's servlet classes, JSP pages and
other files along with the deployment descriptor (web.xml) in a standard WAR
directory structure. The applications are run in a secure 'sandbox' environment
to isolate the application for service and security. This means that an
application cannot spawn threads, or write data to local system files, thus
ensuring that the application does not interfere with performance for other
applications on the cloud infrastructure.
Getting started
Since the applications on App Engine would run on Java 6 virtual machine, it
is ideally required to use Java 6 for developing and testing the application.
Though the App Engine SDK is compatible with Java 5 for development purpose, for
developing apps for App Engine, you should have JDK 5 or JDK 6 installed on your
system. And you can use Eclipse as a development environment. App Engine plugin
is available for Eclipse versions 3.3 and 3.4, and through this plugin you can
install the App Engine SDK from Eclipse using Software Updates. To do so, go to
Help > Software updates and select the Available Software tab and there click on
Add Site button and provide this as URL http://dl.google.com/ eclipse/plugin/3.4
and click OK. The available software list will get refreshed and a new entity
appears named Google Update Site. Select this and its two options of SDKs and
Plugins to install the Google App Engine SDK and integrate it with Eclipse. Now
you can start developing apps for the Google's Cloud.
While creating a new web application for App Engine, ensure that the highlighted option is unchecked and the 'Use Google App Engine' remains checked. |
Demo application
We create a small demo app that will display a message and show the system
time (Google's cloud time). For that go to File > New and then select Web
Application Project. In the project wizard window, give project name as
PcqDemoApp and package as com.pcq. Under Google SDKs option, make sure you
uncheck the “Use Google Web Toolkit” option and ensure that the “Use Google App
Engine” option is checked. Once you click Finish, the whole project complying to
Java Servlet standard gets created with descriptor files along with a
hellopcqServlet.java, which acts as a starting point for the project. Open this
servlet file and modify the code as shown in the snippet below:
public class PcqDemoAppServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
Calendar time= Calendar.getInstance();
resp.setContentType("text/plain");
resp.getWriter().println("This is PCQ Demo");
resp.getWriter().println("The Time is : "+
(time.get(Calendar.MONTH) + 1)
+ "-" + time.get(Calendar.DATE)
+ "-" + time.get(Calendar.YEAR)
+ " " + time.get(Calendar.HOUR_OF_DAY)
+ ":" + time.get(Calendar.MINUTE)
+ ":" + time.get(Calendar.SECOND));
}
}
As we have used a Calendar class, add the necessary java.util import for this
class to the code.
Running the application
To test and run the project, go to Run > Run Configuration and then from the
Run configuration wizard select the Web Application option from the list and
select the project and give a name to it, click on Run. Most of the times,
development machines have some web server installed and running on the default
port 8080. The Run Configuration wizard gives you an option through which you
can run the application using any free port. After successfully running the
application, the WAR deployables get created and now we can deploy the
application on the cloud.
To deploy the app, pass the Application ID which you had created while registering on App Engine site and select the Java project that you have developed |
The embedded web-server uses port 8080 which is generally not free. There is an option to automatically select a free port to run the application |
To deploy the application, you first need to register with the App Engine
site, appengine.google.com, and when you have registered, create an application
and you need to provide the same application name as ID to your Java web
application. Once you are through with this step, from Eclipse, right-click on
the project name and then select Google > Deploy to App Engine.
On the wizard, click on App Engine Project Settings and in the window give
the Application ID which you had created on App Engine website, in our case “hellopcq”.
Click OK and then on the deployment wizard window give your login credentials
and the project you want to deploy. Once the deployment has been completed
successfully, you can access the deployed application on the web by going to
URL, http://hello pcq.appspot.com. This way Java developers can now create
applications for the cloud computing platform using Google's App Engine.