Skip to content
August 9, 2010 / edeustace

Serving cacheable amf

Last week two things came together that has allowed me to come up with a simple solution to an issue I was looking at on my current project.

Firstly I was speaking with Xavi Beumala, he was explaining about some architectural ideas he was having for a new project. He was describing the Apache Sling project. Which provides a resource centric api for your data calls. He was talking about adding an amf converter to the existing converters (json/xml).

A day or two later, I was look at opportunities for optimising the performance of the application I’m currently working on. The application makes a lot of Remote Object AMF based calls during initialisation for almost static data. As the calls are made as Http POST calls they are never cached. This got me to thinking about Xavi’s idea of serving AMF over unique Urls. If I could do that for these calls, it would mean that they could make use of standard browser/server caching mechanisms like “Expires”, “Last-Modified” & “ETag”.

So I decided to see how easy it would be to serve AMF over a servlet. With some help from Xavi, I got it working.

Servlet:

       //.. create an objectToReturn 
       ServletOutputStream stream = response.getOutputStream();
        byte[] bytes = Base64AmfConverter.getAmfBytes(objectToReturn);
        stream.write(bytes);
 public static byte[] getAmfBytes(Object obj) throws java.io.IOException
 {
        //context = SerializationContext
        //see: http://snipplr.com/view/7820/serialization-of-java-object-to-amf/
        Amf3Output amf3Output = new Amf3Output(context);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        amf3Output.setOutputStream(bos);
        amf3Output.writeObject(obj);
        amf3Output.flush();
        amf3Output.close();
        byte[] data = bos.toByteArray();
        return data;
 }

Then on the client side you need to use the URLLoader class and set the return format to binary:

private function load():void
{
	loader = new URLLoader();
	loader.dataFormat=URLLoaderDataFormat.BINARY;
	loader.addEventListener(Event.COMPLETE,onComplete);
	loader.load(new URLRequest(urlInput.text));
}

private function onComplete(event:Event):void
{
	var data : ByteArray = ByteArray(event.target.data);
	var obj : * = data.readObject();
	log(obj);
}

And thats all there is do it. I know have some of my amf calls going to urls like:
GET http://myapp.com/getAppInitialisationData
instead of:
POST http://myapp.com/messagebroker/amf

Now all I have to do is add some caching logic to my Servlet to decide on what I need to set for my caching policy.

I’ve uploaded example code here if anyone is interested.

Advertisements

One Comment

Leave a Comment
  1. poweramp full version apk / Mar 27 2014 4:55 pm

    This blog was… how do you say it? Relevant!! Finally I have found something that helped
    me. Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: