第一步下载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 tickTime:Date;;
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的效果:是不是文字一直在增长?
恭喜。成功了。
分享到:
相关推荐
FLEX,BlazeDS,JAVA实现服务器端数据推送技术,下载后直接导到你的MYECLIPSE即可运行。先在IE内输入:http://localhost:8080/pushDemo/flex/pushDemo.html,点击“button”后,再另一个IE窗口内输入:...
Flex + blazeds + Java推送Demo 本例实现由Flex一端客户端发送消息, 然后由Java端在发布到所有订阅的其它Flex端. 里面有说明与源码, 还有一个直接放到Tomcat里面的直接发布的项目 小编使用工具 eclipse3.5 flex sdk...
基于Java_BlazeDS_Flex_服务器消息推的聊天室 了解BlazeDS推送机制,flex调用java web BlazeDS的过程 服务器推,摒弃ajax的轮询,减轻服务器压力!
elipse下配置的spring+flex+blazeds,进行消息推送,还加入了了quarz定时策略。希望对大家有帮助。
有关于flex推送技术的工程例子,使用blazeds的StreamingAMFChannel 通道实现推送功能。(使用前先启动http://localhost:8080/flex-blazeds/TickCacheServlet?cmd=start)
Flex和Java的简单通信,中间需要BlazeDS技术实现互联,可以实现信息的推送
BLAZEDS实现Java后台数据推送到Flex
压缩包内包含java后端、flex前端代码,配置文档,jar包
flex blazeds实现服务器向客户端推送数据 环境Eclipse Java EE IDE 服务apache-tomcat-7.0.23-windows-x86 blazeds采用的是blazeds4.0.0.14931 (附件中包含 apache-tomcat-7.0.23 + 源码 + 说明文档)
一个blazeds的eclipse工程的消息推送的例子,导入就可以运行例子
flex+blazeds+java后台消息推送(简单示例)[借鉴].pdf
Flex订阅JMS实现实时刷新 利用BlazeDS和JMS的消息机制实现数据的推送,从而实现页面的实时刷新(含详细的说明文档和源码)
flex采用blazeds实现服务器向客户端推数据案例
这是BlazeDS的java端工程,其中用的是flex-spring插件,并且实现了服务器主动推送消息给客户端,工程是用maven3来管理的。 另外工程中用到了Struts2 + spring + jpa的整合,其中Struts2与BlazeDS无关。
Flex+BlazeDS+Spring整合,Flex数据推送,消息订阅代码
这是订阅消息的flex端实现,要与我的资源中的blazeds一块应用。 代码肯定没有问题。 另外,客户端订阅的时候,可能会等时间长一些,可能有一分钟,请不要着急。
包含flex 的包和 spring 的包,用的时候自己拷进去 启动tomcat 访问:http://localhost:8080/monitoring/Test 启动测试线程 在访问:http://localhost:8080/monitoring/monitoring-debug/monitoring.html
以告警数据为例,告警数据通过mq、socket等方式进入系统,在java服务器通过监听来监听消息队列数据,接收后推送到flex各个客户端。blazeDS采用amf协议。该协议可以传输Object, Array, Date, XML。由于AMF采用二进制...