Usually we create channels in services-config.xml and give URL to channel end points in the same file. This is good if the server IP or the domain name will not change. What if we are developing on our system and deploying to the production server? We need to change the end point URL in the services-config.xml and recompile our application. It will be great if you can change the end point URL at one location and the Flex application will start using that URL without a need for recompiling the application.
Solution is simple you will have to create channels on runtime. This is very straight forward and easy.
You need not even add your services-config.xml to the compiler arguments.
I created a sample application which will access a XML file and then create the channels based on the settings in the configuration file.
XML configurations file with channel details
I created a XML file which has details of the channels which my application has to create. If you see the snippet extracted from the XML file it has definition for AMF channel. In the definition the id is the ID of the channel configured in the services-config.xml file on the server and the endpoint node has the URL to the end point.
Download XML from this URL: ChannelsConfiguration.xml
<ChannelsConfig>
<channels>
<channel id=”my-amf”>
<type>amf</type>
<endpoint>http://localhost:9191/lcdssamples/messagebroker/amf</endpoint>
</channel>
</channels>
</ChannelsConfig>
Creating Flex application
Download MXML file from this URL: DynamicChannels.mxml
Now that I have a XML file with details of the channel my application will be using. I loaded this file when my application starts and create required channels so that my application can communicate with the server for using Remoting/Messaging/Proxy/Data management services provided by LCDS/BlazeDS.
Once the application is created function named loadConfiguration() is invoked which will make a HTTP Service request to get the XML file. Check out the function, it is pretty straightforward HTTP Service call.
When the XML is retrieved I parse the XML file and create channels in the parseConfigurationFile() function. Below are few statements extracted from the parseConfigurationFile() function which are worth explaining J
First I create a new channel.
_amfChannel = new AMFChannel(channel.@id, channel.endpoint);
Next add this channel to the ChannelSet.
amfChannelSet = new ChannelSet();
amfChannelSet.addChannel(_amfChannel);
That’s it we have created the channel sets which we will be using in the RemoteObject call later. I created more channels
Now that I have the channel sets I need to instruct my RemoteObject to use this channel sets when it is trying to communicate with the server. This is how you do it.
<mx:RemoteObject id=”rmObj” destination=”MySessionHandler”
channelSet=”{amfChannelSet}”
result=”resultHandler(event)”
fault=”faultHandler(event)”
showBusyCursor=”true”/>
That is all you need to do
you can invoke operations on RemoteObject as usual. This applies to RemoteObject/Consumer/Producer/DataService