重要提示:WEB应用服务器要用tomcat6,blazeds和tomcat7有兼容性问题。
1、先创建一个web工程整合Flex,如果不会参考:
http://huanggua12353719.iteye.com/blog/2295720
2、我的工程名为test,在services-config.xml的channels节点内加入
<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>
3、在messaging-config.xml的service节点内加入
<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>
4、在java文件目录src/com包下创建Tick.java
package com;
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;
}
}
5、在java文件目录src/com包下创建DaemonMessage.java
package com;
import java.math.BigDecimal;
import java.util.Date;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;
public class DaemonMessage
{
private static FeedThread thread;
//开始传递消息
public void startSendMessage(String flags)
{
if (thread == null)
{
thread = new FeedThread();
thread.start();
}
}
//停止消息发布
public void stopSendMessage(String flags)
{
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();
System.out.println("clientID="+clientID);
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(1000);
} catch (InterruptedException e) {}}}
}
}
6、在flex文件目录flex_src下创建Tick.as
package
{
[RemoteClass(alias="com.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;
}
}
7、在flex主文件test.mxml的内容改成
<?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">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.messaging.Channel;
import mx.messaging.ChannelSet;
import mx.messaging.Consumer;
import mx.messaging.events.MessageEvent;
import mx.rpc.events.ResultEvent;
private var myConsumer:Consumer = new Consumer();
//直接利用Remote来进行远程的消息调用
//开始订阅消息
protected function rbt_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
//利用远程调用来触发开始工作
subMessage.startSendMessage("start");
//准备开始订阅消息
myConsumer.destination = "tick-data-feed";
//这里也要与后台的主题名称必须相同 www.it165.net
myConsumer.subtopic = "tick";
myConsumer.channelSet = new ChannelSet(["my-streaming-amf"]);
myConsumer.addEventListener(MessageEvent.MESSAGE, remote_messageHandler);
myConsumer.subscribe();
}
//获取订阅的消息,以文本来显示显示
private function remote_messageHandler(event:MessageEvent):void
{
var tick:Tick = event.message.body as Tick;
demot.text = tick.seqno;
}
//退订该消息
protected function cbr_clickHandler(event:MouseEvent):void
{
subMessage.stopSendMessage("stop");
myConsumer.unsubscribe(false);
}
protected function subMessage_resultHandler(event:ResultEvent):void
{}
]]>
</fx:Script>
<fx:Declarations>
<!--用来启动消息发布 -->
<mx:RemoteObject id="subMessage" destination="RemoteMessage"
result="subMessage_resultHandler(event)"> </mx:RemoteObject>
</fx:Declarations>
<s:TextArea x="445" y="42" width="257" id="demot"/>
<s:Button x="445" y="210" label="订阅消息Remote" id="rbt" click="rbt_clickHandler(event)"/>
<s:Button x="597" y="207" label="退订消息R" id="cbr" click="cbr_clickHandler(event)"/>
</s:Application>
8、在remoting-config.xml文件service节点中加入
<destination id="RemoteMessage">
<properties>
<source>com.DaemonMessage</source>
</properties>
</destination>
9、配置完之后的目录如下:
10、如果bin-debug目录下没有生成相应的html文件。右键工程-》属性-》Flex编译器-》生成HTML包装器文件(H),去掉勾选,点应用,再勾选上就应用 。testmxml相对应的html文件就出现了。
11、启动工程,访问http://localhost:8080/test/test/test.html
如果出现404错误,打开test.mxml随便编辑(不能修改代码,可以在末尾添加一个空格,删除一个空格),目的是让工程重新生成对应的html文件,保存。再访问。如果成功出现
12、点击订阅消息Remote空白框里会出现1、2、3、4 ...不停的在变,点击退订消息R,数字不再变,部署成功。
13、如果这样还不行,可以留言,大家讨论一下。
相关推荐
Flex + blazeds + Java推送Demo 本例实现由Flex一端客户端发送消息, 然后由Java端在发布到所有订阅的其它Flex端. 里面有说明与源码, 还有一个直接放到Tomcat里面的直接发布的项目 小编使用工具 eclipse3.5 flex sdk...
FLEX,BlazeDS,JAVA实现服务器端数据推送技术,下载后直接导到你的MYECLIPSE即可运行。先在IE内输入:http://localhost:8080/pushDemo/flex/pushDemo.html,点击“button”后,再另一个IE窗口内输入:...
基于Java_BlazeDS_Flex_服务器消息推的聊天室 了解BlazeDS推送机制,flex调用java web BlazeDS的过程 服务器推,摒弃ajax的轮询,减轻服务器压力!
elipse下配置的spring+flex+blazeds,进行消息推送,还加入了了quarz定时策略。希望对大家有帮助。
BLAZEDS实现Java后台数据推送到Flex
有关于flex推送技术的工程例子,使用blazeds的StreamingAMFChannel 通道实现推送功能。(使用前先启动http://localhost:8080/flex-blazeds/TickCacheServlet?cmd=start)
Flex和Java的简单通信,中间需要BlazeDS技术实现互联,可以实现信息的推送
flex blazeds实现服务器向客户端推送数据 环境Eclipse Java EE IDE 服务apache-tomcat-7.0.23-windows-x86 blazeds采用的是blazeds4.0.0.14931 (附件中包含 apache-tomcat-7.0.23 + 源码 + 说明文档)
压缩包内包含java后端、flex前端代码,配置文档,jar包
一个blazeds的eclipse工程的消息推送的例子,导入就可以运行例子
Flex订阅JMS实现实时刷新 利用BlazeDS和JMS的消息机制实现数据的推送,从而实现页面的实时刷新(含详细的说明文档和源码)
flex+blazeds+java后台消息推送(简单示例)[借鉴].pdf
flex采用blazeds实现服务器向客户端推数据案例
这是BlazeDS的java端工程,其中用的是flex-spring插件,并且实现了服务器主动推送消息给客户端,工程是用maven3来管理的。 另外工程中用到了Struts2 + spring + jpa的整合,其中Struts2与BlazeDS无关。
这是订阅消息的flex端实现,要与我的资源中的blazeds一块应用。 代码肯定没有问题。 另外,客户端订阅的时候,可能会等时间长一些,可能有一分钟,请不要着急。
Flex+BlazeDS+Spring整合,Flex数据推送,消息订阅代码
包含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采用二进制...