国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

教您如何成為 EJB 專家詳解系列連載之三

2019-11-18 12:35:07
字體:
供稿:網(wǎng)友

  無狀態(tài)會話beans基礎(chǔ)
  無狀態(tài)會話beans是可以模擬業(yè)務(wù)過程的組件,它可以在單獨的方法調(diào)用中被執(zhí)行。Stateless session beans不能夠維持一個調(diào)用客戶的狀態(tài),在一個方法調(diào)用中,Stateless Session beans 可以維持調(diào)用客戶的狀態(tài),當方法執(zhí)行完,狀態(tài)不會被保持。在調(diào)用完成后,Stateless Session beans被立即釋放到緩沖池中,所以Stateless Session beans具有很好的伸縮性,可以支持大量用戶的調(diào)用。
  
  無狀態(tài)會話beans的特點
  1. 沒有對話狀態(tài)
  2. 無狀態(tài)會話beans可以擁有內(nèi)部狀態(tài),它們的狀態(tài)不能為非凡的客戶端定制。這意味著所有的無狀態(tài)beans對于客戶端是無差別的,客戶端也不能分離它們。客戶端必須將所有的必需的客戶端數(shù)據(jù)作為業(yè)務(wù)邏輯方法的參數(shù)傳給無狀態(tài)beans,無狀態(tài)beans可以從外部資源(例如數(shù)據(jù)庫)獲得所需的數(shù)據(jù)。
  3. 初始化無狀態(tài)beans只有一種方法,我們知道會話beans的初始化調(diào)用ejbCreate()方法,因為無狀態(tài)會話beans不能夠在方法調(diào)用之間保留狀態(tài),因此它也不能在客戶端給ejbCreate()調(diào)用傳遞數(shù)據(jù)以后保留狀態(tài)。調(diào)用不帶參數(shù)的ejbCreate()或create()。
  4. 容器可以聚集和重用無狀態(tài)會話beans。
  
  
  構(gòu)建“Hello,World!”遠程接口
  package com.wiley.compBooks.roman.session.helloworld;
  import javax.ejb.*;
  import java.rmi.RemoteException;
  import java.rmi.Remote;
  /**
  * This is the Hellobeans remote interface.
  *
  * This interface is what clients Operate on when
  * they interact with EJB objects. The container
  * vendor will implement this interface; the
  * implemented object is the EJB object, which
  * delegates invocations to the actual beans.
  */
  public interface Hello extends EJBObject {
  /**
  * The one method - hello - returns a greeting to the client.
  */
  public String hello() throws java.rmi.RemoteException;
  }
  Source 4.1 Hello.java.
  
  Hello接口繼續(xù)了EJBObject接口,EJBObject繼續(xù)Remote接口,因此hello可以拋出rmi異常。 下面建立beans,實現(xiàn)業(yè)務(wù)方法:hello()。 他實現(xiàn)了javax.ejb.Sessionbeans接口:
  
  package com.wiley.compBooks.roman.session.helloworld;
  import javax.ejb.*;
  /**
  * Demonstration stateless session beans.
  */
  public class Hellobeans implements Sessionbeans {
  //
  // EJB-required methods
  //
  public void ejbCreate() {
  System.out.  }
  public void ejbRemove() {
  System.out.println("ejbRemove()");
  }
  public void ejbActivate() {
  System.out.println("ejbActivate()");
  }
  public void ejbPassivate() {
  System.out.println("ejbPassivate()");
  }
  public void setSessionContext(SessionContext ctx) {
  System.out.println("setSessionContext()");
  }
  //
  // Business methods
  //
  public String hello() {
  System.out.println("hello()");
  return "Hello, World!";
  }
  }
  Source 4.2 Hellobeans.java
  
  注重:不需要實現(xiàn)自己的遠程接口,初始化方法不帶參數(shù)。破壞beans時,使用比較簡單的ejbRemove()方法。ejbActivate()和ejbPassivate()方法不需應用在無狀態(tài)會話beans,因此,這兩個方法為空。建立“Hello,World!”Home接口:
  
  Home接口繼續(xù)了javax.ejb.EJBHome。Home接口為EJB對象擴展了一個不帶參數(shù)的方法create()方法。
  
  package com.wiley.compBooks.roman.session.helloworld;
  import javax.ejb.*;
  import java.rmi.RemoteException;
  /**
  * This is the home interface for Hellobeans. This interface
  * is implemented by the EJB Server′s glue-code tools - the
  * implemented object is called the Home Object and serves
  * as a factory for EJB Objects.
  *
  * One create() method is in this Home Interface, which
  * corresponds to the ejbCreate() method in Hellobeans.
  */
  public interface HelloHome extends EJBHome {
  /*
  * This method creates the EJB Object.
  *
  * @return The newly created EJB Object.
  */
  Hello create() throws RemoteException, CreateException;
  }
  creat方法拋出了a java.rmi.RemoteException和aavax.ejb.CreateException.異常。
  
  寫配置描述符
  在EJB1.0中,配置描述符是作為文件存儲在磁盤上的java對象。在EJB1.1種,配置描述符是一個
xml文檔。EJB容器或IDE環(huán)境應該提供生成配置描述符的工具。
  配置描述符的設(shè)置
  beans home的名字
  企業(yè)級beans類名
  home接口類名
  遠程接口類名
  Re-entrant
  狀態(tài)或無狀態(tài)
  會話時間
  Hellobeans的配置描述符
  環(huán)境屬性
  beans通過使用此信息來適應不同的非凡環(huán)境。
  Ejb-jar文件
  我們需要將我們所需要的文件打包成Ejb-jar文件。
  企業(yè)級的beans
  遠程接口
  home接口
  配置描述符,包括屬性
  以上這些必須被包含進Ejb-jar文件。在EJB1.0中,jar文件理有一個文本文件的列表。它表示jar的具體信息。它用來鑒別哪個企業(yè)beans在Ejb-jar文件。在EJB1.1中,XML文件包含了所有的必要信息。 生成Ejb-jar文件
  jar cmf ../manifest HelloWorld.jar *
  配置beans
  
  最后,我們還需要在Ejb容器中配置beans。經(jīng)常執(zhí)行一下步驟:
  Ejb-jar文件的檢驗
  容器工具來產(chǎn)生EJB對象和home對象
  容器工具來生成RMI所需的stubs和skeletons
  寫無狀態(tài)beans的客戶代碼:
  
  package com.wiley.compBooks.roman.session.helloworld;
  import javax.ejb.*;
  import javax.naming.*;
  import java.rmi.*;
  import java.util.Properties;
  /**
  * This class is an example of client code that invokes
  * methods on a simple stateless session beans.
  */
  public class HelloClient {
  public static void main(String[] args) {
  try {
  /*
  * Get System properties for JNDI initialization
  */
  Properties props = System.getProperties();
  /*
  * Form an initial context
  */
  Context ctx = new InitialContext(props);
  /*
  * Get a reference to the home object
  * (the factory for EJB objects)
  */
  HelloHome home = (HelloHome) ctx.lookup("HelloHome");
  /*
  * Use the factory to create the EJB Object
  */
  Hello hello = home.create();
  /*
  * Call the hello() method, and print it
  */
  System.out.println(hello.hello());
  /*
  * Done with EJB Object, so remove it
  */
  hello.remove();
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
  }
  
  客戶端代碼執(zhí)行了一下任務(wù):
  
  定位home接口
  
  使用home接口建立EJB對象
  
  調(diào)用EJB對象上的hello()
  
  移走EJB對象
  
  運行
  
  首先運行應用服務(wù)器。對于BEA的WebLogic,執(zhí)行:
  
  t3server
  
  客戶端執(zhí)行:
  
  java -Djava.naming.factory.initial=
  weblogic.jndi.TengahInitialContextFactory
  -Djava.naming.provider.url=
  t3://localhost:7001
  com.wiley.compBooks.roman.session.helloworld.HelloClient
  
  服務(wù)端輸出:
  
  setSessionContext()
  ejbCreate()
  hello()
  ejbRemove()
  
  客戶端輸出:
  
  Hello, World!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 淅川县| 宁明县| 南溪县| 新野县| 米林县| 清丰县| 左贡县| 融水| 二手房| 尖扎县| 景洪市| 治县。| 丹东市| 龙里县| 龙州县| 崇礼县| 枣庄市| 武川县| 桃源县| 台山市| 肃南| 双桥区| 油尖旺区| 黄冈市| 衡阳市| 濮阳市| 甘孜| 韶关市| 饶平县| 宁津县| 宁德市| 二手房| 溆浦县| 凤山市| 屏山县| 岚皋县| 南华县| 迭部县| 肇东市| 新昌县| 乌拉特后旗|