Andrew Powell

Into The Mind of A Solutions Architect

Andrew Powell

Remoting From Java To ColdFusion By Example

October 14, 2008 · 2 Comments

This week, I've gotten the chance to work on a pretty slick proof-of-concept for a client. It's always cool to have a little bit of R&D time to play with some ideas. One of the requirements was that we send data as compactly as possible in order to minimize the bandwidth used from client to server over a low-bandwidth connection. Well, if we're trying to be efficient, the most efficient protocol I know is AMF. A few months ago (July 2008), Adobe announced the Java AMF Client as a part of BlazeDS. What this nifty client does is allow you to make AMF remoting calls from Java to any AMF endpoint including, but not limited to: BlazeDS, LCDS, and ColdFusion. Since we were working with a Java client talking to ColdFusion, this seemed, to me, to be the best and most efficient solution for moving data over low-bandwidth connections. Tom Jordahl has blogged about this a little bit, but has not, as yet, posted a complete sample for talking to ColdFusion over AMF from Java. Here is such an implementation for your digestion:

package com.universalmind.util;
   import flex.messaging.io.amf.client.AMFConnection;
   import flex.messaging.io.amf.client.exceptions.ClientStatusException;
   import flex.messaging.io.amf.client.exceptions.ServerStatusException;
   import flex.messaging.messages.RemotingMessage;
   /**
    * @author Andrew Powell
    */
   public class ColdFusionAMFSender {
    /**
    *
    * @param args the arguments, in order required, to pass into the CFC method
    * @return the value returned from the CFC
    * @throws ClientStatusException
    * @throws ServerStatusException
    */
    public Object sendAMFMessage(Object[] args) throws ClientStatusException, ServerStatusException {
    // Create the AMF connection.
    AMFConnection amfConnection = new AMFConnection();
    // Connect to the remote url.
    String url = "http://{yourservername}:{yourport}/{yourcontextroot}/flex2gateway";
    try
    {
    amfConnection.connect(url);
    }
    catch (ClientStatusException cse)
    {
    System.out.println(cse);
    return null;
    }
    String sourceName = "full.path.to.your.CFC";
    RemotingMessage message = new RemotingMessage();
    message.setMessageId(flex.messaging.util.UUIDUtils.createUUID());
    message.setOperation("remoteMethodName");
    message.setBody(args);
    message.setSource(sourceName);
    message.setDestination("ColdFusion");
    return amfConnection.call(null, message);
    }

   }

Tags: Java · ColdFusion · BlazeDS · Adobe · Universal Mind · MOM

2 responses so far ↓

  • 1 David Beale // Oct 15, 2008 at 9:03 AM

    Nice post! I have been playing with code like this in order to do CF -> CF communication as using web services is slow and painful.
  • 2 Andy Powell // Oct 22, 2008 at 3:28 PM

    Well, remember, if you're doing CF to CF comm, you need to install the latest and greatest version of BlazeDS to support this.

Leave a Comment