2009-12-09 14 views
5

私はFlexアプリケーションプロジェクトでデータプッシュ機能としてBlazeDSを使用しています。公式チュートリアルGetting started with BlazeDSから、APIのproducer/consumerとのメッセージングの例を示します。Flexクライアントからのメッセージを受信せずにBlazeDSからデータをプッシュする方法はありますか?

しかし、Flexクライアントから呼び出す必要はなく、代わりにサーバー側から実装することができます。私はいくつか考えましたが、私はFlex開発者でJava開発者ではないので、どうやってやるのか分からないので、私を助けてくれると思います。グーグルで

  1. 、私はInvoke方法を拡張するJava側でServiceAdapterクラスを拡張する必要がありますについてのチュートリアルショーがあります。私は自分が望むことをするためにこれの代わりに他のクラスを拡張する必要がありますか?

  2. 上記のようにmessage-config.xmlを設定して結果を得るにはどうすればよいですか?

+1

アドバイスの一部、これを見て文法を訂正するように誰かを手に入れてください。それは非常に難しいです。 – cwallenpoole

答えて

8

私が書いたテストコードは、私たちのクライアントにデータを送信することをテストするために書かれたものです。これは、ServiceAdapter実装の剥奪された、裸のJava Javaの例です。 Web上の既存のサンプルから不要なコードをたくさん削除します。それはコンパイルされ、動作し、テストで頻繁に使用します。

package your.package.structure.adapter; 

import your.package.structure.device.DevicePort; 
import flex.messaging.messages.AsyncMessage; 
import flex.messaging.messages.Message; 
import flex.messaging.services.MessageService; 
import flex.messaging.services.ServiceAdapter; 
import flex.messaging.util.UUIDUtils; 

    /** 
    * Test service adapter. Great for testing when you want to JUST SEND AN OBJECT and nothing 
    * else. This class has to stay in the main codebase (instead of test) because, when it's used 
    * it needs to be deployed to Tomcat. 
    * @author Kevin G 
    * 
    */ 

public class TestServiceAdapter extends ServiceAdapter { 

    private volatile boolean running; 

    private Message createTestMessage() { 
     DevicePort objectToSend = new DevicePort("RouterDevice"); 

     final AsyncMessage msg = new AsyncMessage(); 
     msg.setDestination(getClass().getSimpleName() + "Destination"); 
     msg.setClientId(UUIDUtils.createUUID()); 
     msg.setMessageId(UUIDUtils.createUUID()); 
     msg.setBody(objectToSend); 

     return msg; 
    } 

    private void sendMessageToClients(Message msg) { 
     ((MessageService) getDestination().getService()).pushMessageToClients(msg, false); 
    } 

    /** 
    * @see flex.messaging.services.ServiceAdapter#start() 
    */ 
    @Override 
    public void start(){  
     super.start(); 

     Thread messageSender = new Thread(){ 
      public void run(){ 
       running = true; 
       while(running){ 
        sendMessageToClients(createTestMessage()); 
        secondsToSleep(3); 
       } 
      } 
     }; 

     messageSender.start();   
    } 
    /** 
    * @see flex.messaging.services.ServiceAdapter#stop() 
    */ 
    @Override 
    public void stop(){ 
     super.stop(); 
     running = false; 
    } 
    /** 
    * This method is called when a producer sends a message to the destination. Currently, 
    * we don't care when that happens. 
    */ 
    @Override 
    public Object invoke(Message message) { 
     if (message.getBody().equals("stop")) { 
      running = false; 
     } 
     return null; 
    } 
    private void secondsToSleep(int seconds) { 
     try{ 
      Thread.sleep(seconds * 1000); 
     }catch(InterruptedException e){ 
      System.out.println("TestServiceAdapter Interrupted while sending messages"); 
      e.printStackTrace(); 
     } 
    }   
} 

これを機能させるには、tomcatにいくつかのプロパティを設定する必要があります。

messaging-config.xml

、あなたはアダプタと宛先を追加する必要があります。

既存<adapters>タグに次の行を追加します。

<destination id="TestServiceAdapterDestination"> 
     <channels> 
      <channel ref="my-streaming-amf"/> 
     </channels> 
     <adapter ref="TestServiceAdapter"/> 
    </destination> 

<adapter-definition id="TestServiceAdapter" class="your.package.structure.adapter.TestServiceAdapter"/> 

が同じmessaging-config.xmlファイルにこの宛先を追加

最後に、 "my-streaming-amf"チャンネルがservices-config.xmlで定義されていることを確認してください。

<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel"> 
     <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/> 
     <properties> 
      <!-- you don't need to set all these properties, this is just what we set, included for illustration, only --> 
      <idle-timeout-minutes>0</idle-timeout-minutes> 
      <max-streaming-clients>10</max-streaming-clients> 
       <server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis> 
      <user-agent-settings> 
       <user-agent match-on="Safari" kickstart-bytes="2048" max-streaming-connections-per-session="10"/> 
       <user-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="15"/> 
       <user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="10"/> 
      </user-agent-settings> 
     </properties> 
    </channel-definition> 

BlazeDSの中では、これら2つの設定ファイル(メッセージング-config.xmlのとサービス-config.xmlには)次のディレクトリに格納されていることに注意してください:​​は、ディレクトリ、Webアプリケーションである

/blazeds/tomcat/webapps/[nameOfYourApp]/WEB-INF/flex/ 

私はすべてが助けてくれることを願っています!

-kg

+0

これは素晴らしいです。あなたは同じクラスで送るメッセージを受け取ることも可能でしょうか? – MikeW

1

メッセージをサーバーからクライアントにプッシュする必要がありますか?この場合、BlazeDSサンプルを見てください。 traderdesktopと呼ばれるフォルダにサンプルがあります。

MessageBroker msgBroker = MessageBroker.getMessageBroker(null); 

AsyncMessage msg = new AsyncMessage(); 

msg.setDestination(yourdestination); 

msg.setClientId(clientID); 

msg.setMessageId(UUIDUtils.createUUID()); 

msg.setTimestamp(System.currentTimeMillis()); 

msg.setBody("dummy"); 

msgBroker.routeMessageToService(msg, null); 
+0

Comelさん、ありがとうございました。指定する必要があるclientIDは何ですか?どこから入手できますか? – Teerasej

+0

同じサンプルから:String clientID = UUIDUtils.createUUID(); –

0

実行時にストリームにURLを設定したい場合は、次の操作を行います。

//assumes _consumer is an instance variable mx.messaging.Consumer 
var channelSet:ChannelSet = new ChannelSet(); 
//change {server.name}:{server.port} to the end point you wanna hit 
var ep:String = "http://{server.name}:{server.port}/messagebroker/streamingamf"; 
var channel:StreamingAMFChannel = new StreamingAMFChannel("my-streaming-amf", ep); 
channelSet.addChannel(channel); 

_consumer = new Consumer(); 
_consumer.channelSet = channelSet; 
_consumer.destination = "TestServiceAdapterDestination"; 
_consumer.subscribe(); 
_consumer.addEventListener(MessageEvent.MESSAGE, onMsg); 
_consumer.addEventListener(MessageFaultEvent.FAULT, faultHandler); 

ジャストヘッドアップ。これはいくつかの頭を打つ打撃を取った。私はそれが誰かを助けることを望む。

関連する問題