apache-flex 如何从BlazeDS推送数据而不接收来自Flex客户端消息?

djmepvbi  于 2022-11-01  发布在  Apache
关注(0)|答案(3)|浏览(156)

我正在使用BlazeDS在我的Flex应用程序项目中实现数据推送功能。来自官方教程Getting started with BlazeDS,它显示了来自API的生产者/消费者的消息传递示例。
但是我如何实现服务器端,而不是从Flex客户端调用,而是从服务器端内部调用呢?我有一些想法,但是我不知道该怎么做,因为我是Flex开发人员,而不是Java开发人员,所以我想您可以帮助我。
1.在Google中,有一个教程展示了我需要在Java-side中扩展ServiceAdapter类,它扩展了Invoke方法。我需要扩展其他类而不是这个来做我想做的事情吗?
1.如何配置message-config.xml以获得如上所述的结果?

2eafrhcq

2eafrhcq1#

这是我写的测试代码,有时候用来测试向客户端发送数据。这是一个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>

最后,确保在services-config.xml中定义了“my-streaming-amf”通道,如下所示:

<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应用程序所在的目录。
我希望这一切都有帮助!

  • 千克
qqrboqgw

qqrboqgw2#

您需要从服务器向客户端推送消息吗?在这种情况下,请查看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);
k4aesqcs

k4aesqcs3#

如果要在运行时将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);

只是提醒一下。这需要一些头部撞击才能开始。我希望这对某人有帮助。

相关问题