Using LCDS runtime configurations with CF8

Before LCDS 2.5 came out, each time we needed a new destination, we had to change one of the configuration files (data-management, remoting, messaging or proxy) and then restart the server so we could use it in our applications. In LCDS 2.5, this new feature allow us to create new destinations without having to restart the server or configure any xml file.

Unfortunately, CF8 doesn't provide any specific API to do this but, when using LCDS merged with CF8, we can use the JAVA classes to create those destinations.

Using the Tom Jordahl CRM example as reference, when we have a destination like this

<destination id="cfemployee">
<adapter ref="coldfusion-dao" />
<channels>
<channel ref="cf-rtmp"/>
<channel ref="cf-polling-amf" />
</channels>
<properties>
<component>samples.crm.EmployeeAssembler</component>
<scope>request</scope>
<metadata>
<identity property="employeeId" />
<one-to-one property="company" destination="cfcompany" lazy="true"/>
</metadata>
</properties>
</destination>

the same can be achieved at runtime this way

<cfset brokerClass = createObject('java','flex.messaging.MessageBroker')>
<cfset broker = brokerClass.getMessageBroker( javacast('null','') )>
<cfset service = broker.getService('data-service')>

<cfset destination = service.createDestination('cfemployee')>
<cfset destination.createAdapter('coldfusion-dao')>

<cfset configMap = createObject('java','flex.messaging.config.ConfigMap').init()>
<cfset configMap.addProperty('component','samples.crm.EmployeeAssembler')>
<cfset configMap.addProperty('scope','Application')>

<cfset adapter = destination.getAdapter()>
<cfset adapter.initialize('coldfusion-dao',configMap)>

<!--- start of channels --->
<cfset destination.addChannel('cf-rtmp')>
<cfset destination.addChannel('cf-polling-amf')>
<!--- end of channels --->

<!--- start of metadata --->
<cfset metadata = createObject('java','flex.data.config.MetadataSettings').init()>
<!--- Identity field --->
<cfset metadata.addIdentityPropertyName( 'employeeId' )>
<!--- Associations : one-to-one, one-to-many --->
<cfset Association = createObject('java','flex.data.config.AssociationSetting').init('one-to-one','company','cfcompany')>
<cfset Association.setLazy( true )>
<cfset Association.setCacheReferencedItems( false )>
<cfset metadata.addAssociationSetting( Association )>
<!--- end of metadata --->
<cfset destination.setMetadataSettings( metadata )>
<cfset destination.start()>

We first invoke the broker which is the responsible for all destinations. Then we get the service where we want to store our new destination. The data-service value correspond to the id in the service node in the data-management-config.xml. So if we wanted to add a new messaging destination we would use 'message-service' instead. We initialize the destination by creating a new adapter and injecting a configMap which carries that destination configuration.

We add the channels that will be available for that destination.

The metadataSettings object is where we are going to define all primary keys and relationships between destinations.

After assigning the metadata, we can start the destination and use it.

in the flex side, we just have to use this code to use the brand new destination.

dsEmployee = new DataService("cfemployee");
cs = new ChannelSet();
cs.addChannel( new RTMPChannel('cf-rtmp','rtmp://{server.name}:2048') );
cs.addChannel( new AMFChannel('cf-polling-amf','http://{server.name}/flex2gateway/cfamfpolling') );
dsEmployee.channelSet = cs;
dsEmployee.fill( someArrayCollection [ , your arguments here ]);

One thing that you must be aware is that each destination must have an unique name so you should check first with the API if there is already a destination running under that name.

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001. Design by dcarter.