Action Message Format (AMF) is a high-performance web services protocol that was developed and designed especially for Flash applications by Adobe. AMF is generally considered an RPC protocol (i.e. an optimized alternative to SOAP) because it defines a way to invoke remote methods from a Flash-based client.
Another significant part of the AMF specification, however, is the definition of a data serialization format. This serialization format makes AMF a viable representation (i.e. media type) for REST resources. This offers significant potential for Flash/Flex/ActionScript developers, since the availability of AMF representations for REST resources means that they could work with strongly-typed ActionScript classes without having to translate them to/from XML (or JSON or whatever other format). Instead, the objects could be written and/or read directly to a stream.
This tutorial is intended to show how to provide AMF REST resources in Java using freely-available APIs. How to provide AMF resources on other platforms (e.g. Ruby, .NET, PHP, etc.) is left for another day (and probably another author).
Here's what we do:
- Write the server-side data objects
- Write the client-side data objects
- Write the server-side resource methods
- Write the server-side AMF data provider
- Write the client-side invocation code
- Compile, build, and deploy
For this tutorial, we will be writing a simple address book application. We'll provide a
contacts resource for reading the list of contacts and we'll provide a
contact resource for updating contact information. We'll be using JAX-RS for defining our resource methods and providers, and BlazeDS for (de)serializing our Java objects to/from AMF.
Write the server-side data objects
Pretty basic POJOs:
Write the client-side data objects
The associated ActionScript classes are a reflection of the Java server-side classes. Of course, you have to do some translation, since not all Java types are ActionScript types and vice-versa. You can view some of the rules at Converting data from Java to ActionScript. Note also that if the name of your ActionScript object is different from that of your Java object, you need to register a "class alias" with the
registerClassAlias method. (Here's where a code generator, like Enunciate, comes in really handy.)
Write the server-side resource methods
We just write a pair of simple JAX-RS resource methods. We'll refer you to other resources to learn more about JAX-RS. Note that the resource method produces and/or consumes media type "application/x-amf".
Write the server-side AMF data provider
Again, we use JAX-RS to supply a provider for AMF. Here's what it might look like:
Write the client-side invocation code
What you do with the data is up to you. But here's some sample code that shows how one might make a request to a REST resource and (de)serialize the data:
Compile, build, and deploy
This one's up to you. How you build and deploy is very environment-specific. It also depends on the JAX-RS implementation you're using. And of course, you've got to compile your Flex app, too...
Again, if you're using Enunciate, it's easy--Enunciate will package up everything in a war file for you, which you can drop in your favorite servlet container.