How to pop data from BlazeDS without receiving a message from the Flex client?

I am using BlazeDS for the push-push function in my Flex application project. From the official BlazeDS Getting Started tutorial , he shows an example of a manufacturer / consumer messaging from the API.

but how can I implement the server side, which does not need to be called from the Flex client, but instead on the server side. I have an idea, but I don’t know how to do it, because I am a Flex developer and not a Java developer, so I think you can help me.

  • Google shows that I need to extend the class ServiceAdapteron the Java side, which extends the method Invoke. Do I need to extend another class instead to do what I want?

  • How to configure message-config.xmlto get the result as described above?

+5
source share
3 answers

Here is the test code I wrote, and from time to time I check the sending of data to our client. This is a stripped-down, bare example of a Java image of a ServiceAdapter implementation. It removes a lot of unnecessary code from existing examples on the Internet. It compiles, works, and I often use it when 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 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 several properties in tomcat to make this work.

In messaging-config.xmlyou need to add an adapter and destination:

Add this line to the existing tag <adapters>:

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

Add this destination to the same file 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] - , webapp.

, !

-kg

+8

? 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);
+1

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

Just a head. It took several goals to go. Hope this helps someone.

0
source

All Articles