AMFConnection for AMF remoting in Java

In couple posts back, I talked about using NetConnection to talk to a remoting destination in BlazeDS/LCDS. This helps for pure Flash clients talk to BlazeDS/LCDS but what if you have a Java client app and you want to do a remoting call from your Java app, is this possible? That’s when AMFConnection comes into play.

I created this nice little class in BlazeDS source tree a while back. It tries to mimic Flash’s NetConnection in Java (hence the name AMFConnection) and inspired by Peter Farland‘s AMFConnection. It uses BlazeDS’ AMF serializer/deserializer (which means you need flex-messaging-common.jar and flex-messaging-core.jar from BlazeDS in your classpath) and it can potentially be used from any Java code to serialize/deserialize AMF messages. Internally, Java’s HttpURLConnection is used to send/receive HTTP request/replies and can be used to perform remoting calls to BlazeDS, LCDS, or any other server technology that understands AMF from your Java client.

You would use AMFConnection pretty much like NetConnection. One notable difference is that calls are not asynchronous which makes sense for Java. Here’s a brief example:

// Create the AMF connection.
AMFConnection amfConnection = new AMFConnection();

// Connect to the remote url.
amfConnection.connect(“http://localhost:8400/team/messagebroker/amf”);

// Make a remoting call and retrieve the result.
try
{
Object result = amfConnection.call(“remoting_AMF.echo”, “Hello World!”);
}
catch (ClientStatusException cse)
{
// Handle exception.
}
catch (ServerStatusException sse)
{
// Handle exception.
}

// Close the connection.
amfConnection.close();

A full example can be found in this JUnit test in BlazeDS source tree.