转自http://www.sloppy.cn/blog/article/247.htm
网上已有例子,只转载下,按照网上配置会msgBroker为nullpoint的错,需要在web.xml中增加 MessageBroker Servlet的配置,在这记录。
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>pmportalMessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pmportalMessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
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;
}
<mxanel x="524" y="47" width="362" height="302" layout="absolute" title="Watch Tick">
<mxabel x="72" y="43" text="Label" id="txtTick"/>
<mx:Button x="132" y="41" label="Button" click="submsg(); "/>
</mxanel>
第七步:运行Flex:http://localhost:8080/blazeds/HelloWorld-debug/HelloWorld.html?debug=true
点击Button
迂曲运行Servlet:http://localhost:8080/blazeds/TickCacheServlet?cmd=start
再看看Flex的效果:是不是文字一直在增长?
恭喜。成功了。
网上已有例子,只转载下,按照网上配置会msgBroker为nullpoint的错,需要在web.xml中增加 MessageBroker Servlet的配置,在这记录。
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>pmportalMessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pmportalMessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
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;
}
<mxanel x="524" y="47" width="362" height="302" layout="absolute" title="Watch Tick">
<mxabel x="72" y="43" text="Label" id="txtTick"/>
<mx:Button x="132" y="41" label="Button" click="submsg(); "/>
</mxanel>
第七步:运行Flex:http://localhost:8080/blazeds/HelloWorld-debug/HelloWorld.html?debug=true
点击Button
迂曲运行Servlet:http://localhost:8080/blazeds/TickCacheServlet?cmd=start
再看看Flex的效果:是不是文字一直在增长?
恭喜。成功了。
相关推荐
在Blazeds中,向客户端推送数据主要涉及到以下几个关键组件和配置: 1. **通道定义(Channel Definition)**:在`services-config.xml`文件中,定义了数据传输的通道。例如,`StreamingAMFChannel`是一种支持流式...
Flex + blazeds + Java推送Demo 本例实现由Flex一端客户端发送消息, 然后由Java端在发布到所有订阅的其它Flex端. 里面有说明与源码, 还有一个直接放到Tomcat里面的直接发布的项目 小编使用工具 eclipse3.5 flex sdk...
本文主要探讨如何利用Flex和BlazDS实现服务器向客户端的数据推送功能,尤其适用于实时性强的应用,如股票、外汇和期货市场的实时行情显示。 BlazDS是Adobe提供的一个开源服务器端解决方案,它允许Flex客户端与Java...
本文主要介绍如何使用Flex和BlazDS实现服务器向客户端的实时数据推送,这一技术常用于股票、外汇、期货等实时行情的应用。传统的做法是客户端定时向服务器请求数据,而BlazDS提供的StreamingAMFChannel通道则支持...
Flex BlazeDs推数据生成lineChart实例是一个典型的前端与后端实时通信的应用场景,它结合了Adobe Flex技术用于前端用户界面的构建,BlazeDS作为数据推送服务,以及使用lineChart组件来展示动态更新的数据。...
BlazeDS支持服务器向客户端推送数据的机制,这在实时应用中非常关键,如股票交易、在线聊天或实时数据更新。通过Long Polling、HTTP Streaming或WebSocket等技术实现,可以实现低延迟的数据同步。 4. **Remoting**...
flex blazeds实现服务器向客户端推送数据 环境Eclipse Java EE IDE 服务apache-tomcat-7.0.23-windows-x86 blazeds采用的是blazeds4.0.0.14931 (附件中包含 apache-tomcat-7.0.23 + 源码 + 说明文档)
标题中的“flex+spring+blazeds消息推送”是指在开发富互联网应用程序(RIA)时,采用Adobe Flex作为前端UI框架,Spring作为后端服务层框架,BlazeDS作为数据通信中间件,实现服务器到客户端的消息实时推送功能。...
3. **Live Data Push**:BlazeDS支持服务器向客户端推送数据,而不仅仅是响应客户端请求。这种推送技术(如Long Polling、Comet等)使得实时应用(如股票交易、聊天室等)成为可能,无需客户端不断轮询服务器。 4. ...
6. **实时推送(Push)**:BlazeDS支持基于HTTP的Long Polling和Streaming,实现服务器向客户端的实时数据推送。这种特性使得金融、聊天室等需要实时更新的应用场景得以实现。 7. **安全性和性能优化**:BlazeDS...
9. **性能优化**:BlazDS支持数据推送,当后端数据发生变化时,可以通过Push Channel主动将更新推送到Flex客户端,提高应用的响应速度和用户体验。 10. **安全性**:在实际应用中,需要考虑数据安全问题。可以通过...
在Flex和Java集成中,通过BlazeDS支持WebSocket,可以实现真正的实时推送,即服务器可以直接将数据推送到客户端,无需客户端轮询。 在实际应用中,开发人员需要在Java服务器端创建处理推送逻辑的服务,并在Flex...
这个技术允许双向通信,即服务器可以主动向客户端推送数据,而不仅仅是响应客户端的请求。这种功能对于实时性要求高的应用,如股票交易、聊天室或者实时数据显示等场景非常有用。 在"BlazeDS实现java后台消息推送...
在本场景中,我们将探讨如何利用BlazeDS和Flex实现服务器消息推送。 首先,让我们了解BlazeDS的基本概念。BlazeDS是Adobe Flex与Java后端进行数据交互的一个中间件,它支持AMF(Action Message Format)协议,能够...
它支持EJB、Spring、Hibernate等Java企业框架,方便整合现有的企业服务,同时,Blazeds还支持数据推送,使得服务器可以主动将数据推送到客户端,提升了用户体验。 5. **源码分析**:在"FlexTest"这个压缩包中,可能...
BlazeDS支持实时双向通信,包括HTTP长轮询、HTTP流和WebSocket等技术,使得服务器能够主动向客户端推送数据,而非传统的请求-响应模式。 **Flex简介** Flex是基于MXML和ActionScript的开发框架,用于构建具有丰富...
BlazeDS是Adobe提供的一个免费服务器端技术,它为Flex客户端提供了与Java应用程序交互的能力,包括实时消息传递、Remoting和数据推送等功能。Spring是一个广泛使用的Java企业级应用开发框架,它简化了Java应用程序的...
相反,我们可以利用BlazeDS中的StreamingAMFChannel技术,实现服务器向客户端推送数据,提高实时性和效率。 BlazeDS是一个开源的Java服务器端组件,能够为Flex和Ajax应用提供数据集成。它支持多种通信协议,包括...