Flex BlazeDS 数据推送

落花浮王杯 提交于 2020-02-18 23:37:28

BlazeDS有数据推送功能,Flex只需要创建一个订阅者,便可以接收到服务主动推送到前台的消息

实现过程中出现可能会出现的错误:

MessageBroker msgBroker = MessageBroker.getMessageBroker(null);  这个实例返回null

解决方法:不能直接运行java 中的servlet start()方法,需要通过浏览器访问 http://localhost:8080/project/TickCacheServlet?cmd=start

记得在web.xml 配置TickCacheServlet

<servlet>
   <servlet-name>TickCacheServlet</servlet-name> 
   <servlet-class>com.TickCacheServlet</servlet-class> 
   </servlet>
   
   <servlet-mapping>
   <servlet-name>TickCacheServlet</servlet-name> 
   <url-pattern>/TickCacheServlet</url-pattern> 
</servlet-mapping>

 

messaging-config.xml

<destination id="tick-data-feed">  
        <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>

 

services-config.xml 的channels节点

<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">  
            <endpoint url="http://localhost:8080/project/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>

 

TickCacheServlet.java

package com;
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.FlexContext;
import flex.messaging.messages.AsyncMessage;     
import flex.messaging.util.UUIDUtils;     
    
public class TickCacheServlet extends HttpServlet {     
    
    /**   
     *    
     */    
    private static final long serialVersionUID = 1L;     
    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);
//         MessageBroker msgBroker = FlexContext.getMessageBroker();
            String clientID = UUIDUtils.createUUID();     
            int i = 0;     
            while (running) {     
                Tick tick = new Tick();     
                tick.setAskPrice(new BigDecimal("100"));     
                tick.setBidPrice(new BigDecimal("100"));     
                tick.setMidPrice(new BigDecimal("100"));     
                tick.setTickTime(new Date());     
    
                tick.setSeqno(String.valueOf(i));     
                System.out.println(i);     
    
                AsyncMessage msg = new AsyncMessage();     
                msg.setDestination("tick-data-feed");     
                msg.setHeader("DSSubtopic", "tick");     
                msg.setClientId(clientID);     
                msg.setMessageId(UUIDUtils.createUUID());     
                msg.setTimestamp(System.currentTimeMillis());     
                msg.setBody(tick);     
                if(msgBroker != null){
                    msgBroker.routeMessageToService(msg, null);
                    System.out.println("start!!");
                }
     
                i++;     
                try {     
                    Thread.sleep(1000);     
                } catch (InterruptedException e) {     
                }     
    
            }     
        }     
    }
    public static void main(String[] args)
    {
     TickCacheServlet fe = new TickCacheServlet();
     fe.start();
    }
    
}

Tick.java

package com;
import java.math.BigDecimal;     
import java.util.Date;     
    
public class Tick {     
    private BigDecimal askPrice;     
    
    private BigDecimal bidPrice;     
    
    private BigDecimal midPrice;     
    
    private Date tickTime;     
    
    private String seqno;     
    
    public String getSeqno() {     
        return seqno;     
    }     
    
    public void setSeqno(String seqno) {     
        this.seqno = seqno;     
    }     
    
    public BigDecimal getAskPrice() {     
        return askPrice;     
    }     
    
    public void setAskPrice(BigDecimal askPrice) {     
        this.askPrice = askPrice;     
    }     
    
    public BigDecimal getBidPrice() {     
        return bidPrice;     
    }     
    
    public void setBidPrice(BigDecimal bidPrice) {     
        this.bidPrice = bidPrice;     
    }     
    
    public BigDecimal getMidPrice() {     
        return midPrice;     
    }     
    
    public void setMidPrice(BigDecimal midPrice) {     
        this.midPrice = midPrice;     
    }     
    
    public Date getTickTime() {     
        return tickTime;     
    }     
    
    public void setTickTime(Date tickTime) {     
        this.tickTime = tickTime;     
    }     
    
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!