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.


       //.. create an objectToReturn 
       ServletOutputStream stream = response.getOutputStream();
        byte[] bytes = Base64AmfConverter.getAmfBytes(objectToReturn);
 public static byte[] getAmfBytes(Object obj) throws
        //context = SerializationContext
        Amf3Output amf3Output = new Amf3Output(context);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        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.load(new URLRequest(urlInput.text));

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

And thats all there is do it. I know have some of my amf calls going to urls like:
instead of:

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.


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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: