1、通过blazeds/lcds访问远程EJB,Pomer框架封装了blazeds/lcds的访问式,用户无须添加配置文件,只须在EJBDestinationRegister注册即可将Java对象发布成blazeds/lcds的远程对象(RemoteObject),进行远程访问。
2、新建EJB项目并增加一个无状态的SessionBean,在Myeclipse中如何新建和布署EJB不在本讲解范围,用户可参照MyEclipse相关帮助文档。用户自己建一个HelloEJB的SessionBean,并增加public String hello(String name)方法; 确认HelloEJB可以被远程访问。这里主要讲解Flex与EJB之间的调用,亲不懂Flex,没用过EJB,那。。。
3、注册EJB
在cn.org.pomer.flex.remoting.services.EJBDestinationRegister的构造函数增加EJB注册信息。
复制内容到剪贴板
代码:
public EJBDestinationRegister() {
super();
this.list = new ArrayList<EJBDestination>();
//add here
add("helloEJB", "HelloServicesImpl/remote");
}
4、新建Flex项目
代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private function helloClick():void{
helloSpring.hello("pomer");
}
private function helloEJBFault(e:FaultEvent):void{
Alert.show(e.fault.message.toString());
}
private function helloEJBResult(e:ResultEvent):void{
Alert.show(e.result as String);
}
]]>
</mx:Script>
<mx:RemoteObject endpoint="../messagebroker/amf"
id="helloEJB"
fault="helloEJBFault(event)"
result="helloEJBResult(event)"
destination="helloEJB" showBusyCursor="true"/>
<mx:Button label="hello" click="helloClick();"/>
</mx:Application>
5、布署运行
- 启动Tomcat,日志出现如下标记,布署成功
- 右击HelloJava.mxml->Run as->Flex application
6、原理分析
查看WEB-INF\flex\services- config.xml,EJBRemotingDestinationBootstrapService类从 EJBDestinationRegister类得到所有要远程访问的Ejb,自动加入服务中。EJBFactory根据 EJBDestinationRegister提供的内容自动创建远程对象。