Real World, How To Use Flex Data Services with Coldfusion

Tom Jordahl is scholar and a saint! The only mention of needing to use a CF Mapping to get Flex Data Services to work with Coldfusion that I could find, was in Tom Jordahl's Contact Management tutorial at Adobe.com. He says:

In the ColdFusion MX Administrator, define a ColdFusionMapping for /samples that points to the /samples directory that you created in your web root. This step is required because Flex does not invoke the CFC using HTTP; as a result, the normal web server mappings are not available to resolve the component path.

The samples directory he refers to is the directory that contains your CFCs. Use the CFC wizards to create your CFCs and have them put into that folder. As soon as I created the CF Mapping, I had data in my flex app.

The documentation for getting Flex Data Services to work with Coldfusion is accurate, whatever you use, I've used them all - except for four key real-world steps that I will bring to light right now.

In data-management-config.xml you create a destination. Inside the destination tags is a set of properties tags, and inside the properties tags, there is a set of component tags. This is where you indicate where your CFCs are. Here's what it looks like when properly configured for this example:

<component>samples.contactAssembler</component>
You might think that the first name, samples, is the directory where the cfc's are stored - but no - it is the name of the CF Mapping in CF Administrator. Read Tom's quote above again, "...Flex does not invoke the CFC using HTTP; as a result, the normal web server mappings are not available to resolve the component path." So all it can do is rely on the CF Mapping. ContactAssembler is the name of the cfc.

So what if you put your cfcs into a cfcs folder inside of the samples? Then the code between the component tags would be:

<component>samples.cfcs.contactAssembler</component>
As long as the CF Mapping tells Flex where to start looking for the cfcs, in the Samples directory, it will drill down from there.

Also within the properties tags, there is one other tag you will want some real world info about:

There is a set of tags called hostname, and it is very important to have configured correctly. These tags are to be configured with your Coldfusion Hostname. Default is localhost.

I have a development environment that uses a domain name, not localhost. In this set of tags you enter the base URL to your Coldfusion Host (www.myCFServer.com). I should point out that the URL I use to browse my Flexapp is different. Remember that I mentioned in my previous blog entry that using the JRun Web Config tool on an IIS site to connect the IIS site with JRun, renders CF broken; it won't run CF files anymore. Therefore you must use separate IIS sites for CF and Flex apps. I use a hostheader in a separate IIS site for my flex app like this: flexapp.myCFServer.com. But in this set of hostname tags to find the CFCs, I use the IIS site for browing Coldfusion sites (www.myCFServer.com). You should be able to browse right to the CFC and it will auto-launch the CFC Explorer after logging in.

You will get a number of errors if you don't configure everything correctly. I should mention that I have not done anything to the services-config.xml except to copy and paste the RMTP and AMF Polling CF connection code as mentioned in all of the tutorials I have seen for Flex Data Services. I have made no alterations there.

Here are a couple of errors that I got really used to seeing which you might encounter:

[RPC Fault faultString="Unable to contact the RMI registry on 'www.myserver.com' to look for the ColdFusion Data Management Service: java.rmi.ConnectException: Connection refused to host: www.myserver.com; nested exception is:

java.net.ConnectException: Connection timed out: connect"
faultCode="Server.Processing" faultDetail="null"]
There's a technote for this error here. It basically says you have to have port 1099 open on your firewall. That's sort of an important detail that I haven't seen in any tutorial.
[RPC Fault faultString="Error invoking fill-method 'fill' for destination dealers: Error invoking CFC fill operation:Could not find the ColdFusion Component samples.ContactAssembler." faultCode="Server.Processing" faultDetail="null"]
This is what you get when you don't have a CF Mapping configured in your CF Administrator. You can have a samples directory; you can have a ContactAssembler.cfc within the samples directory; and you can even browse to the cfc in a browser; but if you don't have the CF Mapping called /samples then you will get the above error.

Ok so having the benefit of this knowledge (which took me about 12 hours to figure out on my own) to add to your favorite FDS with CF tutorial, should drastically speed up your success so you can move on with your Flex app more quickly than I did. I hope this indeed helps you.

If you happen to see Tom Jordahl, thank him for me. I couldn't have figured it out without him.

Comments
João Fernandes's Gravatar Jim,

what happens if your app switch from rtmp connection to cf-polling? Does it still works?
# Posted By João Fernandes | 3/15/07 10:45 AM
Jim Pickering's Gravatar Joao - That's an intriguing question. I commented out the rtmp channels in my data-management-config.xml file and stopped JRun in the Command Prompt window and the CF Service. I deleted my flexapp file in the JRun context root, so Flex would build a new one. This removes all doubt about cacheing. Then I started CF again; started JRun in a Command Prompt window, and ran my Flex app. Yes, it still worked.
# Posted By Jim Pickering | 3/15/07 3:37 PM
João Fernandes's Gravatar Well if you FDS is running on his own domain (flexapp.yourdomain.com) and you bind the FDS JRun instance to that website, it should work yes because all requests for polling will be asking for flexapp.yourdomain.com/{context-root}/messagebroker/amfpolling/

so it must work. :)

Anyway Ben mentioned on his blog
"Flex Data Service can be integrated in to the ColdFusion installation. This way ColdFusion can call Flex API's directly (rather than via the current RMI connection) providing a signification performance boost."

This will for sure also avoid all this kind of installations.
# Posted By João Fernandes | 3/16/07 4:25 AM
Jim Pickering's Gravatar True, but not all of us can use the Enterprise Edition of Coldfusion. Ben is talking about using CF's integrated JRun which is great, but my small clients won't spend $6000 for CF Enterprise. $2200 for the Stand Alone JRun and CF Standard is not out of the question for most small clients - so that is the environment that I am using.

Perhaps with Scorpio they'll switch features around so that small clients can get the performance boost of using FDS with CF's JRun in CF Standard.
# Posted By Jim Pickering | 3/16/07 2:59 PM
João Fernandes's Gravatar Maybe they will switch, maybe not... but they're doing something...
Try get into the beta :)

Anyway if you have any questions about CF & FDS feel free to ping me offline.
# Posted By João Fernandes | 3/16/07 3:09 PM
Andrew's Gravatar Oh, thanx! I have had the same problem and now i know some nice facts about Coldfusion and Flex...

http://drinkmenow.info
# Posted By Andrew | 8/29/07 6:54 AM
WinBiLL's Gravatar Thanks for very interesting article. btw. I really enjoyed reading all of your posts. It’s interesting to read ideas, and observations from someone else’s point of view… makes you think more. So please keep up the great work. Greetings.
# Posted By WinBiLL | 9/28/07 6:59 AM
be4you's Gravatar Thanks for your post helpfull
# Posted By be4you | 10/27/07 5:38 PM
Copyright ©2007 JimPickering.com. Some rights reserved. BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.