S2Flex2を試してみた。(2)
次はサービスを作ってみることにする。
どうやって作るかの説明がサイトにないので、exampleのAddサンプルをみて作ってみる。
とりあえず、こんな感じか。。
まず、Main.mxmlに S2Flex2Serviceとリモート呼び出しの結果を受け取るスクリプトを追加。
ボタンを押したら、MainService の getHoge()メソッドが呼ばれるようにする。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:s2="http://www.seasar.org/s2flex2/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.controls.Alert; import mx.utils.ObjectUtil; public function getHoge():void { service.getHoge(); } public function onResult(event:ResultEvent):void { Alert.show(ObjectUtil.toString(event)); var hoge:String = event.result as String; message.text = hoge; } public function onFault(event:FaultEvent):void { Alert.show(ObjectUtil.toString(event)); } ]]> </mx:Script> <s2:S2Flex2Service id="service" destination="mainService" result="onResult(event)" fault="onFault(event)" showBusyCursor="true" /> <mx:Text id="message" x="26" y="23" text="Hello World !" width="222" height="45" fontSize="30"/> <mx:Button x="26" y="76" label="ボタン" click="getHoge()"/> </mx:Application>
まだサーバ側を作ってないけど、この状態で一回実行してみよう。
実行すると、サービスが見つからないと起こられる。
そりゃそうだ。
ERROR 2007-03-15 16:03:23,046 [http-8080-Processor24] [EFLX0001]適用可能なServiceInvoker[mainService]が見つかりません。 org.seasar.flex2.rpc.remoting.service.exception.InvokerNotFoundRuntimeException: [EFLX0001]適用可能なServiceInvoker[mainService]が見つかりません。
ということで、サービスを作ってみる。
Main.mxmlの上で右クリックしたときのメニューに、"Create Service" というのがあるので、これで作る。
Create Service を実行すると、s2flex2.test.web.flex.MainService と、s2flex2.test.web.flex.MainServiceImpl を生成した。
で、こいつらに、getHoge メソッドを追加。
package s2flex2.test.web.flex; public interface MainService { String getHoge(); }
package s2flex2.test.web.flex.impl; import org.seasar.flex2.rpc.remoting.service.annotation.RemotingService; import s2flex2.test.web.flex.MainService; @RemotingService public class MainServiceImpl implements MainService { public String getHoge() { return "ほげ"; } }
で、実行してみる。
が、まだ InvokerNotFoundRuntimeException が出る。
exampleのAddサンプルを見ると、どうもconvention.dicon のrootPackageNameの設定が違うっぽい。
Addサンプルのconvention.diconだと、以下のようになっていて、
<initMethod name="addRootPackageName"> <arg>"examples.flex2.add"</arg> </initMethod>
サービスクラスは、examples.flex2.add.service.AddService と、examples.flex2.add.service.impl.AddServiceImple になっている。
つまり、Addサンプルでは、ルートパッケージの直下のserviceパッケージにサービスクラスが入っている。
なので、MessageService と MessageServiceImpl のパッケージを、それぞれ s2flex2.test.service と s2flex2.test.service.impl に変更する。
で、実行してみる。
DEBUG 2007-03-15 16:20:22,875 [http-8080-Processor24] HOT deployを開始します DEBUG 2007-03-15 16:20:22,968 [http-8080-Processor24] クラス(s2flex2.test.service.impl.MainServiceImpl[mainService])のコンポーネント定義を登録します DEBUG 2007-03-15 16:20:23,468 [http-8080-Processor24] mainService(クラス:s2flex2.test.service.impl.MainServiceImpl)をRemotingServiceとして登録しました。 DEBUG 2007-03-15 16:20:23,500 [http-8080-Processor24] BEGIN s2flex2.test.service.impl.MainServiceImpl#getHoge() DEBUG 2007-03-15 16:20:23,500 [http-8080-Processor24] END s2flex2.test.service.impl.MainServiceImpl#getHoge() : ほげ DEBUG 2007-03-15 16:20:23,515 [http-8080-Processor24] HOT deployを終了しました
ようやくうまくいった。
ここまでで、MainServiceを追加して、パッケージ移動したけど、Tomcatは最初に起動したままで再起動はしていない。
HotDeployすげぇ。
続けて、サービスクラスを、s2flex2.test.web.flex.serviceパッケージに、移動してみる。
サービスクラスを、s2flex2.test.web.flex.service.MessageService と s2flex2.test.web.flex.service.impl.MessageServiceImpl に変更。
実行してみる。
DEBUG 2007-03-15 16:42:37,328 [http-8080-Processor24] HOT deployを開始します DEBUG 2007-03-15 16:42:37,359 [http-8080-Processor24] クラス(s2flex2.test.web.flex.service.impl.MainServiceImpl[mainService])のコンポーネント定義を登録します DEBUG 2007-03-15 16:42:37,406 [http-8080-Processor24] mainService(クラス:s2flex2.test.web.flex.service.impl.MainServiceImpl)をRemotingServiceとして登録しました。 DEBUG 2007-03-15 16:42:37,406 [http-8080-Processor24] BEGIN s2flex2.test.web.flex.service.impl.MainServiceImpl#getHoge() DEBUG 2007-03-15 16:42:37,406 [http-8080-Processor24] END s2flex2.test.web.flex.service.impl.MainServiceImpl#getHoge() : ほげ DEBUG 2007-03-15 16:42:37,406 [http-8080-Processor24] HOT deployを終了しました
おぉ、うまく動いた。
どうやら、serviceという名前のパッケージに入れれば動くということらしい。
追記:
いや、Tomcatを再起動したら、s2flex2.test.web.flex.service だと動かない。
[EFLX0001]適用可能なServiceInvoker[mainService]が見つかりません。 org.seasar.flex2.rpc.remoting.service.exception.InvokerNotFoundRuntimeException: [EFLX0001]適用可能なServiceInvoker[mainService]が見つかりません。
そうなると、ルートパッケージ直下のserviceに入れるしかないのか。。。
サブアプリケーション単位で分けたい場合は、convention.diconに追加していく感じ?