如何从Flex客户端接收消息从BlazeDS推送数据? [英] how to push data from BlazeDS without receive message from Flex client?

查看:113
本文介绍了如何从Flex客户端接收消息从BlazeDS推送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Flex应用程序项目中使用BlazeDS来实现数据推送功能。从官方教程 BlazeDS入门,它显示了消息示例与API生产者/消费者。

但是我怎样才能实现服务器端,而不需要从Flex客户端调用,而是从服务器端而不是。我有一些想法,但我不知道该怎么做,因为我是一名Flex开发人员,而不是Java开发人员,所以我认为您可以帮助我。


  1. 在Google中,有一个关于我需要在Java端扩展 ServiceAdapter 类的教程节目,它扩展了 Invoke 方法。如何配置 message-config.xml

  2. 如何配置 / code>得到如上所述的结果? 这是我编写和使用的测试代码,有时会测试发送数据给我们的客户端。这是一个精简的,一个ServiceAdapter实现的骨架Java示例。它从网络上的现有示例中删除了大量不必要的代码。它编译,工作,我经常在测试中使用它。

      package your.package.structure.adapter; 

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

    / **
    *测试服务适配器。当你想要发送一个对象,而不是
    *其他的时候,测试是非常好的。这个类必须留在主代码库(而不是测试),因为当它使用
    *时,它需要部署到Tomcat。
    * @author Kevin G
    *
    * /

    public class TestServiceAdapter extends ServiceAdapter {

    private volatile boolean running;
    $ b $ private消息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;

    $ b private void sendMessageToClients(Message msg){
    ((MessageService)getDestination()。getService())。pushMessageToClients(msg,false);

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

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

    messageSender.start();
    $ b $ **
    * see flex.messaging.services.ServiceAdapter#stop()
    * /
    @Override
    public void stop( ){
    super.stop();
    running = false;
    }
    / **
    *当生产者发送消息到目的地时,调用这个方法。目前,
    *我们不在乎什么时候发生。
    * /
    @Override
    public Object invoke(Message message){
    if(message.getBody()。equals(stop)){
    running =假;
    }
    返回null;

    private void secondsToSleep(int seconds){
    try {
    Thread.sleep(seconds * 1000);
    } catch(InterruptedException e){
    System.out.println(TestServiceAdapter while during sending messages);
    e.printStackTrace();





    $你需要设置一些


    messaging-config.xml 中,您需要添加一个适配器和目标地址:

    将此行添加到现有的< adapters> 标记:

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

    将此目标添加到同一个 messaging-config.xml

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

    最后,确保在code中定义了my-streaming-amf > services-config.xml ,如下所示:

     < channel-definition id =my -streaming-amfclass =mx.messaging.channels.StreamingAMFChannel> 
    < endpoint url =http:// {server.name}:{server.port} / {context.root} / messagebroker / streamingamfclass =flex.messaging.endpoints.StreamingAMFEndpoint/>
    <属性>
    <! - 您不需要设置所有这些属性,这只是我们设置的内容,仅用于说明,仅限于>
    < 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 =Safarikickstart-bytes =2048max-streaming-connections-per-session =10/>
    < user-agent match-on =MSIEkickstart-bytes =2048max-streaming-connections-per-session =15/>
    < user-agent match-on =Firefoxkickstart-bytes =2048max-streaming-connections-per-session =10/>
    < / user-agent-settings>
    < / properties>
    < / channel-definition>注意,在blazeDS中,这两个配置文件(messaging-config.xml和services-config.xml )位于以下目录中:

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

    其中 [nameOfYourApp] 是您的webapp住在。



    我希望所有的帮助!

    -kg


    I am using BlazeDS for data-push feature in my Flex application project. From the official tutorial, Getting started with BlazeDS, it shows messaging example with producer/consumer from API.

    but how can I implement server side which doesn't need to be invoke from Flex client, but from within server-side instead. I got some idea but I don't know how to do because I'm a Flex developer, not Java developer, so I think you can help me.

    1. In Google, there's a tutorial show about I need to extend ServiceAdapter class in Java-side, which extends Invoke method. Do I need to extend other class instead of this to do what I want?

    2. How to configure the message-config.xml to get the result like I describe above?

    解决方案

    Here is test code I wrote and use, at times, to test sending data to our client. It's a stripped down, bare bones Java example of a ServiceAdapter implementation. It removes a lot of unnecessary code from the existing examples on the web. It Compiles, works and I use it often in testing.

    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();
            }
        }        
    }
    

    You need to set a few properties in tomcat to get this to work.

    In messaging-config.xml, you need to add an adapter and destination:

    Add this line to the existing <adapters> tag:

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

    Add this destination to that same messaging-config.xml file:

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

    Finally, make sure the "my-streaming-amf" channel is defined in services-config.xml, as in:

    <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>
    

    Note that in blazeDS, these two config files (messaging-config.xml and services-config.xml) are located in the following directory:

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

    where [nameOfYourApp] is the directory your webapp lives in.

    I hope all that helps!

    -kg

    这篇关于如何从Flex客户端接收消息从BlazeDS推送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆