java服务器向flex客户端一对一推送数据

来源:互联网 发布:adobe encore mac 编辑:程序博客网 时间:2024/06/05 22:45

结合其他前辈所编写的采用BlazeDS的发布-订阅机制实现的一对多的实现,在此基础上采用flex的消息服务的选择过滤功能实现一对一.(其中Tick文件和TickCacheServlet主要部分和flex_blazeds.mxml部分代码为网上前辈所作)

Tick.java

  1. package cn.bestwiz.design.tc;  
  2.   
  3. import java.math.BigDecimal;  
  4. import java.util.Date;  
  5.   
  6. public class Tick {  
  7.       
  8.     private BigDecimal askPrice;  
  9.   
  10.     private BigDecimal bidPrice;  
  11.   
  12.     private BigDecimal midPrice;  
  13.   
  14.     private Date tickTime;  
  15.   
  16.     private String seqno;  
  17.   
  18.     public String getSeqno() {  
  19.         return seqno;  
  20.     }  
  21.   
  22.     public void setSeqno(String seqno) {  
  23.         this.seqno = seqno;  
  24.     }  
  25.   
  26.     public BigDecimal getAskPrice() {  
  27.         return askPrice;  
  28.     }  
  29.   
  30.     public void setAskPrice(BigDecimal askPrice) {  
  31.         this.askPrice = askPrice;  
  32.     }  
  33.   
  34.     public BigDecimal getBidPrice() {  
  35.         return bidPrice;  
  36.     }  
  37.   
  38.     public void setBidPrice(BigDecimal bidPrice) {  
  39.         this.bidPrice = bidPrice;  
  40.     }  
  41.   
  42.     public BigDecimal getMidPrice() {  
  43.         return midPrice;  
  44.     }  
  45.   
  46.     public void setMidPrice(BigDecimal midPrice) {  
  47.         this.midPrice = midPrice;  
  48.     }  
  49.   
  50.     public Date getTickTime() {  
  51.         return tickTime;  
  52.     }  
  53.   
  54.     public void setTickTime(Date tickTime) {  
  55.         this.tickTime = tickTime;  
  56.     }  
  57.   
  58.   
  59.   
  60. }  

TickCacheServlet.java

 

 

  1. package cn.bestwiz.design.tc.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import cn.bestwiz.design.tc.Tick;  
  13. import flex.messaging.MessageBroker;  
  14. import flex.messaging.messages.AsyncMessage;  
  15. import flex.messaging.util.UUIDUtils;  
  16.   
  17. public class TickCacheServlet extends HttpServlet {  
  18.     private static final long serialVersionUID = 1L;  
  19.     private static FeedThread thread;  
  20.   
  21.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  22.             throws ServletException, IOException {  
  23.   
  24. //      String cmd = req.getParameter("cmd");  
  25. //      if (cmd.equals("start")) {  
  26. //          start();  
  27. //      }  
  28. //      if (cmd.equals("stop")) {  
  29. //          stop();  
  30. //      }  
  31.         doPost(req, resp);  
  32.     }  
  33.   
  34.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  35.             throws ServletException, IOException {  
  36.           
  37.         start();  
  38.           
  39.     }  
  40.   
  41.     public void destroy() {  
  42.         super.destroy();  
  43.     }  
  44.   
  45.     public void init() throws ServletException {  
  46.         super.init();  
  47.     }  
  48.   
  49.     public void start() {  
  50.         if (thread == null) {  
  51.             thread = new FeedThread();  
  52.             thread.start();  
  53.         }  
  54.         System.out.println("start!!");  
  55.     }  
  56.   
  57.     public void stop() {  
  58.         thread.running = false;  
  59.         thread = null;  
  60.     }  
  61.   
  62.     public static class FeedThread extends Thread {  
  63.   
  64.         public boolean running = true;  
  65.   
  66.         public void run() {  
  67.             MessageBroker msgBroker = MessageBroker.getMessageBroker(null);  
  68.             String clientID = UUIDUtils.createUUID();  
  69.             int i = 0;  
  70.             while (running) {  
  71.                 i++;
  72.                 Tick tick = new Tick();  
  73.                 tick.setSeqno(String.valueOf(i));  
  74.                 System.out.println(i);  
  75.   
  76.                 AsyncMessage msg = new AsyncMessage();  
  77.                 msg.setDestination("tick-data-feed");  
  78.                 msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME, "tick");  
  79.                 /* 
  80.                  * 在此添加过滤 
  81.                  */  
  82.                 Map headers=new HashMap();  
  83.                 headers.put("prop1"10);  
  84.                 msg.setHeaders(headers);  
  85.                   
  86.                 msg.setClientId(clientID);  
  87.                 msg.setMessageId(UUIDUtils.createUUID());  
  88.                 msg.setTimestamp(System.currentTimeMillis());  
  89.                 msg.setBody(tick);  
  90.                 msgBroker.routeMessageToService(msg, null);  
  91.                   
  92.                 try {  
  93.                     Thread.sleep(2000);  
  94.                 } catch (InterruptedException e) {  
  95.                 }  
  96.             }  
  97.         }  
  98.     }  
  99. }  

 

 

flex_blazeds.mxml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html"  
  3.                   
  4.                  creationComplete="submsg()" height="378" width="426">  
  5.       
  6.     <mx:Script>  
  7.         <!--[CDATA[  
  8.             import mx.controls.Alert;  
  9.             import mx.messaging.ChannelSet;  
  10.             import mx.messaging.Consumer;  
  11.             import mx.messaging.events.MessageEvent;  
  12.             import mx.messaging.channels.StreamingAMFChannel;  
  13.               
  14.             [Bindable]  
  15.             public var tick:Tick;  
  16.               
  17.             public function submsg():void  
  18.             {  
  19.                 Alert.show("click start");  
  20.                 var consumer:Consumer = new Consumer();  
  21.                 consumer.destination = "tick-data-feed";  
  22.                 consumer.subtopic = "tick";  
  23.                 consumer.selector="prop1 = 10";  
  24.                 var url:String ="http://localhost:8080/flex_blazeds/";   
  25.                 var myStreamingAMF:StreamingAMFChannel =  new StreamingAMFChannel(url+"my-streaming-amf", url+"messagebroker/streamingamf");    
  26.                 var channelSet:ChannelSet = new ChannelSet();  
  27.                 channelSet.addChannel(myStreamingAMF);  
  28.                 consumer.channelSet = channelSet;   
  29.                   
  30.                   
  31.                 consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);  
  32.                 consumer.subscribe();  
  33.                 Alert.show("click end");  
  34.                   
  35.             }  
  36.               
  37.             private function messageHandler(event:MessageEvent):void   
  38.             {  
  39.                 var tick:Tick = event.message.body as Tick;  
  40.                 txtTick.text = tick.seqno;  
  41.                 text1.text=tick.seqno;  
  42.             }  
  43.         ]]-->  
  44.     </mx:Script>  
  45.       
  46.           
  47.       
  48.     <mx:Panel x="32" y="43" width="362" height="302" layout="absolute" title="Watch Tick">  
  49.         <mx:Label x="72" y="43" text="Label" id="txtTick"/>  
  50.         <mx:Text id="text1" text="123" x="82" y="10">  
  51.               
  52.         </mx:Text>  
  53.           
  54.             </mx:Panel>  
  55. </mx:Application>  

 

Tick.as

  1. package  
  2. {  
  3.     [RemoteClass(alias="cn.bestwiz.design.tc.Tick")]  
  4.     [Bindable]  
  5.     public class Tick  
  6.     {          
  7.         public var askPrice:Number;  
  8.         public var bidPrice:Number;  
  9.         public var midPrice:Number;  
  10.         public var tickTime:Date;;  
  11.         public var seqno:String;  
  12.     }  
  13.       
  14. }  

triggerServlet.mxml为促发servlet服务

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">  
  5.     <s:layout>  
  6.         <s:BasicLayout/>  
  7.     </s:layout>  
  8.       
  9.     <fx:Script>  
  10.         <!--[CDATA[  
  11.             private function trigger():void{  
  12.                 var request:URLRequest = new URLRequest("http://localhost:8080/flex_blazeds/TickCacheServlet");  
  13.                 request.method = URLRequestMethod.POST;  
  14.                 var loader:URLLoader = new URLLoader();  
  15.                 loader.load(request);  
  16.                   
  17.                 <!-- loader.addEventListener(Event.COMPLETE, onComplete);-->    
  18.             }  
  19.               
  20.         ]]-->  
  21.     </fx:Script>  
  22.     <fx:Declarations>  
  23.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  24.           
  25.           
  26.     </fx:Declarations>  
  27.     <mx:Button label="triggerSev" click="trigger();">  
  28.           
  29.     </mx:Button>  
  30.       
  31. </s:Application>  

 

在messging-config.xml中添加destination

  1. <destination id="tick-data-feed">  
  2.         <properties>  
  3.             <server>  
  4.                 <allow-subtopics>true</allow-subtopics>  
  5.                 <subtopic-separator>.</subtopic-separator>  
  6.             </server>  
  7.         </properties>  
  8.         <channels>  
  9.             <channel ref="my-polling-amf" />  
  10.             <channel ref="my-streaming-amf" />  
  11.         </channels>  
  12.     </destination>  

在services-config.xml中添加channel

  1. <channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">  
  2.             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/>  
  3.             <properties>  
  4.                 <idle-timeout-minutes>0</idle-timeout-minutes>  
  5.                 <max-streaming-clients>10</max-streaming-clients>  
  6.                 <server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis>  
  7.                 <user-agent-settings>  
  8.                     <user-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="1"/>  
  9.                     <user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="1"/>  
  10.                 </user-agent-settings>  
  11.             </properties>  
  12.         </channel-definition>   

在web.xml文件中添加

  1. <servlet>  
  2.     <description>This is the description of my J2EE component</description>  
  3.     <display-name>This is the display name of my J2EE component</display-name>  
  4.     <servlet-name>TickCacheServlet</servlet-name>  
  5.     <servlet-class>cn.bestwiz.design.tc.servlet.TickCacheServlet</servlet-class>  
  6.   </servlet>  
  7.   
  8.   
  9.       <servlet-mapping>  
  10.     <servlet-name>TickCacheServlet</servlet-name>  
  11.     <url-pattern>/TickCacheServlet</url-pattern>  
  12.   </servlet-mapping>  

 

至此大功告成,首先将项目发布到tomcat上,然后先运行triggerservlet订阅,在运行flex_blazeds.mxml会看到结果。

,在未发布(触发)之前,订阅端flex_blazeds.mxml是接收不到任何消息,触发之后则能够接到消息.


测试可用。

0 0