Tuesday, May 12, 2009

Cloud to RIA: Accessing Google App Engine (Java) Data Through Flex AMF - Part I

[Part I] [Part II] [Part III]


and RIA (Rich Internet Application) are probably the hottest buzzwords today in the web world. You can look at cloud as a virtual server that provides single-point access to a distributed (often across the globe) collection of hardware/software and coming in three shapes:
  1. IaaS: Infrastructure as a Service. A good example is Amazon EC2, where you basically get a virtual machine (Amazon Machine Image) on which you install what you want.
  2. PaaS: Platform as a Service. This adds infrastructure software to IaaS so you don't start from scratch. Examples are Google App Engine (GAE) and Salesforce Force.com.
  3. SaaS: Software as a Service. Here, you get the full package that you tweak/customize to your needs. Best example of course is Salesforce.com.
Clouds have many benefits, most importantly:
  1. You don't need to plan/invest in hardware/software upfront
  2. Your infrastructure will scale as your business/needs grow
  3. You system will be fault-tolerant and secure
  4. You don't need to maintain/manage the setup
In this example, I'm using GAE (Java) for the cloud. Google had required developers to use Python for GAE till recently, when they announced a Java version. All references to GAE in this write-up refer to GAE (Java). GAE is free to get started; you will need a Gmail account.

An RIA is an advanced web (or desktop) front-end that's more sophisticated than traditional HTML/CSS/JavaScript web apps. The dominant web RIA today is Adobe Flex, although Microsoft Silverlight is gaining rapidly. Sun (Oracle) recently announced JavaFX that's gaining some traction in the Java community. We will use Flex as the RIA in this example; you will need Flex Builder (free trial for 60 days) and Eclipse.

Technology Choices

Once we've decided to hook up a Flex RIA to a GAE cloud, we need to look at some specifics. First, how do you represent the data on GAE? GAE (Java) does not allow saving to files on the server, so our only option is to save on the Google BigTable. In fact, it's a better option anyhow. Luckily, the new GAE has a standards-based Java persistence API (rather than the Google-proprietary API with Python): JDO and JPA. I'm using JPA for this example.

Next, the interchange data format. Popular message formats with Flex are: XML (SOAP or REST), JSON, and the Adobe-proprietary AMF. XML is verbose and although accesss through REST performs reasonably well, more succint representations like JSON are better for larger chunks of data. But the most efficient (and strongly typed) representation is with AMF, which is what we will use for this example. To employ AMF for the data exchange, we use the Flex RemoteObject.

Adobe has made the AMF spec public, and there are multiple free implementations, probably the most important of which are: BlazeDS, WebORB, and GraniteDS. I've tried all the three with GAE. BlazeDS never worked for me, and as of today I'm not aware of any official fixes, though there are blog reports of unofficial workarounds. WebORB kind of worked with their fix in the sense I could move data through POJO interfaces to data, but not the persisted classes themselves. I also had performance issues. GraniteDS worked like a charm, and that's what we'll use in this example. Do keep checking Will it play in App Engine for the latest info.


To get started, you need to do two things right away:
  1. Get a GAE account. This is easy and free: just sign up at Google App Engine.
  2. Get a GAE (Java) access. Sign up at GAE (Java) Sign-up.
Once you've done this, let's get going with the real thing... in Part II.


decliffy said...

An excellent blog entry...looking forward to part II

Puran said...

ok i am on a queue too...

Sekhar Ravinutala said...

Thanks guys, just posted Part II.

Mark Piller said...

What kind of performance problems did you run into with WebORB? As for transfering VOs, I am not aware of any problems there and the examples we posted online clearly demonstrate that functionaloty.

Sekhar Ravinutala said...

The performance issue was that there'd be big delay (several seconds) at the start of each browser session. Felt like each session was getting a new EM factory, as if the scope was session, even though I clearly specified application. I did post on your forums, but didn't hear back. This was with GAE 1.0 though, may be this is not an issue with the new 1.2.1.

And the problem with transferring was specifically with returning entities directly. E.g., I had an auto-generated id and a return em.find(Person.class, id) would fail. If I created a VO with the entity's contents, I could send the VO back without any problem. Possibly an issue with the generated id. Again, this was with 1.0.

Cliff Meyers said...

AMF is not really proprietary. The AMF specification is open and available from this page:


AMF serialization has been implemented in Java via BlazeDS which is open source.

Sekhar Ravinutala said...

Yes Cliff, Adobe has made the spec public, and GraniteDS (the one I used) is in fact one of the implementations based on that public spec. WebORB is another popular implementation, as I've noted in the blog.

Ji Kim Lucas said...

Thank you. it is really great blog. I have been searching a good blog how to work with Google App Engine.

CC said...

Interesting article! Thx

Paul Georges said...

I'm getting endless class not found errors... the latest that I don't quite understand is this one when trying to run locally...

Caused by: java.lang.ClassNotFoundException: org.granite.messaging.amf.io.convert.impl.GAEKeyConverter

and this one on GAE...

[RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Status 500: url: 'http://granitedsgae.appspot.com/graniteamf/amf'"]

any suggestions?

Anonymous said...

Great article, but why on earth do you compare Adobe Flex to JavaFX or even MS Silverlight?
Both are crap.(tested and prooved)

Naviya Nair said...

Thanks for the post, I am techno savvy. I believe you hit the nail right on the head.
I am highly impressed with your blog. It is very nicely explained.
Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.