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

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

Tick.java

  1. packagecn.bestwiz.design.tc;
  2. importjava.math.BigDecimal;
  3. importjava.util.Date;
  4. publicclassTick{
  5. privateBigDecimalaskPrice;
  6. privateBigDecimalbidPrice;
  7. privateBigDecimalmidPrice;
  8. privateDatetickTime;
  9. privateStringseqno;
  10. publicStringgetSeqno(){
  11. returnseqno;
  12. }
  13. publicvoidsetSeqno(Stringseqno){
  14. this.seqno=seqno;
  15. }
  16. publicBigDecimalgetAskPrice(){
  17. returnaskPrice;
  18. }
  19. publicvoidsetAskPrice(BigDecimalaskPrice){
  20. this.askPrice=askPrice;
  21. }
  22. publicBigDecimalgetBidPrice(){
  23. returnbidPrice;
  24. }
  25. publicvoidsetBidPrice(BigDecimalbidPrice){
  26. this.bidPrice=bidPrice;
  27. }
  28. publicBigDecimalgetMidPrice(){
  29. returnmidPrice;
  30. }
  31. publicvoidsetMidPrice(BigDecimalmidPrice){
  32. this.midPrice=midPrice;
  33. }
  34. publicDategetTickTime(){
  35. returntickTime;
  36. }
  37. publicvoidsetTickTime(DatetickTime){
  38. this.tickTime=tickTime;
  39. }
  40. }

TickCacheServlet.java

  1. packagecn.bestwiz.design.tc.servlet;
  2. importjava.io.IOException;
  3. importjava.util.HashMap;
  4. importjava.util.Map;
  5. importjavax.servlet.ServletException;
  6. importjavax.servlet.http.HttpServlet;
  7. importjavax.servlet.http.HttpServletRequest;
  8. importjavax.servlet.http.HttpServletResponse;
  9. importcn.bestwiz.design.tc.Tick;
  10. importflex.messaging.MessageBroker;
  11. importflex.messaging.messages.AsyncMessage;
  12. importflex.messaging.util.UUIDUtils;
  13. publicclassTickCacheServletextendsHttpServlet{
  14. privatestaticfinallongserialVersionUID=1L;
  15. privatestaticFeedThreadthread;
  16. protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
  17. throwsServletException,IOException{
  18. //Stringcmd=req.getParameter("cmd");
  19. //if(cmd.equals("start")){
  20. //start();
  21. //}
  22. //if(cmd.equals("stop")){
  23. //stop();
  24. //}
  25. doPost(req,resp);
  26. }
  27. protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
  28. throwsServletException,IOException{
  29. start();
  30. }
  31. publicvoiddestroy(){
  32. super.destroy();
  33. }
  34. publicvoidinit()throwsServletException{
  35. super.init();
  36. }
  37. publicvoidstart(){
  38. if(thread==null){
  39. thread=newFeedThread();
  40. thread.start();
  41. }
  42. System.out.println("start!!");
  43. }
  44. publicvoidstop(){
  45. thread.running=false;
  46. thread=null;
  47. }
  48. publicstaticclassFeedThreadextendsThread{
  49. publicbooleanrunning=true;
  50. publicvoidrun(){
  51. MessageBrokermsgBroker=MessageBroker.getMessageBroker(null);
  52. StringclientID=UUIDUtils.createUUID();
  53. inti=0;
  54. while(running){
  55. i++;
  56. Ticktick=newTick();
  57. tick.setSeqno(String.valueOf(i));
  58. System.out.println(i);
  59. AsyncMessagemsg=newAsyncMessage();
  60. msg.setDestination("tick-data-feed");
  61. msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME,"tick");
  62. /*
  63. *在此添加过滤
  64. */
  65. Mapheaders=newHashMap();
  66. headers.put("prop1",10);
  67. msg.setHeaders(headers);
  68. msg.setClientId(clientID);
  69. msg.setMessageId(UUIDUtils.createUUID());
  70. msg.setTimestamp(System.currentTimeMillis());
  71. msg.setBody(tick);
  72. msgBroker.routeMessageToService(msg,null);
  73. try{
  74. Thread.sleep(2000);
  75. }catch(InterruptedExceptione){
  76. }
  77. }
  78. }
  79. }
  80. }

flex_blazeds.mxml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"viewSourceURL="srcview/index.html"
  3. creationComplete="submsg()"height="378"width="426">
  4. <mx:Script>
  5. <!--[CDATA[
  6. importmx.controls.Alert;
  7. importmx.messaging.ChannelSet;
  8. importmx.messaging.Consumer;
  9. importmx.messaging.events.MessageEvent;
  10. importmx.messaging.channels.StreamingAMFChannel;
  11. [Bindable]
  12. publicvartick:Tick;
  13. publicfunctionsubmsg():void
  14. {
  15. Alert.show("clickstart");
  16. varconsumer:Consumer=newConsumer();
  17. consumer.destination="tick-data-feed";
  18. consumer.subtopic="tick";
  19. consumer.selector="prop1=10";
  20. varurl:String="http://localhost:8080/flex_blazeds/";
  21. varmyStreamingAMF:StreamingAMFChannel=newStreamingAMFChannel(url+"my-streaming-amf",url+"messagebroker/streamingamf");
  22. varchannelSet:ChannelSet=newChannelSet();
  23. channelSet.addChannel(myStreamingAMF);
  24. consumer.channelSet=channelSet;
  25. consumer.addEventListener(MessageEvent.MESSAGE,messageHandler);
  26. consumer.subscribe();
  27. Alert.show("clickend");
  28. }
  29. privatefunctionmessageHandler(event:MessageEvent):void
  30. {
  31. vartick:Tick=event.message.bodyasTick;
  32. txtTick.text=tick.seqno;
  33. text1.text=tick.seqno;
  34. }
  35. ]]-->
  36. </mx:Script>
  37. <mx:Panelx="32"y="43"width="362"height="302"layout="absolute"title="WatchTick">
  38. <mx:Labelx="72"y="43"text="Label"id="txtTick"/>
  39. <mx:Textid="text1"text="123"x="82"y="10">
  40. </mx:Text>
  41. </mx:Panel>
  42. </mx:Application>

Tick.as

  1. package
  2. {
  3. [RemoteClass(alias="cn.bestwiz.design.tc.Tick")]
  4. [Bindable]
  5. publicclassTick
  6. {
  7. publicvaraskPrice:Number;
  8. publicvarbidPrice:Number;
  9. publicvarmidPrice:Number;
  10. publicvartickTime:Date;;
  11. publicvarseqno:String;
  12. }
  13. }

triggerServlet.mxml为促发servlet服务

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <s:Applicationxmlns: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. <fx:Script>
  9. <!--[CDATA[
  10. privatefunctiontrigger():void{
  11. varrequest:URLRequest=newURLRequest("http://localhost:8080/flex_blazeds/TickCacheServlet");
  12. request.method=URLRequestMethod.POST;
  13. varloader:URLLoader=newURLLoader();
  14. loader.load(request);
  15. <!--loader.addEventListener(Event.COMPLETE,onComplete);-->
  16. }
  17. ]]-->
  18. </fx:Script>
  19. <fx:Declarations>
  20. <!--将非可视元素(例如服务、值对象)放在此处-->
  21. </fx:Declarations>
  22. <mx:Buttonlabel="triggerSev"click="trigger();">
  23. </mx:Button>
  24. </s:Application>

在messging-config.xml中添加destination

  1. <destinationid="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. <channelref="my-polling-amf"/>
  10. <channelref="my-streaming-amf"/>
  11. </channels>
  12. </destination>

在services-config.xml中添加channel

  1. <channel-definitionid="my-streaming-amf"class="mx.messaging.channels.StreamingAMFChannel">
  2. <endpointurl="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-agentmatch-on="MSIE"kickstart-bytes="2048"max-streaming-connections-per-session="1"/>
  9. <user-agentmatch-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>ThisisthedescriptionofmyJ2EEcomponent</description>
  3. <display-name>ThisisthedisplaynameofmyJ2EEcomponent</display-name>
  4. <servlet-name>TickCacheServlet</servlet-name>
  5. <servlet-class>cn.bestwiz.design.tc.servlet.TickCacheServlet</servlet-class>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>TickCacheServlet</servlet-name>
  9. <url-pattern>/TickCacheServlet</url-pattern>
  10. </servlet-mapping>

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

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


测试可用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值