在上一节已经配置好Blazeds 并实验成功,但是看下来需要手动配置的地方实在太多了。而且很多例子又不太全,搞到最后估计也搞不出来。其实如果你不做手动配置,直接代码写,就实在太方便了。
譬如你现在已经下载下来了Blazeds的安装包,并且启动了自带的Tomcat。你会发现在Tomcat文件下tomcatwebappsblazeds 有两个文件夹,META-INF 和?WEB-INF。
WEB-INF 下的
classes 文件夹是放置你的java的class文件的;
flex 文件夹下
sevices-confi.xml: 配置服务和目的地,也就是一些channel的定义
remoting-confi.xml?:?配置?RemotingService,也就是你要访问的后台的类的destination。
?proxy-config.xml?:配置?HTTPProxyService
?messaging-config.xml:配置MessageService,也就是你要发送消息给后台,一般是指Consummer 和Producer时用到的。
?
现在开始配置:
1.首先编写Java并放到tomcat下。
?
package Kenny; public class Test { public String word(String name) { return name; } }?编译为class文件,并放到tomcatwebappsblazedsWEB-INFclassesKenny 文件夹下。
?
2. 配置remoting-confi.xml?.
在service节点下添加一个节点:
?
<destination id="HelloWorldService"> <properties> <source>Kenny.Test</source> </properties> </destination>?3.创建Flex项目
?
?
private function resultHandler(e:ResultEvent):void { Alert.show("resultHandler"); } private function faultHandler(e:FaultEvent):void { Alert.show(e.message + "-" + e.headers); } protected function button1_clickHandler(event:MouseEvent):void { var remoteObj:RemoteObject = new RemoteObject(); var channelSet:ChannelSet = new ChannelSet(); var channel:AMFChannel = new AMFChannel("ddddddddd","http://localhost:8400/blazeds/messagebroker/amf"); channelSet.addChannel(channel); remoteObj.channelSet = channelSet; remoteObj.initialize(); remoteObj.destination = "HelloWorldService"; var operation:AbstractOperation = remoteObj["word"]; var token:AsyncToken = operation.send("aaa"); token.addResponder(new Responder(resultHandler,faultHandler)); }?其中http://localhost:8400/blazeds/messagebroker/amf 是 channel的endpoint地址,word是要调用的方法,aaa是传过去的参数。
?
你怎么会知道这个endpoint呢?你可以看下remoting-config.xml中定义了default-channels,也就是说如果你不为自己的destination 指定channel的话,就默认用这个channel。这个channel(my-amf)其实是在services-config.xml 中定义的channel的id。
这样就实验成功了。