结合其他前辈所编写的采用BlazeDS的发布-订阅机制实现的一对多的实现,在此基础上采用flex的消息服务的选择过滤功能实现一对一.(其中Tick文件和TickCacheServlet主要部分和flex_blazeds.mxml部分代码为网上前辈所作)
Tick.java
- packagecn.bestwiz.design.tc;
- importjava.math.BigDecimal;
- importjava.util.Date;
- publicclassTick{
- privateBigDecimalaskPrice;
- privateBigDecimalbidPrice;
- privateBigDecimalmidPrice;
- privateDatetickTime;
- privateStringseqno;
- publicStringgetSeqno(){
- returnseqno;
- }
- publicvoidsetSeqno(Stringseqno){
- this.seqno=seqno;
- }
- publicBigDecimalgetAskPrice(){
- returnaskPrice;
- }
- publicvoidsetAskPrice(BigDecimalaskPrice){
- this.askPrice=askPrice;
- }
- publicBigDecimalgetBidPrice(){
- returnbidPrice;
- }
- publicvoidsetBidPrice(BigDecimalbidPrice){
- this.bidPrice=bidPrice;
- }
- publicBigDecimalgetMidPrice(){
- returnmidPrice;
- }
- publicvoidsetMidPrice(BigDecimalmidPrice){
- this.midPrice=midPrice;
- }
- publicDategetTickTime(){
- returntickTime;
- }
- publicvoidsetTickTime(DatetickTime){
- this.tickTime=tickTime;
- }
- }
TickCacheServlet.java
- packagecn.bestwiz.design.tc.servlet;
- importjava.io.IOException;
- importjava.util.HashMap;
- importjava.util.Map;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importcn.bestwiz.design.tc.Tick;
- importflex.messaging.MessageBroker;
- importflex.messaging.messages.AsyncMessage;
- importflex.messaging.util.UUIDUtils;
- publicclassTickCacheServletextendsHttpServlet{
- privatestaticfinallongserialVersionUID=1L;
- privatestaticFeedThreadthread;
- protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
- throwsServletException,IOException{
- //Stringcmd=req.getParameter("cmd");
- //if(cmd.equals("start")){
- //start();
- //}
- //if(cmd.equals("stop")){
- //stop();
- //}
- doPost(req,resp);
- }
- protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
- throwsServletException,IOException{
- start();
- }
- publicvoiddestroy(){
- super.destroy();
- }
- publicvoidinit()throwsServletException{
- super.init();
- }
- publicvoidstart(){
- if(thread==null){
- thread=newFeedThread();
- thread.start();
- }
- System.out.println("start!!");
- }
- publicvoidstop(){
- thread.running=false;
- thread=null;
- }
- publicstaticclassFeedThreadextendsThread{
- publicbooleanrunning=true;
- publicvoidrun(){
- MessageBrokermsgBroker=MessageBroker.getMessageBroker(null);
- StringclientID=UUIDUtils.createUUID();
- inti=0;
- while(running){
- i++;
- Ticktick=newTick();
- tick.setSeqno(String.valueOf(i));
- System.out.println(i);
- AsyncMessagemsg=newAsyncMessage();
- msg.setDestination("tick-data-feed");
- msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME,"tick");
- /*
- *在此添加过滤
- */
- Mapheaders=newHashMap();
- headers.put("prop1",10);
- msg.setHeaders(headers);
- msg.setClientId(clientID);
- msg.setMessageId(UUIDUtils.createUUID());
- msg.setTimestamp(System.currentTimeMillis());
- msg.setBody(tick);
- msgBroker.routeMessageToService(msg,null);
- try{
- Thread.sleep(2000);
- }catch(InterruptedExceptione){
- }
- }
- }
- }
- }
flex_blazeds.mxml
- <?xmlversion="1.0"encoding="utf-8"?>
- <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"viewSourceURL="srcview/index.html"
- creationComplete="submsg()"height="378"width="426">
- <mx:Script>
- <!--[CDATA[
- importmx.controls.Alert;
- importmx.messaging.ChannelSet;
- importmx.messaging.Consumer;
- importmx.messaging.events.MessageEvent;
- importmx.messaging.channels.StreamingAMFChannel;
- [Bindable]
- publicvartick:Tick;
- publicfunctionsubmsg():void
- {
- Alert.show("clickstart");
- varconsumer:Consumer=newConsumer();
- consumer.destination="tick-data-feed";
- consumer.subtopic="tick";
- consumer.selector="prop1=10";
- varurl:String="http://localhost:8080/flex_blazeds/";
- varmyStreamingAMF:StreamingAMFChannel=newStreamingAMFChannel(url+"my-streaming-amf",url+"messagebroker/streamingamf");
- varchannelSet:ChannelSet=newChannelSet();
- channelSet.addChannel(myStreamingAMF);
- consumer.channelSet=channelSet;
- consumer.addEventListener(MessageEvent.MESSAGE,messageHandler);
- consumer.subscribe();
- Alert.show("clickend");
- }
- privatefunctionmessageHandler(event:MessageEvent):void
- {
- vartick:Tick=event.message.bodyasTick;
- txtTick.text=tick.seqno;
- text1.text=tick.seqno;
- }
- ]]-->
- </mx:Script>
- <mx:Panelx="32"y="43"width="362"height="302"layout="absolute"title="WatchTick">
- <mx:Labelx="72"y="43"text="Label"id="txtTick"/>
- <mx:Textid="text1"text="123"x="82"y="10">
- </mx:Text>
- </mx:Panel>
- </mx:Application>
Tick.as
- package
- {
- [RemoteClass(alias="cn.bestwiz.design.tc.Tick")]
- [Bindable]
- publicclassTick
- {
- publicvaraskPrice:Number;
- publicvarbidPrice:Number;
- publicvarmidPrice:Number;
- publicvartickTime:Date;;
- publicvarseqno:String;
- }
- }
triggerServlet.mxml为促发servlet服务
- <?xmlversion="1.0"encoding="utf-8"?>
- <s:Applicationxmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx"minWidth="955"minHeight="600">
- <s:layout>
- <s:BasicLayout/>
- </s:layout>
- <fx:Script>
- <!--[CDATA[
- privatefunctiontrigger():void{
- varrequest:URLRequest=newURLRequest("http://localhost:8080/flex_blazeds/TickCacheServlet");
- request.method=URLRequestMethod.POST;
- varloader:URLLoader=newURLLoader();
- loader.load(request);
- <!--loader.addEventListener(Event.COMPLETE,onComplete);-->
- }
- ]]-->
- </fx:Script>
- <fx:Declarations>
- <!--将非可视元素(例如服务、值对象)放在此处-->
- </fx:Declarations>
- <mx:Buttonlabel="triggerSev"click="trigger();">
- </mx:Button>
- </s:Application>
在messging-config.xml中添加destination
- <destinationid="tick-data-feed">
- <properties>
- <server>
- <allow-subtopics>true</allow-subtopics>
- <subtopic-separator>.</subtopic-separator>
- </server>
- </properties>
- <channels>
- <channelref="my-polling-amf"/>
- <channelref="my-streaming-amf"/>
- </channels>
- </destination>
在services-config.xml中添加channel
- <channel-definitionid="my-streaming-amf"class="mx.messaging.channels.StreamingAMFChannel">
- <endpointurl="http://{server.name}:{server.port}/{context.root}/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-agentmatch-on="MSIE"kickstart-bytes="2048"max-streaming-connections-per-session="1"/>
- <user-agentmatch-on="Firefox"kickstart-bytes="2048"max-streaming-connections-per-session="1"/>
- </user-agent-settings>
- </properties>
- </channel-definition>
在web.xml文件中添加
- <servlet>
- <description>ThisisthedescriptionofmyJ2EEcomponent</description>
- <display-name>ThisisthedisplaynameofmyJ2EEcomponent</display-name>
- <servlet-name>TickCacheServlet</servlet-name>
- <servlet-class>cn.bestwiz.design.tc.servlet.TickCacheServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>TickCacheServlet</servlet-name>
- <url-pattern>/TickCacheServlet</url-pattern>
- </servlet-mapping>
至此大功告成,首先将项目发布到tomcat上,然后先运行triggerservlet订阅,在运行flex_blazeds.mxml会看到结果。
,在未发布(触发)之前,订阅端flex_blazeds.mxml是接收不到任何消息,触发之后则能够接到消息.
测试可用。