Flash Remoting with ModelGlue2
I was wondering if anyone has figured out a better way to expose a cfc for Flash Remoting or as a webservice when using MG:2 than the way I have been doing it. I know it's possible to to just target a cfc directly but I want to have Coldspring configure of the cfc I'm targeting as have the ability to use Reactor within it.
Coldspring actully has a very cool ability to create a facade of a bean for use for flash remoting. The tricky part was getting this to work with Coldspring and Reactor as part of MG:2.
Here is the way I did it:
The Coldspring documentation describes how you can create a remoting facade for a bean configured in Coldsrping, so in my Coldspring.xml file I use something like this:
<bean id="myRemoting" class="mg2test.model.remoting.myRemoting" autowire="byName"></bean>
<bean id="myRemoting_Facade" class="coldspring.aop.framework.RemoteFactoryBean">
<property name="target">
<ref bean="myRemoting" />
</property>
<property name="serviceName">
<value>myRemotingService</value>
</property>
<property name="relativePath">
<value>mg2test/remotefacades/</value>
</property>
<property name="remoteMethodNames">
<value>get*</value>
</property>
</bean>
Then I can just (but not quite) target mg2test.remotefacades.myRemotingService with my remoting calls.
There are a couple of additional things I had to set up before this would work though.
1. You have to call getBean on the bean factory for myRemoting_Facade before Coldspring will create the mg2test.remotefacades.myRemotingService cfc for you. I do this by having a setmyRemoting_Facade method in on of my model-glue controllers, and because of autowiring it is created automatically. Also it makes the the facade available in my MG controller.
2. The cfc generated by Coldspring "myRemotingService" won't be able to find a reference to the coldspring bean factory unless it is an a scope that it can access. By default it looks for it at application["coldspring.beanfactory.root"]. I got arround this by adding the following to the end of my project's index.cfm:
<cfif StructKeyExists(_modelglue, "beanFactory")>
<cflock name="setUpForRemoting" type="exclusive" timeout="10" throwOnTimeout="true">
<cfset application["coldspring.beanfactory.root"] =
application[ModelGlue_APP_KEY].framework.GetBeanFactory()>
<cfset application.reactor =
application[ModelGlue_APP_KEY].framework.GETORMSERVICE()>
</cflock>
</cfif>
I also put a reference to reactor in the application scope for easy access from within myRemoting.cfc. Both application scope references should be reset if model-glue is forced to reload (the mg2test/remotefacades/myRemotingService.cfc should be recreated too for that matter).
3. With steps 1 and 2 you won't be able to make a request directly to mg2test.remotefacades.myRemotingService until at least one regular MG request has been made.
This set up seems to work pretty well and I haven't found anything on the interweb yet describing another way of doing this with MG:2.
I wonder though, is there anyway this could be improved or simplified? Or is there a completely different way that this could be done that is far simpler?
http://blog.onepixeloff.com/trackback.cfm?B0E2C72B-0562-6ABB-ED3198E3AF4F91A2