找到这个资料Calling Java methods from Flex using mx:RemoteObject and Red5,立即下载下来,运行了一把,成功。现在我们从头配置一个类似的项目。
1.在Eclipse中新建web项目"emeetings",参照第二篇(建立新的Red5应用)中的方法,还是再讲一遍吧:
(1).在Eclipse下新建一个web应用,起名叫emeetings。
(2).把在http://dl.fancycode.com/red5/0.6.3/war/ 下载的是java5目录下的三个war(ROOT.war、admin.war、echo.war)中的ROOT.war解压出来,先拷贝所有解压文件到Eclipse的emeetings项目的WebRoot目录下,然后将解压文件夹\WEB-INF\classes下的*.properties、*.xml等配置文件拷贝到项目的src目录下,最后将解压文件夹\WEB-INF\classes下的所有class文件打包成red5-0.6.3-java5.jar(不会打的在附件下载)放到Eclipse的emeetings项目的WebRoot\WEB-INF\lib目录。
2.编写上下文类文件"Application.java":
- package net.xicp.cnfire.red5.remoteobject;
- import org.red5.server.adapter.ApplicationAdapter;
- public class Application extends ApplicationAdapter {
- public String getMsg() {
- return "this msg from server";
- }
- }
3.配置该项目的spring配置文件(remoteobject-web.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="web.context.emeetings" class="org.red5.server.Context">
- <property name="scopeResolver" ref="red5.scopeResolver" />
- <property name="clientRegistry" ref="global.clientRegistry" />
- <property name="serviceInvoker" ref="global.serviceInvoker" />
- <property name="mappingStrategy" ref="global.mappingStrategy" />
- </bean>
- <bean id="web.scope.emeetings" class="org.red5.server.WebScope" init-method="register">
- <property name="server" ref="red5.server" />
- <property name="parent" ref="global.scope" />
- <property name="context" ref="web.context.emeetings" />
- <property name="handler" ref="web.handler.emeetings" />
- <property name="contextPath" value="/emeetings" />
- <property name="virtualHosts" value="*,localhost, localhost:8080, 127.0.0.1:8080" />
- </bean>
- <bean id="web.handler.emeetings" class="net.xicp.cnfire.red5.remoteobject.Application"/>
- <!-- enable support for mx:RemoteObject requests -->
- <bean id="flexMessaging.service" class="org.red5.server.net.remoting.FlexMessagingService">
- <property name="serviceInvoker" ref="global.serviceInvoker" />
- <!-- add an entry for all possible "destination" attributes of your
- mx:RemoteObject sources -->
- <property name="endpoints">
- <map>
- <entry key="Red5RemoteObject">
- <!-- object that handles requests sent to this endpoint -->
- <ref bean="web.handler.emeetings" />
- </entry>
- </map>
- </property>
- </bean>
- </beans>
需要注意的地方(非常重要,谁能告诉我为什么):
(1) Application.java的包名最后一级net.xicp.cnfire.red5.remoteobject(红色部分)不能和项目名"emeetings"相同。
(2) remoteobject-web.xml(红色部分)必须和net.xicp.cnfire.red5.remoteobject(红色部分)对应相同。
4.编写客户端Flex代码测试RemoteObject(example.mxml):
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.controls.Alert;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.remoting.mxml.RemoteObject;
- private var myHandler: RemoteObject;
- public function init(): void {
- myHandler = new RemoteObject("Red5RemoteObject");
- myHandler.showBusyCursor = true;
- //这里要写上端口8080,因为我的80端口被别的程序占用了
- myHandler.endpoint = "http://localhost:8080/emeetings/gateway";
- myHandler.addEventListener("fault", onError);
- myHandler.getMsg.addEventListener("result", onEchoDetails);
- myHandler.getMsg();
- }
- private function onError(event: FaultEvent): void {
- Alert.show("Error: " + event.fault.faultString);
- }
- protected function onEchoDetails(event: ResultEvent): void {
- Alert.show(event.result.toString());
- }
- ]]>
- </mx:Script>
- </mx:Application>
5.在浏览器地址栏输入:http://localhost:8080/emeetings/example.html 正确的话在弹出对话框里应该是“this msg from server” 。如果为“Error: Send failed”则可能是端口错误,查看8080端口是否被占用。或者为 remoteobject-web.xml(红色部分)与项目名称"emeetings"相同了;如果为“Error: Endpoint Red5RemoteObject doesn't exist.”则可能是remoteobject-web.xml(红色部分)和net.xicp.cnfire.red5.remoteobject(红色部分)与项目名"emeetings"相同。注意看上面一步的需要注意的地方(非常重要)
下面再演示配置多个RemoteObject,增加一个RemoteObject类(RemoteObjectDemo.java):
- package net.xicp.cnfire.red5.remoteobject;
- public class RemoteObjectDemo {
- public String getMsg() {
- return "这是第二个RemoteObject调用,通过RemoteObject调用普通类的例子!";
- }
- }
修改remoteobject-web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="web.context.emeetings" class="org.red5.server.Context">
- <property name="scopeResolver" ref="red5.scopeResolver" />
- <property name="clientRegistry" ref="global.clientRegistry" />
- <property name="serviceInvoker" ref="global.serviceInvoker" />
- <property name="mappingStrategy" ref="global.mappingStrategy" />
- </bean>
- <bean id="web.scope.emeetings" class="org.red5.server.WebScope" init-method="register">
- <property name="server" ref="red5.server" />
- <property name="parent" ref="global.scope" />
- <property name="context" ref="web.context.emeetings" />
- <property name="handler" ref="web.handler.emeetings" />
- <property name="contextPath" value="/emeetings" />
- <property name="virtualHosts" value="*,localhost, localhost:8080, 127.0.0.1:8080" />
- </bean>
- <bean id="web.handler.emeetings" class="net.xicp.cnfire.red5.remoteobject.Application"/>
- <bean id="my.handler.remoteobject" class="net.xicp.cnfire.red5.remoteobject.RemoteObjectDemo"/>
- <!-- enable support for mx:RemoteObject requests -->
- <bean id="flexMessaging.service" class="org.red5.server.net.remoting.FlexMessagingService">
- <property name="serviceInvoker" ref="global.serviceInvoker" />
- <!-- add an entry for all possible "destination" attributes of your
- mx:RemoteObject sources -->
- <property name="endpoints">
- <map>
- <entry key="Red5RemoteObject">
- <!-- object that handles requests sent to this endpoint -->
- <ref bean="web.handler.emeetings" />
- </entry>
- <entry key="RODemo">
- <!-- object that handles requests sent to this endpoint -->
- <ref bean="my.handler.remoteobject" />
- </entry>
- </map>
- </property>
- </bean>
- </beans>
可以看到增加了一个bean:<bean id="my.handler.remoteobject" class="net.xicp.cnfire.red5.remoteobject.RemoteObjectDemo"/>
和一个entry:
<entry key="RODemo">
<!-- object that handles requests sent to this endpoint -->
<ref bean="my.handler.remoteobject" />
</entry>
最后修改Flex代码(example.mxml):
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.controls.Alert;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.remoting.mxml.RemoteObject;
- private var myHandler: RemoteObject;
- private var myHandler1:RemoteObject;
- public function init(): void {
- myHandler = new RemoteObject("Red5RemoteObject");
- myHandler.showBusyCursor = true;
- myHandler1 = new RemoteObject("RODemo");
- myHandler1.showBusyCursor = true;
- //这里要写上端口8080,因为我的80端口被别的程序占用了
- myHandler.endpoint = "http://localhost:8080/emeetings/gateway";
- myHandler.addEventListener("fault", onError);
- myHandler.getMsg.addEventListener("result", onEchoDetails);
- myHandler.getMsg();
- myHandler1.endpoint = "http://localhost:8080/emeetings/gateway";
- myHandler1.addEventListener("fault", onError);
- myHandler1.getMsg.addEventListener("result", onEchoDetails);
- myHandler1.getMsg();
- }
- private function onError(event: FaultEvent): void {
- Alert.show("Error: " + event.fault.faultString);
- }
- protected function onEchoDetails(event: ResultEvent): void {
- Alert.show(event.result.toString());
- }
- ]]>
- </mx:Script>
- </mx:Application>
打开浏览器在地址栏输入http://localhost:8080/emeetings/example.html 查看弹出对话框,可以看到两个弹出对话框,内容分别为"这是第二个RemoteObject调用,通过RemoteObject调用普通类的例子!"和"this msg from server"。
至此,原来基于LCDS的Flex应用都可以平滑的过渡到Red5了(因为我仅仅用到了LCDS的Remote服务--RemoteObject,其他的DataManagement服务、Message服务都还没用到~~,也不知道RED5支持不支持这些,也查不到相关信息,知道的告诉我一下啊~~),后面将介绍从FMS过渡到Red5的一些内容,首当其冲的就是RemoteSharedObject了。