小编典典

如何在不从Flex客户端接收消息的情况下从BlazeDS推送数据?

java

我在Flex应用程序项目中将BlazeDS用于数据推送功能。在官方教程BlazeDS入门中,它显示了API中带有生产者/消费者的消息传递示例。

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

  1. 在Google中,有一个教程展示了我需要ServiceAdapter在Java端扩展类,即扩展Invoke方法。我需要扩展其他班级而不是这个做我想要的吗?

  2. 如何配置message-config.xml以获得如上所述的结果?


阅读 222

收藏
2020-10-12

共1个答案

小编典典

这是我编写并有时使用的测试代码,用于测试向客户端发送数据。这是ServiceAdapter实现的精简,简单的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>标签:

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

将此目标添加到同一messaging-config.xml文件:

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

最后,确保在中定义了“ 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中,这两个配置文件(messaging-config.xml和services-config.xml)位于以下目录中:

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

[nameOfYourApp]您的Web应用程序所在的目录在哪里。

希望对您有所帮助!

-公斤

2020-10-12