服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

beans入门必读之状态会话bean基础


  stateful session bean可以一对一的维持某个调用客户的状态,并且在不同的方法调用中维持这个状态, 由于对于每一个并发用户,必须有一个对应的stateful session bean,为了提高系统的效率,stateful session bean可以在一定的客户空闲时间后被写入二级存储设备(如硬盘),在客户发出新的调用请求后,再从二级存储 设备恢复到内存中。但是在多用户下,stateless session bean运行效率高于stateful session bean。
  javax.ejb.enterprisebean接口继承了java.io.serializable,用以实现写入读出操作。
  当ejb容器调用ejbpassivate()方法钝化了bean之后,就可以把它写入二级存储设备,然后容器调用ejbactivate()方法激活bean,把它从二级存储设备中读出。
  
  状态bean的钝化过程
  计数bean的远程接口
  远程接口定义了一个业务方法count(),它将在企业bean类中实现。
  
  
  激活状态bean
  package com.wiley.compbooks.roman.session.count;
  import javax.ejb.*;
  import java.rmi.remoteexception;
  /**
  * these are countbean'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 bean.
  */
  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 bean. this bean 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 bean and how passivation/activation works.
  */
  public class countbean implements sessionbean {
  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 countbean.java
  bean实现了javax.ejb.sessionbean。所以,它必须定义所有sessionbean定义的方法。
  ojbcreate()初始化带了val的参数。它将作为counter的初始状态。在钝化和激活bean的过程中,val变量被保护。
  
  计数bean的home接口
  package com.wiley.compbooks.roman.session.count;
  import javax.ejb.*;
  import java.rmi.remoteexception;
  /**
  * this is the home interface for countbean. 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 countbean 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.
  计数bean的配置描述符
  
  计数bean的配置描述符
  计数bean的环境属性
  生成计数bean的ejb-jar文件
  计数bean的客户端代码
  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 bean.
  *
  * we create 3 ejb objects in this example, but we allow
  * the container to have only 2 in memory. this illustrates how
  * beans 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、配置描述符限制了同时只能有两个bean工作,因此3个bean中一定有钝化的。在调用ejbpassivate()时,打印一条信息。
  5、在每个ejb对象上调用count()方法,调用ejbactivate()方法激活bean,该方法打印一条信息。
  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 bean.
  *
  * we create 3 ejb objects in this example, but we allow
  * the container to have only 2 in memory. this illustrates how
  * beans 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 beans...");
  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 beans were passivated and
  * activated properly.
  */
  system.out.println("calling count() on beans...");
  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

扫描关注微信公众号