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

结合其他前辈所编写的采用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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值