Thursday, November 4, 2010

Dynamic , Static creation of AMFChannel for accessing Java methods using Remote Objects

In one of the projects I am working on , we are using Flex ,Spring BlazeDs integration . Initially we configured the RemoteObject using dynamically created AMFChannel. Later on due to some changing requirements , we changed to read the ChannelSet definition and endpoint URL mapping from services-config.xml.  Thought many of the Flex developers may be looking for something like this and hence sharing what needs to be done.

RemoteObject with dynamic AMFChannel and ChannelSet.
1)      My services-config.xml has following channel definition. (As I am using Flex –Spring- BlazeDs inetgeration hence the spring/messagebroker/amf )
              <channels>
                       <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
                                    <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf"
                                                class="flex.messaging.endpoints.AMFEndpoint"/>
                                                <properties>
                                <record-message-times>true</record-message-times>
                                <record-message-sizes>false</record-message-sizes>
                                </properties>
                                </channel-definition>
                  </channels>
2)      Create dynamic AMFChannel and ChannelSet

var amfChannel = new AMFChannel("myamf", “/DemoApp/spring/messagebroker/amf”);
                where DemoApp is the context-root of my application. One can also store the above URI into a properties file and use ResourceManager to get the string at runtime, rather using the hardcoding.
var channelSet = new ChannelSet();
channelSet.addChannel(amfChannel);

3)      Create RemoteObject
Have created a public function which returns a RemoteObject instance .
The destination is the id of the destination mentioned in services-config.xml or incase of spring-blazeDs integration it’s the bean id defined in applicationContext.xml.

public function getService(destination:String):RemoteObject
                                {
                                                var remoteService:RemoteObject = new RemoteObject();
//reference to channelset we created dynamically in step 2.
                                                remoteService.channelSet = this.channelSet ;
                                                remoteService.destination = destination ;
                                                remoteService.showBusyCursor = true ;
                                                return remoteService ;
                                }
               
RemoteObject with AMFChannel and ChannelSet.defined in services-config.xml

1)      Services-config.xml , we difine a channel-definition like below.


<channel-definition id="demoApp-amf"

class="mx.messaging.channels.AMFChannel">

<endpoint url="http://{server.name}:{server.port}/DemoApp/spring/messagebroker/amf"

class="flex.messaging.endpoints.AMFEndpoint"/>

<properties>

<record-message-times>false</record-message-times>

<record-message-sizes>false</record-message-sizes>

</properties>

</channel-definition>

2)      Create AMFChannel and Channel set from entry provided in Services-config.xml
var channeled:String  =  demoApp-amf”;
var amfChannel:AMFChannel  = null ;
try{
if(ServerConfig.getChannel(channelId)!=null){
amfChannel = new AMFChannel(channelId,ServerConfig.getChannel(channelId).endpoint);                                                     
}
                                                }catch(error : InvalidChannelError){}

var channelSet = new ChannelSet();
channelSet.addChannel(amfChannel);

3)      Create RemoteObject
Have created a public function which returns a RemoteObject instance .
The destination is the id of the destination mentioned in services-config.xml or incase of spring-blazeDs integration it’s the bean id defined in applicationContext.xml.

public function getService(destination:String):RemoteObject
                                {
                                                var remoteService:RemoteObject = new RemoteObject();
//reference to channelset we created dynamically in step 2.
                                                remoteService.channelSet = this.channelSet ;
                                                remoteService.destination = destination ;
                                                remoteService.showBusyCursor = true ;
                                                return remoteService ;
                                }


No comments:

Post a Comment