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