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

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

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

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

  Stateful session Beans可以一對一的維持某個調(diào)用客戶的狀態(tài),并且在不同的方法調(diào)用中維持這個狀態(tài),由于對于每一個并發(fā)用戶,必須有一個對應(yīng)的Stateful Session Beans,為了提高系統(tǒng)的效率,Stateful Session Beans可以在一定的客戶空閑時間后被寫入二級存儲設(shè)備(如硬盤),在客戶發(fā)出新的調(diào)用請求后,再從二級存儲 設(shè)備恢復(fù)到內(nèi)存中。
  
  但是在多用戶下,Stateless Session Beans運行效率高于Stateful Session Beans。javax.ejb.EnterPRiseBeans接口繼續(xù)了Java.io.Serializable,用以實現(xiàn)寫入讀出操作。當(dāng)EJB容器調(diào)用ejbPassivate()方法鈍化了Beans之后,就可以把它寫入二級存儲設(shè)備,然后容器調(diào)用ejbActivate()方法激活Beans,把它從二級存儲設(shè)備中讀出。
  
  狀態(tài)Beans的鈍化過程;計數(shù)Beans的遠(yuǎn)程接口;遠(yuǎn)程接口定義了一個業(yè)務(wù)方法count(),它將在企業(yè)Beans類中實現(xiàn)。
  
  激活狀態(tài)Beans:
  package com.wiley.compBooks.roman.session.count;
  import Javax.ejb.*;
  import Java.rmi.RemoteException;
  /**
  * These are CountBeans′s business logic methods.
  *
  * 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 Count extends EJBObject {
  /**
  * Increments the int stored as conversational state
  */
  public int count() throws RemoteException;
  }
  Source Count.Java
  
  package com.wiley.compBooks.roman.session.count;
  import Javax.ejb.*;
  /**
  * Demonstration Stateful Session Beans. This Beans is
  * initialized to some integer value and has a business
  * method that increments the value.
  *
  * This example shows the basics of how to write a stateful
  * session Beans and how passivation/activation works.
  */
  public class CountBeans implements SessionBeans {
  private SessionContext ctx;
  // The current counter is our conversational state.
  public int val;
  //
  // Business methods
  //
  /**
  * Counts up
  */
  public int count() {
  System.out.println("count()");
  return ++val;
  }
  //
  // EJB-required methods
  //
  public void ejbCreate(int val)
  throws CreateException {
  this.val = val;
  System.out.println("ejbCreate()");
  }
  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) {
  }
  }
  Source CountBeans.Java
  Beans實現(xiàn)了Javax.ejb.SessionBeans。
  所以,它必須定義所有SessionBeans定義的方法。
  OjbCreate()初始化帶了val的參數(shù)。
  它將作為counter的初始狀態(tài)。
  在鈍化和激活Beans的過程中,val變量被保護(hù)。
  
  計數(shù)Beans的home接口
  package com.wiley.compBooks.roman.session.count;
  import Javax.ejb.*;
  import Java.rmi.RemoteException;
  /**
  * This is the home interface for CountBeans. 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 the CountBeans file.
  */
  public interface CountHome extends EJBHome {
  /*
  * This method creates the EJB Object.
  *
  * @param val Value to initialize counter to
  *
  * @return The newly created EJB Object.
  */
  Count create(int val) throws RemoteException, CreateException;
  }
  Source CountHome.Java.
  計數(shù)Beans的配置描述符
  
  計數(shù)Beans的配置描述符
  計數(shù)Beans的環(huán)境屬性
  生成計數(shù)Beans的Ejb-jar文件
  計數(shù)Beans的客戶端代碼
  package com.wiley.compBooks.roman.session.count;
  import Javax.ejb.*;
  import Javax.naming.*;
  import Java.util.Properties;
  /**
  * This class is a simple example of client code that invokes
  * methods on a simple Stateless Enterprise Beans.
  *
  * We create 3 EJB Objects in this example, but we allow
  * the container to have only 2 in memory. This illustrates how
  * Beanss are passivated to storage.
  */
  public class CountClient {
  public static void main(String[] args) {
  try {
  /*
  * Get System properties for JNDI initialization
  */
  Properties props = System.getProperties();
  /*
  * Get a reference to the Home Object - the
  * factory for EJB Objects
  */
  Source CountClient.Java
  1、需要JNDL初始化上下文
  2、使用JNDL定位home接口
  3、使用home對象建立3個不同的計數(shù)EJB對象,
  因此也就和三個不同的客戶端建立了會話
  4、配置描述符限制了同時只能有兩個Beans工作,
  因此3個Beans中一定有鈍化的。在調(diào)用ejbPassivate()時,打印一條信息。
  5、在每個EJB對象上調(diào)用count()方法,
  調(diào)用ejbActivate()方法激活Beans,該方法打印一條信息。
  6、最后所有的EJB對象被刪除。
  package com.wiley.compBooks.roman.session.count;
  import Javax.ejb.*;
  import Javax.naming.*;
  import Java.util.Properties;
  /**
  * This class is a simple example of client code that invokes
  * methods on a simple Stateless Enterprise Beans.
  *
  * We create 3 EJB Objects in this example, but we allow
  * the container to have only 2 in memory. This illustrates how
  * Beanss are passivated to storage.
  */
  public class CountClient {
  public static void main(String[] args) {
  try {
  /*
  * Get System properties for JNDI initialization
  */
  Properties props = System.getProperties();
  /*
  * Get a reference to the Home Object - the
  * factory for EJB Objects
  */
  Context ctx = new InitialContext(props);
  CountHome home = (CountHome) ctx.lookup("CountHome");
  /*
  * An array to hold 3 Count EJB Objects
  */
  Count count[] = new Count[3];
  int countVal = 0;
  /*
  * Create and count() on each member of array
  */
  System.out.println("Instantiating Beanss...");
  for (int i=0; i < 3; i++) {
  /*
  * Create an EJB Object and initialize
  * it to the current count value.
  */
  count[i] = home.create(countVal);
  /*
  * Add 1 and print
  */
  countVal = count[i].count();
  System.out.println(countVal);
  /*
  * Sleep for 1/2 second
  */
  Thread.sleep(500);
  }
  /*
  * Let′s call count() on each EJB Object to
  * make sure the Beanss were passivated and
  * activated properly.
  */
  System.out.println("Calling count() on Beanss...");
  for (int i=0; i < 3; i++) {
  /*
  * Add 1 and print
  */
  countVal = count[i].count();
  System.out.println(countVal);
  /*
  * Sleep for 1/2 second
  */
  Thread.sleep(500);
  }
  /*
  * Done with EJB Objects, so remove them
  */
  for (int i=0; i < 3; i++) {
  count[i].remove();
  }
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
  }
  Source CountClient.Java
  運行客戶端:
  對于BEA的WebLogic,執(zhí)行:
  Java -DJava.naming.factory.initial=
  weblogic.jndi.TengahInitialContextFactory
  -DJava.naming.provider.url=
  t3://localhost:7001
  com.wiley.compBooks.roman.session.count.CountClient
  客戶端輸出:
  Instantiating Beanss...
  1
  2
  3
  Calling count() on Beanss...
  2
  3
  4
  服

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宽城| 泾阳县| 乐至县| 海宁市| 临邑县| 汉阴县| 彭阳县| 肇东市| 普洱| 紫金县| 安溪县| 防城港市| 林周县| 理塘县| 莱芜市| 海安县| 彝良县| 连城县| 曲麻莱县| 温泉县| 天门市| 肃南| 桐乡市| 蚌埠市| 高陵县| 根河市| 九龙坡区| 依兰县| 永嘉县| 庄河市| 怀仁县| 巴彦淖尔市| 彝良县| 丰城市| 侯马市| 长垣县| 儋州市| 建始县| 建始县| 北流市| 冷水江市|