高水平的郑州网站建设,编写程序的步骤,购物网站开发流程,搜索引擎推广的方法有哪些Flex通过Blazeds利用Remoteservice与后台java消息推送 准备工作#xff1a;Myeclipse中先建立一个Web project工程#xff0c;然后导入Blazeds的文件#xff0c;再转换为Flex项目类型。前言#xff1a;Flex 通过开源的BlazeDS消息服务来支持订阅及发布消息。这个消息服务管… Flex通过Blazeds利用Remoteservice与后台java消息推送 准备工作Myeclipse中先建立一个Web project工程然后导入Blazeds的文件再转换为Flex项目类型。 前言Flex 通过开源的BlazeDS消息服务来支持订阅及发布消息。这个消息服务管理着Flex客户端可以订阅或发布的目标地址。Flex提供了 Producer和Consumer这两个组件让你用来向目标地址发送或订阅消息。如果要订阅消息你就使用Consumer类的 subscribe()方法。当有消息发送到你订阅了的目标地址时Consumer上就会触发message事件。 消息传递的目标地址是在Flex应用根下一个叫messaging-config.xml中配置的。一个目标地址配置的关键元素是在客户端和服务器建立交换数据的通道。使用BlazeDS消息传递的目标地址通常使用流通道或者轮询通道。 1使用流通道服务器响应会一直保持开放状态直到通道连接关闭这样可以让服务器持续向客户端发送变化的数据。 2如果数据没有立刻准备好长轮询就可以通过一个简单的时间间隔或者服务器等待时间来配置轮询通道。 修改两个配置文件services-config.xmlmessaging-config.xml services-config.xml加入以下代码 channel-definition idmy-streaming-amf classmx.messaging.channels.StreamingAMFChannel endpoint urlhttp://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf classflex.messaging.endpoints.StreamingAMFEndpoint/ properties idle-timeout-minutes0/idle-timeout-minutes max-streaming-clients10/max-streaming-clients server-to-client-heartbeat-millis5000 /server-to-client-heartbeat-millis user-agent-settings user-agent match-onMSIE kickstart-bytes2048 max-streaming-connections-per-session1/ user-agent match-onFirefox kickstart-bytes2048 max-streaming-connections-per-session1/ /user-agent-settings /properties /channel-definition messaging-config.xml加入以下代码 destination idmessage-data-feed properties server allow-subtopicstrue/allow-subtopics subtopic-separator./subtopic-separator /server /properties channels channel refmy-polling-amf / channel refmy-streaming-amf / /channels /destination 注这里的id就是目标地址也就是在flex代码中需要订阅消息的Consumer的属性destination所要设置的值以及发布消息java后台代码AsyncMessage的setDestination()所要传递的参数必须保证这三个地方名称一致。正是通过这一个目标地址信息发布者就会将信息发布到该目标地址然后所有已经设置订阅了该目标地址的flex就会触发MessageEvent.MESSAGE事件来获取发布的消息。 Flex前端代码 ?xml version1.0 encodingutf-8? s:Application xmlns:fxhttp://ns.adobe.com/mxml/2009 xmlns:slibrary://ns.adobe.com/flex/spark xmlns:mxlibrary://ns.adobe.com/flex/mx minWidth955 minHeight600 fx:Script ![CDATA[ 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 message -data-feed; //这里也要与后台的主题名称必须相同 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 mess:String event.message.body as String; demot.appendText(\n mess); } //退订该消息 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 idsubMessage destinationRemoteMessage resultsubMessage_resultHandler(event) /mx:RemoteObject /fx:Declarations s:TextArea x445 y42 width257 iddemot/ s:Button x445 y210 label订阅消息Remote idrbt clickrbt_clickHandler(event)/ s:Button x597 y207 label退订消息R idcbr clickcbr_clickHandler(event)/ /s:Application Java后台代码 package com.whut.daemon; 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.runningfalse; threadnull; } 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(clientIDclientID); while (running) { //异步消息 AsyncMessage msg new AsyncMessage(); msg.setDestination(tick-data-feed111); msg.setHeader(DSSubtopic, tick); msg.setClientId(clientID); msg.setMessageId(UUIDUtils.createUUID()); msg.setTimestamp(System.currentTimeMillis()); msg.setBody(hello); msgBroker.routeMessageToService(msg, null); try { Thread.sleep(500); } catch (InterruptedException e) {}}}}} remoting-config.xml加入以下代码 destination idRemoteMessage properties sourcecom.whut.daemon.DaemonMessage/source /properties /destination 转载于:https://blog.51cto.com/computerdragon/1143326