A simple approach to Flex ColdFusion integration
A few weeks ago, I decided to challenge myself and start a side project that involves a Flex front end with a ColdFusion backend. This was not only my first Flex+ColdFusion project, it was my first hands-on experience with ColdFusion!
Like most folks, I created a new Flex project in Flex Builder and selected ColdFusion as the “Server technology” in the New Flex Project dialog box. This triggered another dialog which prompted me for information about my local installation of ColdFusion. Afterwards, I was staring at a blank main.mxml file that magically had the ColdFusion connectivity taken care of. That’s fine, but I’m the type of guy that wants to understand what has been done behind the curtain. Also, I wondered how I would handle things when I moved my application to a server that was running a different configuration, etc. Since all of the configuration is “baked in”, I wasn’t sure how I was going to handle it.
I had recently read Christophe Coenraets blog post title, “Externalizing Service Configuration using BlazeDS and LCDS” which addresses the same issue with configuring Flex applications to use BlazeDS or LiveCycle DS. After reading Christophe’s blog post, I was able to quickly convert my Flex application to integrate with ColdFusion without any special compiler options or any knowledge of my local file structure, and so on. After a little trial and error, I am now able to create a plain Flex project with the “Application server type” set to None and handle all of the remoting/messaging setup directly in my code.
My application uses simple remoting to call a ColdFusion components (orderManage.cfc). It also utilizes LCDS messaging so I need two channel sets.
I start with a global variable for each of my channel sets:
private var messagingChannelSet:ChannelSet; private var remotingChannelSet:ChannelSet;
In my MXML, I setup my remote object and my consumer/producer:
<mx:RemoteObject id="cfsvc" destination="ColdFusion" source="orderManage"> <mx:method name="makeOrder" result="makeOrderResultHandler(event)" /> <mx:method name="getOrder" result="getOrderResultHandler(event)" /> </mx:RemoteObject> <mx:Consumer id="chatConsumer" destination="appComm" message="chatListener(event)" /> <mx:Producer id="chatProducer" destination="appComm" />
In my init(), I setup my channels, add them to the channelset and then setup my consumer and producer:
remotingChannelSet = new ChannelSet(); var rChannel:AMFChannel = new AMFChannel("my-cfamf", "http://localhost/flex2gateway"); remotingChannelSet.addChannel(rChannel); cfsvc.channelSet = remotingChannelSet; messagingChannelSet = new ChannelSet(); var mChannel:AMFChannel = new AMFChannel("cf-longpolling-amf", "http://localhost/flex2gateway/amflongpolling"); messagingChannelSet.addChannel(mChannel); chatConsumer.channelSet = messagingChannelSet; chatProducer.channelSet = messagingChannelSet; chatConsumer.subtopic = instanceID + ".CHAT"; chatConsumer.subscribe();
The rest of the application code continues as normal. I still need to externalize the URL that is hardwired into my code. I’ll probably use a technique similar to Christophe’s example.
I can now call functions in my orderManage.cfc component like this: cfsvc.makeOrder(….), etc.
Today, I found a blog post by Mike Nimer that covers this same topic from a slightly different perspective. Mike also has some great tips, including how to externalize the context root and other settings.
I’ll blog more about my application as it nears completion.
PS: This is my first ColdFusion blog post. I hope my boss, Ben Forta is proud! 😉