Build the Flex client
I used Cairngorm in my Flex client. I've used it before so I'm reasonably comfortable with it. I'm not doing anything out of the ordinary, but I did want to show some bits which are hooked to the remoting piece.
Value object mapping
Notice that I use the RemoteClass metadata to bind this ActionScript object to its Java counterpart. Also, make sure your ActionScript constructors are no-argument constructors, especially for classes that will be returned through the remoting calls.
package myblazedsapp.valueobjects
{
import mx.collections.ArrayCollection;
[RemoteClass(alias="myblazedsapp.valueobjects.SearchResponseVO")]
public class SearchResponseVO
{
public var searchString:String;
public var searchResults:ArrayCollection;
public function SearchResponseVO()
{
}
}
}
Services.mxml
Cairngorm mandates that services be maintained in a central repository, typically a Services.mxml file. Here's mine. Nothing special here.
<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator
xmlns:cairngorm="com.adobe.cairngorm.business.*"
xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:RemoteObject id="loginService"
showBusyCursor="true"
destination="loginService"
source="myblazedsapp.services.LoginService">
</mx:RemoteObject>
<mx:RemoteObject id="searchService"
showBusyCursor="true"
destination="searchService"
source="myblazedsapp.services.SearchService">
</mx:RemoteObject>
</cairngorm:ServiceLocator>
Cairngorm delegates
Finally, the Cairngorm delegates use the remoting services defined in the Services.mxml file. Here's one from my Flex app...
package myblazedsapp.business
{
import com.adobe.cairngorm.business.ServiceLocator;
import mx.rpc.IResponder;
import myblazedsapp.valueobjects.SearchRequestVO;
public class SearchDelegate
{
private var responder:IResponder;
private var service:Object;
public function SearchDelegate(responder:IResponder) {
this.responder = responder;
this.service = ServiceLocator.getInstance().getRemoteObject("searchService");
}
public function search(searchRequest:SearchRequestVO) : void {
var call:Object = service.search(searchRequest);
call.addResponder(responder);
}
}
}
and the associated Cairngorm command that collaborates with this delegate:
package myblazedsapp.commands
{
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import mx.controls.Alert;
import mx.rpc.IResponder;
import mx.rpc.events.ResultEvent;
import myblazedsapp.business.SearchDelegate;
import myblazedsapp.events.SearchEvent;
import myblazedsapp.models.ModelLocator;
import myblazedsapp.valueobjects.SearchRequestVO;
import myblazedsapp.valueobjects.SearchResponseVO;
public class SearchCommand implements ICommand, IResponder
{
public function SearchCommand()
{
}
public function execute(event:CairngormEvent):void
{
var searchEvent:SearchEvent = event as SearchEvent;
var delegate:SearchDelegate = new SearchDelegate(this);
delegate.search(new SearchRequestVO(searchEvent.searchString));
}
public function result(event:Object):void {
var result:SearchResponseVO = (event as ResultEvent).result as SearchResponseVO;
ModelLocator.getInstance().searchResults = result.searchResults;
ModelLocator.getInstance().currentMainViewViewStackIndex = 1;
}
public function fault(info:Object):void {
trace("Service error");
Alert.show("Service error.", "FAULT");
}
}
}
Configuring your Flex client for BlazeDS services
Add the -services and -context-root flags onto the Additional compiler arguments. For this example, I'm using the Flex Builder 3 plugin with Eclipse 3.3 Europa for Java EE, so I have a Flex project and a Dynamic Web project.
-services ..\..\WebServer\WebContent\WEB-INF\flex\services-config.xml -context-root "WebServer"You may have to play with the services relative pathname a bit. For the one above, both the Flex project and the Java server project are contained in a common workspace.
Click on the image to view in original size