flex采用blazeds实现服务器向(特定标识的)客户端推数据(基于consumer模式)

本文主要是介绍flex采用blazeds实现服务器向(特定标识的)客户端推数据(基于consumer模式),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:
有很多类似股票、外汇、期货等实时行情这种应用,客户端需要显示行情牌价等信息。
目前的作法是:客户端定时向服务器请求,无论数据是否有更新,都把数据发到客户端。
我们这里讲的一种技术不同以上这个做法,我们是采用服务器向客户端推的这种方式,该方式的好处不言自明。blazeds中有一个名为:StreamingAMFChannel 的通道,我们就是采用它来实现向客户端推这个功能。
Tick.java :

package test;

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;
}


}

FeedThread .java :

public static class FeedThread extends Thread {
private String uid;

public void setUid(String uid) {
this.uid = uid;
}

public FeedThread(String uid){
this.uid = uid;
}
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+"--"+uid);

AsyncMessage msg = new AsyncMessage();
msg.setDestination("tick-data-feed");
msg.setHeader("DSSubtopic", "tick"+uid);
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) {
}

}
}
}

UserService.java :

package sws.service;

import module.User;

public interface UserService {
public void Test(String uid);
public void stop(String uid);
}

UserServiceImpl.java :

package sws.service;

import java.util.HashMap;
import java.util.Map;

import module.User;

import org.springframework.beans.factory.annotation.Autowired;

import sws.dao.UserDao;
import test.TickServlet.FeedThread;


public class UserServiceImpl implements UserService {
private static Map<String,Thread> map = new HashMap<String,Thread>();

public void Test(String uid) {
Boolean b = map.containsKey(uid);
if(!b){
FeedThread thread = new FeedThread(uid);
thread.start();
map.put(uid, thread);
}
}

public void stop(String uid) {
FeedThread thread = (FeedThread)map.get(uid);
thread.running = false;
}

}

下一步加入flex配置文件。
flex的配置文件默认有四个,文件目录在WebContent\WEB-INF\flex目录下。
messaging-config.xml
proxy-config.xml
remoting-config.xml
services-config.xml
其实,这中间用的是一个,就是services-config.xml,只是在services-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>


Tick.as文件内容如下:
//Tick.as
package
{
[RemoteClass(alias="test.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;
}

}

再在main.mxml文件中,加入如下代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html" height="378" width="426">

<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.messaging.Consumer;
import mx.messaging.Channel;
import mx.messaging.ChannelSet;
import mx.messaging.events.MessageEvent;

[Bindable]
public var tick:Tick;
public function postUrl():void {
//
// httpservice.url = "http://localhost:8080/test/servlet/TickServlet?cmd=start&uid="+telId.text;
// httpservice.send();
// httpservice.addEventListener(ResultEvent.RESULT,resultHandler);
// httpservice.addEventListener(FaultEvent.FAULT,faultHandler);
user.Test(telId.text);
}
public function resultHandler(event:ResultEvent):void{
submsg();
}
public function faultHandler(event:FaultEvent):void{
trace(event.fault);
}
public function submsg():void
{

var consumer:Consumer = new Consumer();
consumer.destination = "tick-data-feed";
consumer.subtopic = "tick"+telId.text;
consumer.channelSet = new ChannelSet(["my-streaming-amf"]);
consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);
consumer.subscribe();
}

private function messageHandler(event:MessageEvent):void
{

tick = event.message.body as Tick;

txtTick.text = tick.seqno;
}
private function stop():void {
user.stop(telId.text);
}
]]>
</mx:Script>

//RemoteObject 的方式远程调用java类 userService在rremoting-config.xml中配置
<mx:RemoteObject id="user" destination="userService" source="userService" result="resultHandler(event)"/>
<mx:HTTPService id="httpservice" />
<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:Button x="132" y="41" label="start" click="postUrl(); "/>
<mx:TextInput id="telId" x="37" y="83"/>//标识用户
<mx:Button label="stop" click="stop();" x="216" y="41"/>
</mx:Panel>
</mx:Application>


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aini281032/archive/2010/03/12/5367795.aspx

这篇关于flex采用blazeds实现服务器向(特定标识的)客户端推数据(基于consumer模式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/456154

相关文章

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

详解如何在SpringBoot控制器中处理用户数据

《详解如何在SpringBoot控制器中处理用户数据》在SpringBoot应用开发中,控制器(Controller)扮演着至关重要的角色,它负责接收用户请求、处理数据并返回响应,本文将深入浅出地讲解... 目录一、获取请求参数1.1 获取查询参数1.2 获取路径参数二、处理表单提交2.1 处理表单数据三、