Running a Java Wicket Application in the Google App Engine

Nico Heid's picture

The Google App Engine (GAE) is a wonderful playground for your apps, especially when you have no server at hand. It gives you everything you need for free, up to a certain limit. As I happened to be a Java developer it became really interesting when the GAE started supporting Java. You have to register for a GAE account and then apply for Java to be activated. There is a small waiting time but there still seem to be open slots.

As I'm working with Apache Wicket at the moment I thought it would be a nice idea to give you everything you need to get started with development with the GAE and Wicket as your frontend. So in the following lines we will set up a minimal Java / Wicket application and deploy it into the GAE.

Download and install the Google App Engine Java

You'll find everything you need to know on Google's page: http://code.google.com/appengine/
Make sure to select the Java SDK and not the Python one. While downloading you can register and get a GAE account. After you are locked in apply for the Java environment. The developer kit brings along an application server, so that you can test your application locally. It might take some time till you get the confirmation link which will be telling that you're free to use Java. Until then you have to limit your experiments to your local machine.

Creating the Wicket Project with Maven

As build tool we will be using Maven, so if you're new to that you might want to read the Maven tutorial of my colleague Phillip. It gives a good introduction of maven and how it will aid you in software development.

For the impatient, here's the quickstart, as found on the Wicket homepage:

  1. mvn archetype:create -DarchetypeGroupId=org.apache.wicket \
  2.    -DarchetypeArtifactId=wicket-archetype-quickstart  \
  3.    -DarchetypeVersion=1.4-rc2 \
  4.    -DgroupId=com.united-coders -DartifactId=gae-minimal

I'll be using the second release candidate of Wicket 1.4. Running the command will leave you with the project structure we'll be working with. You can run and deploy this in you local servlet containter, but to deploy it into the GAE we need to make some changes.

Google App Engine specific changes

As mentioned on Alastair Maw’s blog, we need to make a few changes to the project so that it will be running, once deployed. This is due to the special environment (the sandbox) the application will be running in. For our example we have to change the application so that it does not spawn any extra threads. Alastair Maw already mentioned what we have to do, so we take his advice and then import the GAE settings into our project, so that we can deploy it.

Changes in the Wicket project

When building the project for deployment make sure to enable the deployment mode. In deployment mode Wicket does not try to spawn any processes for monitoring changes in resource files. This can be done in the configuration file or in the command line when building the project. Because this is a setting only used before deployment I prefer to use the command line option and leave the configuration untouched.
So when you're ready to deploy use the following command: (not yet, we need to make some more changes. but it should compile)

  1. mvn -Dwicket.configuration=deployment clean package

Additionally we need to override the creation of the newSessionStore(), so that it does not run in a separate thread in the background.
So edit your WicketApplication.java as shown below.

  1. public class WicketApplication extends WebApplication
  2. {
  3.     /**
  4.      * Constructor
  5.      */
  6.         public WicketApplication()
  7.         {
  8.         }
  9.  
  10.         /**
  11.          * @see org.apache.wicket.Application#getHomePage()
  12.          */
  13.         public Class<HomePage> getHomePage()
  14.         {
  15.                 return HomePage.class;
  16.         }
  17.  
  18.     @Override
  19.     public HttpSessionStore newSessionStore(){
  20.        return new HttpSessionStore(this);
  21.     }
  22.  
  23. }

GAE specific settings

Take the appengine-web.xml file from the new_project_template in the demo folder and copy it to your projects src/main/webapp/WEB-INF/ folder.
Enable session support and enter the name of your project (as shown in the GAE). After that your appengine-web.xml file should look similar to this:

  1. <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  2.  
  3.   <!-- Replace this with your application id from <a href="http://appengine.google.com" title="http://appengine.google.com">http://appengine.google.com</a> -->
  4.   <application>nameOfYourProject</application>
  5.  
  6.   <version>1</version>
  7.  
  8.   <sessions-enabled>true</sessions-enabled>
  9.  
  10. </appengine-web-app>

Deploy your application into the Google App Engine

After building your project with maven you can upload the project files with the script supplied with the SDK.

  1. ./bin/appcfg.sh update projects/wickethelloworld/target/wickethelloworld-1.0-SNAPSHOT

That's it. Sources are attached. The deployed app can be found here: http://wickethelloworld.appspot.com/

UPDATE
* The limit of available accounts has been abolished.

And there two articles I'd recommend reading when you want to get a bit more detail before you start:
* Google App Engine for Java - 3 Tips for Getting Started
* Google App Engine For Java - Microblogging Case Study

AttachmentSize
wickethelloworld sources12.37 KB

Comments

Anonymous's picture

3 days ago I did some testing using a trivial class using the eclipse plugin. Everything was running smoothly on my PC but on the server I kept on failing due to a class not serializable exception. The class was absolutely trvial (ID + 2 text fields) and so was my code. I gathered all the hints I could on the web and kept on trying for 3 hours and maybe 10 deployments. In the end I posted the code + the logs on the google group for GAE receiving no answer until now. Maybe I was unlucky, but do run your tests before developing anything in Wicket.
Giorgio Malagutti

Nico Heid's picture

I've seen several issues with Wicket in the App Engine. And of course brining an application, or a complex framework which is based upon a regular JRE, into a limited or modified runtime environment is more complicated that deploying it in a compliant servlet container. In terms of scalability the Engine might be nice though.
Can you point us to the code, it might be interesting to take a closer look.

Greetings, Nico

Anonymous's picture

the eclipse project is here:
http://mediazionebologna.com/out/mediazione.zip
I am relatively a newbie in Wicket and AppEngine and of course I can understand that a new technology like Java for AppEngine can have its hidden pitfalls at the beginning.
It would be a good thing if any blogger could publish a simple guide for serialization in Wicket running inside AppEngine, it would close the circle for simple applications.

Anonymous's picture

Hi!

Just wanted to say thank you. I've tried various approaches to this that failed, despite following instructions from other blogs.

I was not happy with the structure that the Google plugin for Eclipse suggested as I am used to the Maven style. I decided to go with it for a start but never managed to deploy a Wicket app. Vanilla servlets went fine, but others resulted in "The requested URL was not found on this server"

That was yesterday and today I started wit a fresh search on Google and found your blog. Not only did the it help me to get the wicket app deployed (don't know why though) but it also gave me the Maven part.

Fingers crossed I can continue this path.

/Per (http://blog.crisp.se/perlundholm)

Anonymous's picture

When I worked through this tutorial, the GAE eclipse plugin didn't find the appengine-web.xml until I configured a WAR directory (project properties > Google > Web Application > this application has a WAR directory > src/main/webapp)

Anonymous's picture

I am currently testing wicket with GAE. When submitting a form the first time, all code on my page runs as expected. However if the page fails some validation and the page is redisplayed then all following submits do nothing. It appears that google app engine is just redisplaying a cached page and none of the wicket lifecycle is being run.

A simple example is I created a form with a single RequiredTextField and a submit button. If I submit the page with no input in the required text field, I see an error in the feedback panel that the field is required. I then fill in the field, click on submit, and the page displays again stating that the field is required.

I then added my own validator to the text field, and just added a simple log message in that validator. When the page is submitted the first time I see my log message, but after they page is redisplayed my validator is never run again. I have to go back to my home page, then back to my test page and then the submit works again.

Why would the wicket life cycle not run on the second submit? Thanks for any feedback you can send me.

I am running Google App Engine 1.4.2 in Eclipse Helios Service Release 2