`

Flex与java Server Push数据

阅读更多

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

1.Flex 工程

<?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();
				}
			}
		}
		
		
	}

}

 

分享到:
评论

相关推荐

    使用BlazeDS实现flex与java 整合

    3. **Real-time Data Push**:BlazeDS支持Server-Sent Events和Long Polling,实现数据的实时推送,使Flex客户端能够实时更新视图。 4. **Security**:在整合过程中,确保数据安全是非常重要的。可以通过配置...

    Flex与Java的消息推送

    Flex与Java的消息推送技术主要涉及的是前端Flex应用与后端Java服务器之间的实时数据交互。在Web应用程序中,传统的HTTP协议是基于请求-响应模型的,客户端(浏览器)发起请求,服务器响应,然后连接关闭。然而,对于...

    ServerPush

    以告警数据为例,告警数据通过mq、socket等方式进入系统,在java服务器通过监听来监听消息队列数据,接收后推送到flex各个客户端。blazeDS采用amf协议。该协议可以传输Object, Array, Date, XML。由于AMF采用二进制...

    blazeDs解决flex客户端与Server的远程通讯

    在Flex客户端与Server的远程通讯中,BlazeDS起到了关键作用。Flex作为RIA(Rich Internet Applications)开发平台,其强大的图形用户界面和富交互性需要与服务器进行频繁且高效的数据交换。BlazeDS通过以下方式实现...

    Flex+BlazeDS+java通信详细笔记和源代码

    `PushTest6`可能包含了Flex客户端的源代码,用于订阅Java服务器发布的数据,以及服务器端的Java代码,负责生成并推送数据。 在实际项目中,Flex+BlazeDS+Java通信的应用场景包括股票报价、实时聊天、在线协作工具等...

    flex dataservice

    #### 消息传递与数据推送 除了远程调用之外,数据服务还支持消息传递和数据推送。这意味着服务器可以将数据主动推送给客户端,而不仅仅是等待客户端的请求。这对于需要实时更新的应用场景非常重要。 #### 媒体流 ...

    flex 截图并上传到服务器中

    然后,Flex通过HTTPService或WebService组件与Java后台进行通信,将图像数据发送到服务器。HTTPService通常用于RESTful API,而WebService更适合SOAP协议。在发送请求时,通常会将图像数据作为二进制流附加到POST...

    Flex的LCDS服务器架构

    此外,LCDS还提供了服务端推(server-push)API,允许服务端主动将消息传递到客户端,这在实时性要求高的应用场景中尤其有用。 总的来说,Flex的LCDS服务器架构提供了一个强大的工具集,使得Flex和Java应用之间的...

    flex实现多文件上传

    在Flex中实现多文件上传,通常涉及到ActionScript编程、组件使用以及与服务器端的交互。下面将详细介绍如何使用Flex来实现这个功能。 一、Flex中的文件选择组件 在Flex中,我们可以使用`FileReference`类来处理文件...

    blazeds java源码部分

    BlazeDS is the server-based Java remoting and web messaging technology that enables developers to easily connect to back-end distributed data and push data in real-time to Adobe® Flex® and Adobe AIR...

    一些前端面试题.pdf

    **MVVM**(Model-View-ViewModel):类似于MVC,但View与ViewModel之间通过数据绑定来通信。 **MVP**(Model-View-Presenter):视图负责显示数据,模型负责业务逻辑,Presenter作为两者之间的中介。 ### 模块化与...

Global site tag (gtag.js) - Google Analytics