科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道教您如何成为 EJB 专家详解系列连载之四

教您如何成为 EJB 专家详解系列连载之四

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Stateful Session Beans可以一对一的维持某个调用客户的状态,并且在不同的方法调用中维持这个状态,由于对于每一个并发用户,必须有一个对应的Stateful Session Beans

作者:中国IT实验室 来源:中国IT实验室 2007年9月7日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
  Stateful Session Beans可以一对一的维持某个调用客户的状态,并且在不同的方法调用中维持这个状态,由于对于每一个并发用户,必须有一个对应的Stateful Session Beans,为了提高系统的效率,Stateful Session Beans可以在一定的客户空闲时间后被写入二级存储设备(如硬盘),在客户发出新的调用请求后,再从二级存储 设备恢复到内存中。
  
  但是在多用户下,Stateless Session Beans运行效率高于Stateful Session Beans。Javax.ejb.EnterpriseBeans接口继承了Java.io.Serializable,用以实现写入读出操作。当EJB容器调用ejbPassivate()方法钝化了Beans之后,就可以把它写入二级存储设备,然后容器调用ejbActivate()方法激活Beans,把它从二级存储设备中读出。
  
  状态Beans的钝化过程;计数Beans的远程接口;远程接口定义了一个业务方法count(),它将在企业Beans类中实现。
  
  激活状态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实现了Javax.ejb.SessionBeans。
  所以,它必须定义所有SessionBeans定义的方法。
  OjbCreate()初始化带了val的参数。
  它将作为counter的初始状态。
  在钝化和激活Beans的过程中,val变量被保护。
  
  计数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.
  计数Beans的配置描述符
  
  计数Beans的配置描述符
  计数Beans的环境属性
  生成计数Beans的Ejb-jar文件
  计数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个不同的计数EJB对象,
  因此也就和三个不同的客户端建立了会话
  4、配置描述符限制了同时只能有两个Beans工作,
  因此3个Beans中一定有钝化的。在调用ejbPassivate()时,打印一条信息。
  5、在每个EJB对象上调用count()方法,
  调用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,执行:
  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

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章