독도 광고 모금 캠페인

출처 : adobe devnet

Creating BlazeDS channels at runtime

by SujitG on July 10, 2008 Avg Rating 5.0 (1)   |   Log in to rate post.

Tagged with dynamic channels , flex and blazeds , Flex and other technologies , runtime channels

Problem Summary

Usually we create channels in services-config.xml and give URL to channel end points in the same file. In services-config.xml {server.name} and {server.port} tokens are evaluated at runtime, but the {context.root} is not evaluated at runtime. If we have to change the URL along with the web application context root, then we have to re-compile Flex application.

Solution Summary

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.

Explanation

Usually we create channels in services-config.xml and give URL to channel end points in the same file. {server.name} and {server.port} tokens are evaluated at runtime, that is these tokens are replaced based on the URL you are using to access the application. But the {context.root} is not evaluated at runtime. If we have to change the URL along with the web application context root, then we have to re-compile Flex 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.

I created a very simple 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

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
Posted by 이현호
,