登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

股票实时行情例子  

2009-12-10 14:02:32|  分类: flex技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1、 采用推技术

1) 设计

a)服务器端设计

建立服务器数据读取线程

FeedThread,功能定时从文件或数据库中读取股票行情数据。

采用flash技术,如下:

AsyncMessage msg= new AsyncMessage();

msg.setDestination("market-data-feed");//

market-data-feed在message-config.xml定义如下:

<destination id="market-data-feed">

        <properties>

            <server>

                <allow-subtopics>true</allow-subtopics>

                <subtopic-separator>.</subtopic-separator>s

            </server>

        </properties>

        <channels>

           <channel ref="my-polling-amf"/>

           <channel ref="my-streaming-amf"/>

            <channel ref="per-client-qos-polling-amf"/>

        </channels>       

    </destination>

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

b)客户端设计

1)客户端建立连接(traderdesktop.mxml)

功能:初始化股票列表(可通过建立数据库服务,获取股票列表)。

initializeWatchList(["IBM", "JBLU", "ADBE", "GE", "C"]);

 
     

 

2)每个股票建立订阅机制。

private function subscribe(symbol:String):void

{

    var consumer:Consumer = new Consumer();

    consumer.destination = "market-data-feed";

    consumer.subtopic = symbol;

    consumer.channelSet = new ChannelSet([channels.selectedItem]);

    consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);

    consumer.subscribe();

    consumers[symbol] = consumer;

}

 

 

 

 

 

 

 

 

 

 

 

3)客户端接收服务CustomDelayQueueProcessor extends FlexClientOutboundQueueProcessor

功能:接收从consumer.destination过来的数据,延迟处理。

public FlushResult flush(List outboundQueue)

    {

    int delay = delayTimeBetweenFlushes;

    // Read custom delay from client's FlexClient instance

    FlexClient flexClient = getFlexClient();

    if (flexClient != null)

    {

       Object obj = flexClient.getAttribute("market-data-delay");

        if (obj != null)

        {

              try {

                  delay = Integer.parseInt((String) obj);

              } catch (NumberFormatException ignore) {

              }          

        }

    }      

        long currentTime = System.currentTimeMillis();

    if ((currentTime - lastFlushTime) < delay)

        {

            // Delaying flush. No messages will be returned at this point

            FlushResult flushResult = new FlushResult();

            // Don't return any messages to flush.

            // And request that the next flush doesn't occur until 3 seconds since the previous.

            flushResult.setNextFlushWaitTimeMillis((int)(delay - (currentTime - lastFlushTime)));

            return flushResult;

        }

        else // OK to flush.

        {

            // Flushing. All queued messages will now be returned

            lastFlushTime = currentTime;   

            FlushResult flushResult = new FlushResult();

            flushResult.setNextFlushWaitTimeMillis(delay);

            flushResult.setMessages(new ArrayList(outboundQueue));

            outboundQueue.clear();       

            return flushResult;

        }

    }

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

        4)显示

               数据以列表显示

<mx:Panel title="Watch List" width="400" height="400">

<mx:DataGrid id="dg" dataProvider="{items}" width="100%" height="100%">

    <mx:columns>

     <mx:DataGridColumn headerText="Symbol" dataField="symbol" width="80"/>

     <mx:DataGridColumn headerText="Open" dataField="open" labelFunction="formatNumber" textAlign="right" width="60"/>

     <mx:DataGridColumn headerText="Last" dataField="last" itemRenderer="BackgroundColorRenderer" labelFunction="formatNumber" textAlign="right" width="60"/>

    <mx:DataGridColumn headerText="Change" dataField="change" itemRenderer="ColorRenderer" labelFunction="formatNumber" textAlign="right" width="60"/>

    <mx:DataGridColumn headerText="High" dataField="high" labelFunction="formatNumber" textAlign="right" width="60"/>

    <mx:DataGridColumn headerText="Low" dataField="low" labelFunction="formatNumber" textAlign="right" width="60"/>

</mx:columns>

</mx:DataGrid>

<mx:ControlBar>

    <mx:TextInput id="symbol" enter="addSymbol(symbol.text);symbol.text='';" width="50"/>

    <mx:Button label="Add Symbol" click="addSymbol(symbol.text);symbol.text='';"/>

    <mx:Spacer width="100%"/>

    <mx:Button label="Delete Symbol" click="deleteSymbol()" enabled="{dg.selectedItem}"/>

</mx:ControlBar>

</mx:Panel>

 
             

   数据列表渲染(底色、文字颜色等)




 

protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void

        {

           super.updateDisplayList(unscaledWidth, unscaledHeight);

           if (data && listData && data[DataGridListData(listData).dataField] < 0)

           {

               setStyle("color", 0xFF0000);

            }

            else

            {

               setStyle("color", 0x009900);

            }

       }


override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void

        {

           super.updateDisplayList(unscaledWidth, unscaledHeight);

          

           var g:Graphics = graphics;

          

           g.clear();

 

           if (data && data.date && data.symbol == symbol)

           {

              if( data.change && data.change >= 0 )

              {

                  g.beginFill(0x009900, 0.5);

                  g.drawRect(0, 0, unscaledWidth, unscaledHeight);

                  g.endFill();

              }

              else

              {

                  g.beginFill(0xFF0000, 0.5);

                  g.drawRect(0, 0, unscaledWidth, unscaledHeight);

                  g.endFill();

              }

           }

       }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  评论这张
 
阅读(1353)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018