March 16, 2010

BlazeDS: Creating endpoint at runtime

Why? Because I do not want to bother end users with xml configuration files. Simple as that.
Biggest enemy of your application is user itself. Therefore, reducing possibility to make a mistake is most important goal if you wish to left good impression with your app. This was one of steps that I take on the road to make Collyba installation procedure as easy as possible.
As I said in previous blog, while I was working on this, insight  to BlazeDS source files was of great help. Actually that is where I found a solution.

What are channels and endpoints?
Channels are client-side objects that encapsulate the connection behavior between Flex components and the BlazeDS server. Channels communicate with corresponding endpoints on the BlazeDS server. You configure the properties of a channel and its corresponding endpoint in the services-config.xml file.
But in our case, we won't configure it in XML file but programmatically.

* Creates endpoint. Run this before creating destination.
* @id hint="Channel Id which communicate with corresponding endpoints on the BlazeDS server."
public void function createEndpoint(required string channelId){
 var brokerClass = createObject('java','flex.messaging.MessageBroker');
 var broker = brokerClass.getMessageBroker( javacast('null','') );
 var channelSets = broker.getAllChannelSettings();
 var chennIds = broker.getChannelIds();
 //check if channel/endpoint is already created
 if (arrayContains(chennIds,arguments.channelId)){
 /*Channel definiton properties*/
 var channelSettings = createObject('java','flex.messaging.config.ChannelSettings').init(arguments.channelId);
 var channelProperties = createObject('java','flex.messaging.config.ConfigMap').init();
 var chPropSerialize = createObject('java','flex.messaging.config.ConfigMap').init();  
 var endpoint = '';


 /*Channel definiton endpoint*/
 //Creates an Endpoint instance, sets its id and url
 endpoint = broker.createEndpoint(
 // Initialize the endpoint with id and properties
 endpoint.initialize(arguments.channelId, channelSettings.getProperties());
 //Start the endpoint
 channelSets[arguments.channelId] = channelSettings;

XML equivalent of  this channel/endpoint definition ( set in services-config.xml) is:
<channel-definition id="collyba-longpolling-amf" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://{}:{server.port}{context.root}/flex2gateway/cfamflongpolling" class="flex.messaging.endpoints.AMFEndpoint"/>

There are 2 crucial points here:
a) flex.messaging.config.ChannelSettings class holds all information from channel-definition definition element from services-config.xml file
b) createEndpoint method call. This is a place when we create Endpoint instance assign it to MessageBroker

Easy, isnt't it. Well it wasn't for me. But, knowing that I'll save you at least a day of R&D makes me happy :)


Raymond Camden said...

Thank you for sharing your last two blog entries. Any chance you can submit these to CFLIb?

Marko said...

Glad you like it Ray.
Sure I can and will. When it comes to endpoint creation, I think it should be re-factored a bit to make it more flexible. There are many different channel/endpoint configuration sets and this is example of just one.
Thinking of adding additional attribute which will be an XML with channel properties () that will be translated into ConfigMap structure.

However, If you think that is ok to submit it "as is", I'll do it and upgrade it later.

Destination method can be used "as is".