Flex&BlazeDSでmx.rpc.remoting.RemoteObjectを利用してJavaオブジェクトのリモート呼び出しを実施する際、接続先のサーバ側のエンドポイントURLをどーやって設定するのか分からずハマッた。
外部から設定しなくてもActionScript内で
public function authentication(id:String, password:String):void { remote = new RemoteObject("authenticationService"); var cs:ChannelSet = new ChannelSet(); var ac:AMFChannel = new AMFChannel("my-amf", "http://localhost:8400/messagebroker/amf"); cs.addChannel(ac); remote.channelSet = cs; remote.addEventListener(ResultEvent.RESULT, resultHandler); remote.addEventListener(FaultEvent.FAULT, faultHandler); remote.authenticate(id, password); }
な感じで、AMFChannelのオブジェクトを生成しRemoteObjectのchannelSetにセットすればエンドポイントURLの設定は可能になる。RemoteObjectを使用する度に↑なコードを書くのはちょっと…と思ってたら、SWFのコンパイル時に設定することが可能なのね。
mxmlcのコンパイラオプションにservicesというオプションがあるので、このオプションでservices-config.xmlのパスを指定すれば良い。エンドポイントのURLはchannel毎にservices-config.xmlに定義してやれば良い。こんな感じ↓
<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <default-channels> <channel ref="my-amf"/> </default-channels> </services> <channels> <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"> <endpoint url="http://localhost:8400/messagebroker/amf" class="org.seasar.blazeds.endpoints.S2AMFEndpoint"/> <properties> <polling-enabled>false</polling-enabled> </properties> </channel-definition> </channels> </services-config>
services-config.xmlは、BlazeDS側の設定ファイルでサーバ側でのみ利用するのかと思ってたけど、クライアント側のSWF生成の際にも利用するのね…。まぁ、サーバ側で提供されるサービスの情報を定義してるんだから当然と言えば当然なのか。