java-web端配置
在service-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="realtime-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>
public static void send(ChangeDataVO cd) {
MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
String clientID = UUIDUtils.createUUID();
AsyncMessage msg = new AsyncMessage();
msg.setDestination("realtime-data-feed");
msg.setHeader("DSSubtopic", "realdata");
msg.setClientId(clientID);
msg.setMessageId(UUIDUtils.createUUID());
msg.setTimestamp(System.currentTimeMillis());
msg.setBody(cd);
msgBroker.routeMessageToService(msg, null);
Log.log("服务端消息推送完成");
}
//3.接收推送消息
var client:Consumer = new Consumer();
client.destination="realtime-data-feed";
client.subtopic="realdata";
client.channelSet=new ChannelSet(["my-streaming-amf"]);
client.addEventListener(MessageEvent.MESSAGE,messageHandler);
client.subscribe();
消息处理
private function messageHandler(event:MessageEvent):void {
var data:ChangeDataVO=event.message.body as ChangeDataVO;
var item:Item=itemCache[data.cosgiid] as Item;
if(item!=null){
item.statuschange(data);
}
}
测试中发现若flex客户端较长时间不操作(有网友测试过大概一个小时),推送将失败,必须重新进行订阅。处理方法之一:
//判断消息订阅是否失效,如果失效,则重新订阅
timer=new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,function():void{
if(!client.subscribed){
client.subscribe();
}
});
timer.start();
或是配置一个client的状态监听器(没去研究过。。。)?
对于超时需重新注册的问题,细想,服务端持有客户端连接信息,据此推送消息,但如果客户端浏览器突然关闭或其他异常退出,服务端是不知道客户端是否在线,仍然推送消息,必将导致服务端资源浪费,所以设定一个服务时限,超出此时限客户端需重新订阅。据此开了blazeds没有使用心跳包之类来维护客户端状态,或是有此设置?求解!