March 16, 2010

BlazeDS: Creating destinations at runtime

Because it is already said a lot on this subject, I won't waste much words, but go straight to the point/code.
/**
* Create blazeds destination at runtime. Follow SVN link to java source files for more details.
* @name hint="Destination name" 
*/
function createDestination(destname,channelId){
 var local = structNew();
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/MessageBroker.java
 local.brokerClass = createObject('java','flex.messaging.MessageBroker');
 local.broker = local.brokerClass.getMessageBroker( javacast('null','') );
 local.service = local.broker.getService('message-service');
 local.destinations = local.service.getDestinations();
 
 //check if destination already exists. If it does just return it
 if (structKeyExists(local.destinations,arguments.destname)){ //check if destination already exist
  return local.destinations[arguments.destname]; 
 }
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/Destination.java
 local.destination = local.service.createDestination(arguments.destname);
 // Creates a ServiceAdapter instance and sets its id, sets if destination  is manageable
 local.destination.createAdapter('cfgateway');
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/common/src/flex/messaging/config/ConfigMap.java
 local.configMap = createObject('java','flex.messaging.config.ConfigMap').init();
 local.configMap.addProperty('gatewayid',application.CFEventGatewayId);
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/config/ServerSettings.java
 local.ss = createObject("java","flex.messaging.config.ServerSettings");
 local.ss.setAllowSubtopics(true); 
 local.ss.setSubtopicSeparator('.');
 local.ss.setDurable(false);
 
 local.destination.setServerSettings(ss);
 
 local.adapter = local.destination.getAdapter();
 
 //Initializes the adapter with the properties.
 local.adapter.initialize('cfgateway',configMap);

 local.destination.addChannel(arguments.channelId);
 
 //Starts the destination if its associated Service is started and if the destination is not already running. 
 local.destination.start();
 
 return local.destination;
}
Note that I placed svn links to BlazeDS java source files. While I was working on this and especially endpoint creation, insight to these files was invaluable to me. Each method is nicely documented, so it is very easy to understand.

More on this subject:

2 comments:

Tony Nelson said...

I might be missing something, but I think you want to replace arguments.roomname with arguments.destname.

Unknown said...

You are absolutely right. Fixed. Thank you.