blazeds推送技术至Flex zz 转自http://www.sloppy.cn/blog/article/247.htm 第一步下载blazeds.war 第二步修改两个配置文件:services-config.xml,messaging-config.xml services-config.xml加入下面代码 程序代码 <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> messaging-config.xml加入下面的代码 程序代码 <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> 第三步:创建一个Servlet: 程序代码 package cn.bestwiz.design.tc.servlet; import java.io.IOException; import java.math.BigDecimal; import java.util.Date; 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; @Override 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(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); } @Override public void init() throws ServletException { // TODO Auto-generated method stub 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.setAskPrice(new BigDecimal("100")); tick.setBidPrice(new BigDecimal("100")); tick.setMidPrice(new BigDecimal("100")); tick.setTickTime(new Date()); tick.setSeqno(String.valueOf(i)); System.out.println(i); AsyncMessage msg = new AsyncMessage(); msg.setDestination("tick-data-feed"); msg.setHeader("DSSubtopic", "tick"); msg.setClientId(clientID); msg.setMessageId(UUIDUtils.createUUID()); msg.setTimestamp(System.currentTimeMillis()); msg.setBody(tick); msgBroker.routeMessageToService(msg, null); i++; try { Thread.sleep(20); } catch (InterruptedException e) { } } } } } 第四步:创建一个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; } } 第五步: 配置Flex项目: Root Folder:C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/blazeds Root URL:http://localhost:8080/blazeds Context Root:/blazeds 第六步:创建AS类: 程序代码 package cn.sloppy { [RemoteClass(alias="cn.bestwiz.design.tc.Tick")] [Bindable] public class Tick { public function Tick() { } public var askPrice:Number; public var bidPrice:Number; public var midPrice:Number; public var tickTimeate;; public var seqno:String; } } 第七步:Flex主程序代码 程序代码 // import mx.controls.Alert; import mx.rpc.events.ResultEvent; import mx.messaging.Consumer; import mx.messaging.Channel; import mx.messaging.ChannelSet; import mx.messaging.events.MessageEvent; // [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.channelSet = new ChannelSet(["my-streaming-amf"]); 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; } <mx:Panel x="524" y="47" width="362" height="302" layout="absolute" title="Watch Tick"> <mx:Label x="72" y="43" text="Label" id="txtTick"/> <mx:Button x="132" y="41" label="Button" click="submsg(); "/> </mx:Panel> 第七步:运行Flex:http://localhost:8080/blazeds/HelloWorld-debug/HelloWorld.html?debug=true 点击Button 迂曲运行Servlet:http://localhost:8080/blazeds/TickCacheServlet?cmd=start 再看看Flex的效果:是不是文字一直在增长? 恭喜。成功了。