基于java-flex-blazeds的消息推送


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>


java代码中消息推送

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("服务端消息推送完成");
	}


Flex端消息订阅

//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没有使用心跳包之类来维护客户端状态,或是有此设置?求解!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值