Flex与java Server Push数据_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Flex与java Server Push数据

Flex与java Server Push数据

 2014/8/7 18:37:03  dk05408  程序员俱乐部  我要评论(0)
  • 摘要:我根据网上Push实例进行整理下,可以实现Push功能myeclipse8+flex4.61.Flex工程<?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
  • 标签:Server Java 数据 Flex

我根据网上Push实例进行整理下,可以实现Push功能 myeclipse8+flex4.6?

1.Flex 工程

class="java" name="code"><?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" creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			import mx.messaging.channels.StreamingAMFChannel;
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				
			}
			
			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;
			import com.Tick;
			[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();   */
				var streamingAMFChannel:StreamingAMFChannel=new StreamingAMFChannel();
				//streamingAMFChannel.requestTimeout=0;
				streamingAMFChannel.url="http://localhost:8080/pushDemo/messagebroker/streamingamf";
				var consumer:Consumer=new Consumer();
				consumer.destination="tick-data-feed";
				consumer.subtopic="tick";
				var set:ChannelSet=new ChannelSet();
				set.addChannel(streamingAMFChannel);
				consumer.channelSet=set; 
				//consumer.channelSet = new ChannelSet(["my-streaming-amf"]);
				consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);
				consumer.unsubscribe();
				consumer.subscribe();
				Alert.show("click end");  
			}  
			
			private function messageHandler(event:MessageEvent):void   
			{  
				var tick:Tick = event.message.body as Tick;  
				txtTick.text = tick.seqno;  
				area.text = "askPrice"+tick.askPrice+  
					"bidPrice"+tick.bidPrice+  
					"midPrice"+tick.midPrice+  
					"tickTime"+tick.tickTime+  
					"seqno"+tick.seqno; 
			} 
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<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(); "/>
		<s:TextArea x="53" y="96" width="259" id="area" height="152"/>
	</mx:Panel>
</s:Application>

?2.java 工程

package com.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 com.model.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;  

	/**
	 * Constructor of the object.
	 */
	public TickCacheServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	      String cmd = request.getParameter("cmd");  
	        if (cmd.equals("start")) {  
	            start();  
	        }  
	        if (cmd.equals("stop")) {  
	            stop();  
	        }  


	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}


	public void start(){
		if(thread == null){
			thread = new FeedThread();
			thread.start();
		}
		System.out.println("thread start......");
	}
	public void stop(){
		thread.running = false;
		thread = null;
		System.out.println("thread stop......");
	}
	public static class FeedThread extends Thread{
		public boolean  running = true;
		public void run(){
			MessageBroker  broker = 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.setMessageId(UUIDUtils.createUUID());
                msg.setTimestamp(System.currentTimeMillis());
                msg.setBody(tick);
                broker.routeMessageToService(msg, null);
                i++;
                try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}

}

?

  • flex.rar (20.1 KB)
  • 下载次数: 1
  • java.rar (4.3 MB)
  • 下载次数: 1
  • 相关文章
发表评论
用户名: 匿名