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

时间:2009-12-09 09:47:55

标签: java flex blazeds

我在Flex应用程序项目中使用BlazeDS进行数据推送功能。在官方教程Getting started with BlazeDS中,它显示了来自API的生产者/消费者的消息传递示例。

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

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

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

3 个答案:

答案 0 :(得分:8)

这是我编写的测试代码,有时用于测试向客户端发送数据。它是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]是您的网络应用所在的目录。

我希望这一切都有帮助!

-kg

答案 1 :(得分: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);

答案 2 :(得分: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); 

只是抬头。这需要一些头脑才能开始。我希望它有所帮助。