- 開始之前
在學(xué)習(xí)本文內(nèi)容之前,你至少要能用JAVA、EJB和Flex寫出Helloword這樣簡單的應(yīng)用程序,并且下載安裝了Pomer和運(yùn)行過PomerUserIndex.mxml,但不一定要了解blazeds和lcds。如果閱讀過Pomer簡介、Pomer下載與安裝和Pomer架構(gòu)介紹 有助于學(xué)習(xí)本文。
- 概述
本文主要演示通過blazeds/lcds訪問遠(yuǎn)程EJB,Pomer框架封裝了blazeds/lcds的訪問式,用戶無須添加配置文件,只須在EJBDestinationRegister注冊即可將Java對象發(fā)布成blazeds/lcds的遠(yuǎn)程對象(RemoteObject),進(jìn)行遠(yuǎn)程訪問。
- 新建EJB項(xiàng)目并增加一個無狀態(tài)的SessionBean
在Myeclipse中如何新建和布署EJB不在本講解范圍,用戶可參照MyEclipse相關(guān)幫助文檔。
用戶自己建一個HelloEJB的SessionBean,并增加public String hello(String name)方法; 確認(rèn)HelloEJB可以被遠(yuǎn)程訪問
- 注冊EJB
在cn.org.pomer.flex.remoting.services.EJBDestinationRegister的構(gòu)造函數(shù)增加EJB注冊信息。復(fù)制內(nèi)容到剪貼板代碼:
public EJBDestinationRegister() {
super();
this.list = new ArrayList<EJBDestination>();
//add here
add("helloEJB", "HelloServicesImpl/remote");
} - 新建Flex應(yīng)用HelloEJB.mxml
復(fù)制內(nèi)容到剪貼板
代碼:
<?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> - 布署運(yùn)行
- 啟動Tomcat,日志出現(xiàn)如下標(biāo)記,布署成功
- 右擊HelloJava.mxml->Run as->Flex application
- 啟動Tomcat,日志出現(xiàn)如下標(biāo)記,布署成功
- 原理分析
查看WEB-INF\flex\services- config.xml,EJBRemotingDestinationBootstrapService類從 EJBDestinationRegister類得到所有要遠(yuǎn)程訪問的Ejb,自動加入服務(wù)中。EJBFactory根據(jù) EJBDestinationRegister提供的內(nèi)容自動創(chuàng)建遠(yuǎn)程對象。