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

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

Tick.java

package cn.bestwiz.design.tc; import java.math.BigDecimal; import java.util.Date; public class Tick { private BigDecimal askPrice; private BigDecimal bidPrice; private BigDecimal midPrice; private Date tickTime; private String seqno; public String getSeqno() { return seqno; } public void setSeqno(String seqno) { this.seqno = seqno; } public BigDecimal getAskPrice() { return askPrice; } public void setAskPrice(BigDecimal askPrice) { this.askPrice = askPrice; } public BigDecimal getBidPrice() { return bidPrice; } public void setBidPrice(BigDecimal bidPrice) { this.bidPrice = bidPrice; } public BigDecimal getMidPrice() { return midPrice; } public void setMidPrice(BigDecimal midPrice) { this.midPrice = midPrice; } public Date getTickTime() { return tickTime; } public void setTickTime(Date tickTime) { this.tickTime = tickTime; } }

TickCacheServlet.java

package cn.bestwiz.design.tc.servlet; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.bestwiz.design.tc.Tick; import flex.messaging.MessageBroker; import flex.messaging.messages.AsyncMessage; import flex.messaging.util.UUIDUtils; public class TickCacheServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static FeedThread thread; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // String cmd = req.getParameter("cmd"); // if (cmd.equals("start")) { // start(); // } // if (cmd.equals("stop")) { // stop(); // } doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { start(); } public void destroy() { super.destroy(); } public void init() throws ServletException { super.init(); } public void start() { if (thread == null) { thread = new FeedThread(); thread.start(); } System.out.println("start!!"); } public void stop() { thread.running = false; thread = null; } public static class FeedThread extends Thread { public boolean running = true; public void run() { MessageBroker msgBroker = MessageBroker.getMessageBroker(null); String clientID = UUIDUtils.createUUID(); int i = 0; while (running) { Tick tick = new Tick(); tick.setSeqno(String.valueOf(i)); System.out.println(i); AsyncMessage msg = new AsyncMessage(); msg.setDestination("tick-data-feed"); msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME, "tick"); /* * 在此添加过滤 */ Map headers=new HashMap(); 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 (InterruptedException e) { } } } } }

flex_blazeds.mxml

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html" creationComplete="submsg()" height="378" width="426"> <mx:Script> <!--[CDATA[ import mx.controls.Alert; import mx.messaging.ChannelSet; import mx.messaging.Consumer; import mx.messaging.events.MessageEvent; import mx.messaging.channels.StreamingAMFChannel; [Bindable] public var tick:Tick; public function submsg():void { Alert.show("click start"); var consumer:Consumer = new Consumer(); consumer.destination = "tick-data-feed"; consumer.subtopic = "tick"; consumer.selector="prop1 = 10"; var url:String ="http://localhost:8080/flex_blazeds/"; var myStreamingAMF:StreamingAMFChannel = new StreamingAMFChannel(url+"my-streaming-amf", url+"messagebroker/streamingamf"); var channelSet:ChannelSet = new ChannelSet(); channelSet.addChannel(myStreamingAMF); consumer.channelSet = channelSet; consumer.addEventListener(MessageEvent.MESSAGE, messageHandler); consumer.subscribe(); Alert.show("click end"); } private function messageHandler(event:MessageEvent):void { var tick:Tick = event.message.body as Tick; txtTick.text = tick.seqno; text1.text=tick.seqno; } ]]--> </mx:Script> <mx:Panel x="32" y="43" width="362" height="302" layout="absolute" title="Watch Tick"> <mx:Label x="72" y="43" text="Label" id="txtTick"/> <mx:Text id="text1" text="123" x="82" y="10"> </mx:Text> </mx:Panel> </mx:Application>

Tick.as

package { [RemoteClass(alias="cn.bestwiz.design.tc.Tick")] [Bindable] public class Tick { public var askPrice:Number; public var bidPrice:Number; public var midPrice:Number; public var tickTime:Date;; public var seqno:String; } }

triggerServlet.mxml为促发servlet服务

<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns: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[ private function trigger():void{ var request:URLRequest = new URLRequest("http://localhost:8080/flex_blazeds/TickCacheServlet"); request.method = URLRequestMethod.POST; var loader:URLLoader = new URLLoader(); loader.load(request); <!-- loader.addEventListener(Event.COMPLETE, onComplete);--> } ]]--> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <mx:Button label="triggerSev" click="trigger();"> </mx:Button> </s:Application>

在messging-config.xml中添加destination

<destination id="tick-data-feed"> <properties> <server> <allow-subtopics>true</allow-subtopics> <subtopic-separator>.</subtopic-separator> </server> </properties> <channels> <channel ref="my-polling-amf" /> <channel ref="my-streaming-amf" /> </channels> </destination>

在services-config.xml中添加channel

<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel"> <endpoint url="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-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="1"/> <user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="1"/> </user-agent-settings> </properties> </channel-definition>

在web.xml文件中添加

<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</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是接收不到任何消息,触发之后则能够接到消息.

该工作完成感谢网上各位前辈的经验给我的指导,在此有所转载望前辈谅解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值