10 April 2009

Google Appengine Java Test Ride

Time to climb on the appengine-java-blog train.

9:45 OK, Bootsy Collins 'If 6 was 9' playing, check.

I've created a an app using appengine with python and as much as writting in python is fun, I don't like the maintenance aspect of it (trying to remember what methods classes had without looking it up again etc).

I really like the idea of appengine especially the ease of use in deploying and versioning your application, but especially the zero cost to try aspect. I'm dying to try amazon's service, but I can't try it without entering my credit card details and I don't have one at the moment.

So I've been looking forward to writing for appengine in java. Here is my first experience with it I timestamped to give you an idea of the time it takes to create and app. I've been programming in Java since 1995 and Servlets since 2000, so your experience may vary ;)

The kids are watching Asterix in the study, bit distracting but I have headphones, hence the musical references


10:02 So, eclipse plugin loaded, lets create a servlet and send it live.

10:04 Project created without GWT, creates a default servlet, bleh.

10:05 Simple single button project publishing, must create new application on appengine first.

The deploy dialog doesn't display application setting by default, need to open 'App Engine project settings..', not quite intuitive. Weird email completion too (got it wrong first time).

10:09 Up and running! Not bad, under ten minutes to create a new application and send it live, pretty painless.

Next: index.jsp -> link to servlet -> forward to index.jsp

hmm, creating a servlet using the wizard doesn't work, needs a web project and I can't enter one. Oh well, back to creating by hand, will look into that later..

rrr, prefer netbeans

10:19 editing web.xml -> added TestServlet, changed welcome file to index.jsp

10:21 Re-deploying the app I have to re-enter password, that could be improved.

10:22 Tested and working, yay!

Next lets try persisting some data (as soon as 'Maggot Brain' is finish)

JDO or JPA: I've used JDO long time we go, been using JPA since it came out, but there seems to be more documentation for JDO on the appengine site and I can't resist trying unfamiliar technology, so JDO it is.

Let me try vertical development and implement an object first without relationships and do some basic CRUD. later (10:28)

10:30 Going to read the docs about created JPA Entities in eclipse..

Once I've added the annotaions to the class, saving it triggers the enhancer as reported in the output: 'DataNucleus Enhancer completed with success', cool.

10:38 Created the persistent object with id, date, and string fields, painless. Now to look at the PersistenceManagerFactory, which google recommends wrapping as a singleton, I hope to use some DI framework in the project (Guice perhaps), but for the moment I'm going to hack it right into the servlets.


10:43 Alright, singleton copied from google code and modified slightly. Now to add some data.

I'll create a form on the index page with fields and submit it to an action servlet which will persist it (I won't create a service/business tier just yet) and forward back to the index page.

I'll also adding the created object to the request before the forward.
 
Eclipse: oh the irony of your name. hmm, no EL completion in JSP, mff, have to google for request attributes (requestScope, duh).


11:03 and deploy (and enter password)

11:06 Oops, overwrote doGet instead of doPost, fix and publish

The persistence seems to work, except the EL variable ${requestScope.object.field} is being displayed in plain text and not being interpreted, maybe I've missed something - back to the docs.

From the mailing list I discovered kindred soles: one needs to add <%@page isELIgnored="false" %> in the JSP, guess it is true by default. Its a pity they don't turn scriplets off by default, I see the docs show extensive use of scriplets within the JSP - there are beginners opening up a world of pain with scriplets in JSP.

Started using the local server for development.

11:22 yay! working

Next up, create a custom tag for displaying all records.

mothership connection coming to take back the pyramids - having a look at some of the mailing list queries.

11:27 SimpleTagHandler here we go. Ah, man: have to write the TLD by hand (well copy and paste and edit by hand).

do you remember September?

11:39 Almost there, need to look at JDO queries and add Tony Allen to the play list.

11:49 Working locally, now to publish..

11:52 And working live, nice. I think a little break, OK?


I'm impressed and looking forward to writing applications in java on appengine, I might even port my previous application from python to java. Apparently python and java can run side by side and against the same data.

Tweaks I'd like to see so far:
  • Use of EL by default in JSP pages
  • Saving password when deploying (at least for a session)
  • Deploy dialog to include fields for app name/version
  • Email field in the dialog box to be fixed
  • Maybe some better integration between the wizards and GAEJ (e.g. create servlets)

8 comments:

dartdog said...

How about the link to the app on app-engine?

Rick said...

Thanks for the write-up! Please do let us know later about further experiences...

Rick

Brian Silberbauer said...

@dartdog: I can't show you yet, its a work in progress of a real app. I'll let you know when it goes into beta.

@Rick: Only a pleasure, was fun. I will write a follow up as soon as I have time to work on it again.

Another thing I'd like to see added to the framework would be DI of some wort out the box. Maybe a watered down EJB container?

I've used spring before, but I want something a little lighter this time, I hear Guice works, any suggestions would be appreciated.

Francis said...

Nice article!!!

I also have similar problem as u.
now i'm using jpa named query. however, a exception about the "named query was not found" is encountered. Do u have the same problem.? I'm pretty sure I'm doing the right thing as the same code can run on glassfish..

Brian Silberbauer said...

@Francis: I haven't used JPA on app engine yet. Are you sure you have the named query registered with a @NamedQuery annotation, maybe you have a typo?

On the other hand I haven't seen any reference to using named queries, try asking on the mailing list: http://groups.google.com/group/google-appengine-java/.

Christian Gruber said...

Hey. Good article. Small point - for the record, "google" doesn't "recommend" wrapping the persistence manager factory in a singleton - the demo and samples folks merely did so as an expedient. Generally, Google doesn't recommend singletons at all.

http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/

http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html

http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html

While this isn't universally held in all Google code (the GAE does have singletons you access), it is something increasingly promulgated. I would recommend using Guice or some other IoC container, and wrap a service around your persistence manager creation.

Brian Silberbauer said...

@Christian: Thanks for that.

It would be nice if GAEJ supported a DI framework out the box, I would vote for Guice or EJB3's way of doing it (or at least using the same annotations would be an idea).

Brian Silberbauer said...

@Francis: I'm sure you figured this out already, but just in case have a look here: http://groups.google.com/group/google-appengine-java/browse_thread/thread/260dd1ed946b6e11#

Basically, you will need 'register' the entity classes in the persistance.xml configuration using the 'class' element.