Tag Archive for 'blazeds'

25AugMessaging中にRemoteObjectの処理をする

Messaging中にRemoteObjectにデータを保存したり、メソッド実行したりする際にRemotingObjectにアクセスする方法を忘れないようにメモ。

  • パッケージ名:packagename
  • Messagingをするクラス名:MessagingAdapter(ServiceAdapterのサブクラス)
  • Messagingのadapter-definition id:hoge-messaging-adapter
  • RemoteObjectのクラス名:ClassName
  • RemoteObjectのdestination id:hoge-remote-object

FlexContext.getServletContext()で取得したServletContextから、getAttribute('destination id')でRemoteObjectの参照を取得する。
これでメッセージングする度にRemoteObjectのsetDataにmessageのbodyが渡されるようになる。
あとは煮るなり焼くなり・・・

  1. package packagename;
  2.  
  3. import packagename.ClassName;
  4.  
  5. import flex.messaging.config.ConfigMap;
  6. import flex.messaging.messages.Message;
  7. import flex.messaging.FlexContext;
  8. import flex.messaging.MessageBroker;
  9. import flex.messaging.services.MessageService;
  10. import flex.messaging.services.ServiceAdapter;
  11.  
  12. import java.util.HashMap;
  13.  
  14. import javax.servlet.ServletContext;
  15.  
  16. public class MessagingAdapter extends ServiceAdapter {
  17.     private MessageBroker messageBroker;
  18.     private MessageService messageService;
  19.     private ServletContext servletContext;
  20.     private ClassName remoteObject;
  21.    
  22.     @Override
  23.     public void initialize(String id, ConfigMap properties) {
  24.        
  25.     }
  26.    
  27.     @Override
  28.     public void start() {
  29.         // MessagingService取得
  30.         messageBroker = MessageBroker.getMessageBroker(null);
  31.         messageService = (MessageService) messageBroker.getService("hoge-messaging-service");
  32.        
  33.         // RemoteObject取得
  34.         servletContext = FlexContext.getServletContext();
  35.         remoteObject = (IconManager) servletContext.getAttribute("hoge-remote-object");
  36.     }
  37.    
  38.     @Override
  39.     public void stop() {
  40.        
  41.     }
  42.    
  43.     @Override
  44.     public Object invoke(Message message) {
  45.         // Messaging配信
  46.         messageService.pushMessageToClients(message, true);
  47.         messageService.sendPushMessageFromPeer(message, true);
  48.        
  49.         // RemoteObjectへmessageのbodyを渡す
  50.         HashMap hmMessage = (HashMap) message.getBody();
  51.         remoteObject.setData(hmMessage);
  52.        
  53.         return null;
  54.     }
  55.    
  56. }

ちなみにこの時の*-config.xmlたちはこんな感じ。

  • XML
  • messaging-config.xml
  • Source
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <service id="hoge-messaging-service"
  4.     class="flex.messaging.services.MessageService">
  5.  
  6.     <adapters>
  7.         <adapter-definition id="actionscript"
  8.             class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"
  9.             default="true" />
  10.         <adapter-definition id="hoge-messaging-service"
  11.             class="packagename.MessagingAdapter" />
  12.     </adapters>
  13.  
  14.     <!-- ...中略... -->
  15.  
  16. </service>
  • XML
  • remoting-config.xml
  • Source
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <service id="hoge-remoting-service" class="flex.messaging.services.RemotingService">
  4.    
  5.     <!-- ...中略... -->
  6.    
  7.     <destination id="hoge-remote-object">
  8.         <properties>
  9.             <source>packagename.ClassName</source>
  10.             <scope>application</scope>
  11.         </properties>
  12.     </destination>
  13.    
  14. </service>

21Aug識別子なのに識別できなかった件

BlazeDSのMessaging APIsのServiceAdapterとAbstractBootstrapServiceのサブクラスを作っていた時のお話。

  • Java
  • MessagingAdapter.java
  • Source
  1. public class MessagingAdapter extends ServiceAdapter {
  2.     // ...中略...
  3.     @Override
  4.     public Object invoke(Message message) {
  5.         System.out.println(message.getClientId())// Result 19FF0FC6-1818-5450-D6BE-33FF6297BFD0
  6.     }
  7. }
  • Java
  • BootstrapService.java
  • Source
  1. public class BootstrapService extends AbstractBootstrapService {
  2.     // ...中略...
  3.     class ClientWatcher implements MessageClientListener {
  4.         public void messageClientCreated(MessageClient msgClient) {
  5.             System.out.println(msgClient.getClientId());    // Result 19FF11B5-B30A-3172-EDD2-0AB30F8A73EA
  6.         }
  7.     }
  8. }

この二つで取得できるクライアントIDって同一のクライアントなはずなのに別モノ。
この二つのクラスからRemoteObjectにクライアントID渡してごにょごにょしようとしてたらできなくて、調べていくとこういうことだった。

同じものが取得できると思ったのに。同じものが取得できると思ったのに。

17JulBlazeDS - Messaging(Client)

BlazeDSの主要な機能の一つであるMessegingのクライアントサイドの処理について。

  1. srcを右クリックして、「新規 > MXML アプリケーション」をクリック。「ファイル名」をmessaging.mxmlと入力して「終了」。

    ss_032 ss_033
  2. クライアントの処理を書く。

    ss_034
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="init()" viewSourceURL="srcview/index.html">
    3.    
    4.     <mx:Script>
    5.         <![CDATA[
    6.             import mx.messaging.events.MessageFaultEvent;
    7.             import mx.messaging.messages.AsyncMessage;
    8.             import mx.messaging.messages.IMessage;
    9.             import mx.messaging.ChannelSet;
    10.             import mx.messaging.channels.AMFChannel;
    11.             import mx.messaging.channels.StreamingAMFChannel;
    12.             import mx.messaging.events.MessageEvent;
    13.             import mx.messaging.events.ChannelEvent;
    14.             import mx.messaging.Consumer;
    15.             import mx.messaging.Producer;
    16.            
    17.             private var producer:Producer = new Producer();
    18.             private var consumer:Consumer = new Consumer();
    19.            
    20.             private function init():void {
    21.                 initConnectEvent();
    22.                
    23.                 connectToServer();
    24.             }
    25.            
    26.             private function initConnectEvent():void {
    27.                 consumer.addEventListener(ChannelEvent.CONNECT, hdlConsumerConnect);
    28.                 consumer.addEventListener(ChannelEvent.DISCONNECT, hdlConsumerDisconnect);
    29.                 consumer.addEventListener(MessageEvent.MESSAGE, hdlConsumerMessage);
    30.             }
    31.            
    32.             private function hdlConsumerConnect(ce:ChannelEvent):void {
    33.                 log("Connect to Server.");
    34.                
    35.                 setSend(true);
    36.             }
    37.            
    38.             private function hdlConsumerDisconnect(ce:ChannelEvent):void {
    39.                 log("Disconnect from Server.");
    40.                
    41.                 setSend(false);
    42.             }
    43.            
    44.             private function hdlConsumerMessage(me:MessageEvent):void {
    45.                 log(me.message.body.chat);
    46.             }
    47.            
    48.             private function setSend(bln:Boolean):void {
    49.                 if (bln) {
    50.                     btnSend.addEventListener(MouseEvent.CLICK, hdlSendClick);
    51.                 } else {
    52.                     btnSend.removeEventListener(MouseEvent.CLICK, hdlSendClick);
    53.                 }
    54.                 btnSend.enabled = bln;
    55.                 tiChat.enabled = bln;
    56.             }
    57.            
    58.             private function hdlSendClick(e:*):void {
    59.                 var msg:IMessage = new AsyncMessage();
    60.                 msg.body = {chat:tiChat.text};
    61.                
    62.                 producer.send(msg);
    63.                 tiChat.text = "";
    64.             }
    65.            
    66.             private function connectToServer():void {
    67.                 setChannel("localhost");
    68.                 setDistination();
    69.                
    70.                 consumer.subscribe();
    71.             }
    72.            
    73.             private function setChannel(hostname:String):void {
    74.                 var channelSet:ChannelSet = new ChannelSet();
    75.                 var myStreamingAMF:AMFChannel = new StreamingAMFChannel("my-streaming-amf", "http://" + hostname + ":8400/tutorial/messagebroker/streamingamf");
    76.                 var myPollingAMF:AMFChannel = new AMFChannel("my-polling-amf", "http://" + hostname + ":8400/tutorial/messagebroker/amfpolling");
    77.                 myPollingAMF.pollingEnabled = true;
    78.                 myPollingAMF.pollingInterval = 1;
    79.                
    80.                 channelSet.addChannel(myStreamingAMF);
    81.                 channelSet.addChannel(myPollingAMF);
    82.                
    83.                 producer.channelSet = channelSet;
    84.                 consumer.channelSet = channelSet;
    85.             }
    86.            
    87.             private function setDistination():void {
    88.                 producer.destination = "messagingTutorial";
    89.                 consumer.destination = "messagingTutorial";
    90.             }
    91.            
    92.             private function log(str:String):void {
    93.                 taLog.text = str + "\n" + taLog.text;
    94.             }
    95.            
    96.         ]]>
    97.     </mx:Script>
    98.    
    99.     <mx:ApplicationControlBar width="100%" height="100%">
    100.         <mx:TextArea width="100%" height="100%" id="taLog" editable="false"/>
    101.     </mx:ApplicationControlBar>
    102.     <mx:ApplicationControlBar width="100%">
    103.         <mx:TextInput id="tiChat" width="100%" enabled="false" enter="hdlSendClick(event)"/>
    104.         <mx:Button label="Send" id="btnSend" enabled="false"/>
    105.     </mx:ApplicationControlBar>
    106.    
    107. </mx:Application>
  3. 「ファイル > エクスポート」をクリックして、「Flex Builder > リリースビルド」を選択して「次へ」。
    「書き出し先フォルダ」をtutorialにして「終了」。

    ss_035 ss_036 ss_037
  4. ブラウザを二窓開いてhttp://localhost:8400/tutorial/messaging.htmlへアクセス。
    一方の窓でテキストを入力し、Sendを押すと、両方のクライアントにストリーミングでテキストが配信される。

    ss_038 ss_039

17JulBlazeDS - Messaging(Server)

BlazeDSの主要な機能の一つであるMessegingのサーバーサイドの処理について。

  1. services-config.xmlにstreamingの設定を書く。

    ss_030
    • XML
    • services-config.xml
    • Source
    1. <?xml version="1.0" encoding="UTF-8"?>
    2.  
    3. <service id="message-service" class="flex.messaging.services.MessageService">
    4.    
    5.     <adapters>
    6.         <adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
    7.         <!-- <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/> -->
    8.     </adapters>
    9.    
    10.     <default-channels>
    11.         <channel ref="my-streaming-amf"/>
    12.     </default-channels>
    13.    
    14.     <destination id="messagingTutorial"/>
    15.    
    16. </service>
  2. messaging-config.xmlにデフォルトチャンネルの設定、サービスのIDを書く。

    ss_031
    • XML
    • messaging-config.xml
    • Source
    1. <channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">
    2.     <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/>
    3.     <properties>
    4.         <idle-timeout-minutes>0</idle-timeout-minutes>
    5.         <max-streaming-clients>100</max-streaming-clients>
    6.         <server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis>
    7.         <user-agent-settings>
    8.             <user-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="10"/>
    9.             <user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="10"/>
    10.             <user-agent match-on="AppleWebKit" kickstart-bytes="2048" max-streaming-connections-per-session="10"/>
    11.         </user-agent-settings>
    12.     </properties>
    13. </channel-definition>
  3. サーバーを再起動。

08JulBlazeDS - Remotoing(Client)

BlazeDSの主要な機能の一つであるRemotingのクライアントサイドの処理について。

  1. 「ファイル > 新規 > MXML アプリケーション」をクリックして、「ファイル名」にRemoting.mxmlと入力して、「終了」。

    ss_023 ss_024
  2. remoting.mxmlにクライアントの処理を書く。

    • ActionScript
    • remoting.mxml
    • Source
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="init()" viewSourceURL="srcview/index.html">
    3.    
    4.     <mx:Script>
    5.         <![CDATA[
    6.             import mx.rpc.remoting.RemoteObject;
    7.             import mx.rpc.events.ResultEvent;
    8.             import mx.rpc.events.FaultEvent;
    9.            
    10.             private var remoteObject:RemoteObject = new RemoteObject();
    11.            
    12.             private function init():void {
    13.                 initRemoteObject();
    14.                 initButtons();
    15.             }
    16.            
    17.             private function initRemoteObject():void {
    18.                 remoteObject.destination = "remotingTutorial";
    19.                 remoteObject.addEventListener("result", hdlROResult);
    20.                 remoteObject.addEventListener("fault", hdlROFault);
    21.             }
    22.            
    23.             private function hdlROResult(re:ResultEvent):void {
    24.                 var result:int = re.message.body.result;
    25.                 setResult(result.toString());
    26.             }
    27.            
    28.             private function hdlROFault(fe:FaultEvent):void {
    29.                 setResult("Fail to get responce.");
    30.             }
    31.            
    32.             private function setResult(str:String):void {
    33.                 tiResult.text = str;
    34.             }
    35.            
    36.             private function initButtons():void {
    37.                 btnCalc.addEventListener(MouseEvent.CLICK, hdlGetCalcClick);
    38.                 btnClear.addEventListener(MouseEvent.CLICK, hdlClearClick);
    39.                
    40.                 btnCalc.enabled = true;
    41.                 btnClear.enabled = true;
    42.             }
    43.            
    44.             private function hdlGetCalcClick(me:MouseEvent):void {
    45.                 var obj:Object = new Object();
    46.                
    47.                 obj.param0 = int(tiParam_0.text);
    48.                 obj.param1 = int(tiParam_1.text);
    49.                
    50.                 remoteObject.getCalcResult(obj);
    51.             }
    52.            
    53.             private function hdlClearClick(me:MouseEvent):void {
    54.                 setResult("");
    55.             }
    56.            
    57.         ]]>
    58.     </mx:Script>
    59.    
    60.     <mx:ApplicationControlBar>
    61.         <mx:TextInput id="tiParam_0" text="10"/>
    62.         <mx:Label text="+" textAlign="center"/>
    63.         <mx:TextInput id="tiParam_1" text="20"/>
    64.         <mx:Label text="=" textAlign="center"/>
    65.         <mx:TextInput id="tiResult"/>
    66.     </mx:ApplicationControlBar>
    67.     <mx:ApplicationControlBar>
    68.         <mx:Button label="getCalcResult" id="btnCalc" enabled="false"/>
    69.         <mx:Button label="clear" id="btnClear" enabled="false"/>
    70.     </mx:ApplicationControlBar>
    71.    
    72. </mx:Application>
    ss_025
  3. 「ファイル > エクスポート」より、「Flex Builder > リリースビルド」を選択して「次へ」。「書き出し先フォルダ」をtutorialにして、「終了」。

    ss_026 ss_027 ss_028
  4. これで、クライアントの開発は完了。ブラウザでhttp://localhost:8400/tutorial/remoting.htmlにアクセスして確認。

    ss_029



Return to page top