api说的继承这个就可以。基本是官方的例子 package RuntimeConfig; import flex.messaging.MessageBroker; import flex.messaging.MessageDestination; import flex.messaging.config.ConfigMap; import flex.messaging.services.AbstractBootstrapService; import flex.messaging.services.MessageService; public class RuntimeConfigMessage extends AbstractBootstrapService { @Override public void initialize(String arg0, ConfigMap arg1) { System.out.println("serviceId"+arg0);//runtime-message //service-config.xml //<service class="RuntimeConfig.RuntimeConfigMessage" id="runtime-message" /> String service="message-service"; MessageBroker mb=getMessageBroker(); MessageService ms=(MessageService) mb.getService(service); ms.registerAdapter("testAdapter", "adapter.MessageAdapter"); ms.registerAdapter("test2", "adapter.MessageAdapter2"); ms.setDefaultAdapter("testAdapter");//设置缺省的apdapter,否则registerAdapter没用 MessageDestination md=(MessageDestination) ms.createDestination("testRuntimeConfig"); //md.setAdapter(md.createAdapter("test2"));这样会把上边ms.setDefaultAdapter替换掉 //会用MessageAdapter2来处理消息,所以处理消息时,会用MessageAdapter2来处理 if(ms.isStarted()){ md.start(); } } @Override public void start() { } @Override public void stop() { } }
package adapter; import flex.messaging.messages.Message; import flex.messaging.services.messaging.adapters.ActionScriptAdapter; import flex.messaging.services.remoting.adapters.JavaAdapter; public class MessageAdapter extends ActionScriptAdapter { public MessageAdapter(){ super(); System.out.println("实例化次数");//只有一次 } @Override public Object invoke(Message message) { super.invoke(message); System.out.println("invoke message");//一定要setDefaultAdapter return null; } }
package adapter; import flex.messaging.messages.Message; import flex.messaging.services.messaging.adapters.ActionScriptAdapter; public class MessageAdapter2 extends ActionScriptAdapter { public MessageAdapter2() { super(); System.out.println("instance");//只有一次 } @Override public Object invoke(Message message) { super.invoke(message); System.out.println("invoke 2 Message"); return null; } }
<services-config> <services> <service-include file-path="remoting-config.xml" /> <service-include file-path="proxy-config.xml" /> <service-include file-path="messaging-config.xml" /> //注意下边这个 <service class="RuntimeConfig.RuntimeConfigMessage" id="runtime-message" /> </services>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Producer id="producer" channelSet="{sets}"> <mx:fault> <![CDATA[ Alert.show(event.message+"producer"); ]]> </mx:fault> <mx:destination>testRuntimeConfig</mx:destination> </mx:Producer> <mx:Consumer id="consumer" channelSet="{sets}"> <mx:fault> <![CDATA[ Alert.show("consumer"+event.message); ]]> </mx:fault> <mx:message> <![CDATA[ ta.text+=ta.text+"receive"; ]]> </mx:message> <mx:destination>testRuntimeConfig</mx:destination> </mx:Consumer> <mx:ChannelSet id="sets"> <mx:AMFChannel uri="messagebroker/amfpolling"> </mx:AMFChannel> <mx:StreamingAMFChannel uri="messagebroker/streamingamf"> </mx:StreamingAMFChannel> </mx:ChannelSet> <mx:Script> <![CDATA[ import mx.messaging.messages.AsyncMessage; import mx.controls.Alert; private function init():void{ consumer.subscribe(); } ]]> </mx:Script> <mx:VBox> <mx:TextArea id="ta"> </mx:TextArea> <mx:Button> <mx:click> <![CDATA[ var message:AsyncMessage=new AsyncMessage; message.body=ta.text; producer.send(message); ]]> </mx:click> </mx:Button> </mx:VBox> </mx:Application>
package RuntimeConfig; import flex.messaging.MessageBroker; import flex.messaging.MessageDestination; import flex.messaging.config.ConfigMap; import flex.messaging.services.AbstractBootstrapService; import flex.messaging.services.MessageService; public class RuntimeConfigMessage extends AbstractBootstrapService { @Override public void initialize(String arg0, ConfigMap arg1) { System.out.println("serviceId"+arg0);//runtime-message //service-config.xml // String service="message-service"; MessageBroker mb=getMessageBroker(); MessageService ms=(MessageService) mb.getService(service); ms.registerAdapter("testAdapter", "adapter.MessageAdapter"); ms.registerAdapter("test2", "adapter.MessageAdapter2"); ms.setDefaultAdapter("testAdapter");//设置缺省的apdapter,否则registerAdapter没用 MessageDestination md=(MessageDestination) ms.createDestination("testRuntimeConfig"); //md.setAdapter(md.createAdapter("test2"));这样会把上边ms.setDefaultAdapter替换掉 //会用MessageAdapter2来处理消息,所以处理消息时,会用MessageAdapter2来处理 if(ms.isStarted()){ md.start(); } } @Override public void start() { } @Override public void stop() { } }