Call Encore now on +44 (0) 1273 722 544 or contact us
Monday 6th, February 2012

BlazeDS and the art of lazy loading

Posted by Jethro Grassie on May 13, 2009

A while back I had some fun playing with Flex, Spring, BlazeDS and Hibernate.

We recently won a contract to create quite a large on and offline application that we are implementing the GUI in Flex and the back-end in J2EE.
Given how much we like Spring and Hibernate, and how useful BlazeDS is, I have spent a bit of time again getting these all playing nicely together.

Firstly there is now a Spring and BlazeDS integration kit available which moves the configuration of BlazeDS completely to Spring. I really wanted this in our latest setup as it is far more elegant than the mish-mash of configuration files before.

Secondly there are now a couple of lazy loading frameworks for BlazeDS and Hibernate. We identified Gilead and dpHibernate as contenders. We really wanted decent automatic lazy loading for this particular project due to the size of the data model. Lazy loading is critical in any Flex-BlazeDS web app that is going to have a large and deep data model, otherwise the whole data model will have to go over the wire when only little bits of it are actually needed.

Getting this all working together nicely was not however straight forward!

Firstly I spent some time evaluating Gilead. At the time of writing, this simply doesn’t do what it says on the tin.
I was expecting automatic lazy loading but Gilead does not do this. Rather, it expects you to make the calls and what it does offer is the merging and cloning of objects. This solution is not a complete lazy loading solution and still would require quite a lot of code around it to achieve automatic lazy loading. So I moved onto dpHibernate…

And this gladly does do what one would expect! The source code is also very nicely written and well laid out, which made a thorough examination quite easy.

Next up was getting the Spring and Flex integration kit setup.
This went fine until trying to configure in dpHibernate, as the M2 release (which was current release at the time), didnt have support for wiring in and configuring a custom BlazeDS adapter, essential for any lazy loading solution.
Luckily, in the trunk was work by the very talented Jeremy Grelle which seemed to be the solution.
Getting it to actually work took a bit of debugging but I finally got there and documented the core stumbling block here.

So what we end up with is a Spring configured BlazeDS web app with a Hibernate persistence layer with lazy collection loading.

As I final point I want to highlight the issue of LiveCycle Data Services, which is Adobe’s commercial offering of BlazeDS, and does handle lazy loading out of the box.
My problem here was that after 6, yes SIX, attempts to get a cost from Adobe for LiveCycle DS, Adobe failed to ever respond (I tried all the contact options detailed on the product page).
Apart from lazy loading, the other reason we quite liked the idea of LiveCycle DS was the on/off-line data synchronization it has for AIR applications (as we will also be creating an AIR app for this particular project). Instead we will roll are own framework for this bit, and likely open-source it. So Adobe, if your listening, you lost a sale here by simply not responding. I wonder how many other potential sales have been missed?

A framework mashup

Posted by Jethro Grassie on November 16, 2008

Recently we have been working on another web app – or RIA as some would coin.
When we do server side stuff at Encore, we often make use of Java, J2EE more specifically.
This particular project required a flex front end.

On the server then, we do our usual hibernate for persistence and spring for the IoC which all yields a nice well designed and configurable server side web app.
What made this job a little more interesting was we wanted to have a play with one of Adobe’s recent open-source contributions – BlazeDS.

Among other things, BlazeDS gives us remoting, which is what we wanted for this particular project. We can call our remote Java services and pass actionscript objects to the server, these are serialized to and sent over as AMF. BlazeDS (the server-side J2EE web app), deserializes this AMF into Java objects. When returning objects to the client, BlazeDS serializes the Java object(s) and then the client side flex app, deserializes the AMF back into actionscript objects. Neat.

However, all good and well, what happens when you want to integrate BlazeDS into the rest of your J2EE infrastructure?
Luckily enough, BlazeDS does allow for the configuration to assign a factory class to deal with looking up your services. So we write a factory that returns Java beans (our service classes) we have configured via spring. BlazeDS calls the appropriate methods on the service bean passing in the deserialized AMF method parameters. The result is then serialized and sent back to the client flex app. Sweet.

One gotcha we faced was this…
As we are using hibernate for persistence, we could (and needed to) make use of lazy collections. That is, when a persistent object holds any kind of collection, hibernate can return empty proxy objects in the collection that only actually retrieve the value when the caller asks for it (eg iterating over the collection). This is a great way to reduce overhead, but Blaze’s implementation just doesn’t handle this out of the bag, so when returning a lazy collection, flex simply couldn’t retrieve the objects in that collection.
Now with large tree structures of data objects, we couldn’t simply switch off the lazy loading in our hibernate mappings to allow returning to flex the whole object tree, so we had to look to keeping it lazy. Not difficult, we just had to add in the necessary service calls for this projects data model.

In summary, we got BlazeDS, hibernate and spring all working together nicely, just would have been nice if BlazeDS handled lazy collections for us. They are commonly essential for an enterprise level application.
I would say, at the moment, the superb Granite Data Services is a better option at present over BlazeDS.
It handles all that BlazeDS does and more. It also deals with the lazy loading issue ;)

References

BlazeDS: http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/
Granite Data Services: http://www.graniteds.org/
Spring: http://www.springframework.org/
Hibernate: http://www.hibernate.org/
AMF specification [pdf].

Like what you see? Then get in touch;
t. +44 (0) 1273 722 544   e. contact us