使用Blazeds中的StreamingAMFChannel通道进行通讯

本文介绍了一种使用Blazeds中的StreamingAMFChannel实现客户端与服务器实时通讯的方法。通过创建Servlet程序并配置services-config.xml及messaging-config.xml文件,实现了服务器端消息推送至Flex客户端的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在网上看到一遍通过Blazeds中的StreamingAMFChannel通过进行通讯的方法。非常喜欢,特记录下来以备以后使用。

  这个方法是首先建立一个Servlet程序,如下例所示:
 
import java.io.IOException;  
import java.math.BigDecimal;  
import java.util.Date;  
 
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;   
 
import flex.messaging.MessageBroker;  
import flex.messaging.messages.AsyncMessage;  
import flex.messaging.util.UUIDUtils;  
 
public class TickCacheServlet extends HttpServlet {  
 
    private static FeedThread thread;  
 
    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
 
        String cmd = req.getParameter("cmd");  
        if (cmd.equals("start")) {  
            start();  
        }  
        if (cmd.equals("stop")) {  
            stop();  
        }  
    }  
 
    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        // TODO Auto-generated method stub  
        super.doGet(req, resp);  
    }  
 
    @Override 
    public void destroy() {  
        // TODO Auto-generated method stub  
        super.destroy();  
    }  
 
    @Override 
    public void init() throws ServletException {  
        // TODO Auto-generated method stub  
        super.init();  
    }  
 
    public void start() {  
        if (thread == null) {  
            thread = new FeedThread();  
            thread.start();  
        }  
        System.out.println("start!!");  
    }  
 
    public void stop() {  
        thread.running = false;  
        thread = null;  
    }  
 
    public static class FeedThread extends Thread {  
 
        public boolean running = true;  
 
        public void run() {  
            MessageBroker msgBroker = MessageBroker.getMessageBroker(null);  
            String clientID = UUIDUtils.createUUID();  
            int i = 0;  
            while (running) {   
                UserDto user = new UserDto ();   
                user.setId(1);
                user.setName("test");
                user.setDept("财务部")

                user.setCreateDate(new Date());   
 
                System.out.println(i);  
 
                AsyncMessage msg = new AsyncMessage();  
                msg.setDestination("user_Tun");  
                msg.setHeader("DSSubtopic", "user");  
                msg.setClientId(clientID);  
                msg.setMessageId(UUIDUtils.createUUID());  
                msg.setTimestamp(System.currentTimeMillis());  
                msg.setBody(user);  
                msgBroker.routeMessageToService(msg, null);  
                i++;  
                try {  
                    Thread.sleep(20);  
                } catch (InterruptedException e) {  
                }  
 
            }  
        }  
    }  
 

 

2。设置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>
                <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="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="1"/>
                    <user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="1"/>
                </user-agent-settings>
            </properties>
        </channel-definition>

 

messaging-config.xml文件中,加入如下:
    <destination id="user_Tun">
        <properties>
            <server>
                <allow-subtopics>true</allow-subtopics>
                <subtopic-separator>.</subtopic-separator>
            </server>
        </properties>
        <channels>
            <channel ref="my-polling-amf" />
            <channel ref="my-streaming-amf" />
        </channels>
    </destination>

 

4.Flex执行程序

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html" height="378" width="426">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.messaging.Consumer;
            import mx.messaging.Channel;
            import mx.messaging.ChannelSet;
            import mx.messaging.events.MessageEvent;

            [Bindable]
            public var user:UserVO;
           
            public function sendmsg():void
            {
                Alert.show("click start");
                var consumer:Consumer = new Consumer();
                consumer.destination = "user_Tun";
                consumer.subtopic = "user";
                consumer.channelSet = new ChannelSet(["my-streaming-amf"]);
                consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);
                consumer.subscribe();
              }
           
            private function messageHandler(event:MessageEvent):void
            {
                var t:UserVO = event.message.body as UserVO;
                txtName.text = t.name;
            }
        ]]>
    </mx:Script>
    <mx:Panel x="32" y="43" width="362" height="302" layout="absolute" >
        <mx:Label x="72" y="43" text="Label" id="txtName"/>
        <mx:Button x="132" y="41" label="Button" click="sendmsg(); "/>
    </mx:Panel>
</mx:Application>

 

----再次感谢这个作者给了一个非常好的思路。文章略有修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值