网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.min 方法.
.CDE 的目录结构.
.JAVA 省略语与名词解释.
.RegExp 对象.
.spring在MVC层解决JPA的缓迟加载.
.Hibernate分页查询原理解读.
.sql命令:导入导出.
.java中按字节截取String字符串..
.SUN系统维护命令大全.
.破除java神话之原子操作都是线程.
.Java也存在2000年问题.
.调整配置实现对Java的应用性能优.
.Java编程中更新XML文档的常用方法.
.使用Java Swing 创建一个XML编辑.
.Java性能之我见.
.java访问com组件_jacob使用指南.
.探讨与比较Java和.NET的事件处理.
.嵌入式Java的应用实例.
.建立一个结构合理的概念框架.
.Java 基本语法(二)——变量和常.

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

发表日期:2008-1-5



  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
  服
上一篇:详细解析 JavaBeans 与 Ejb 的区别 人气:516
下一篇:教您如何成为 EJB 专家详解系列连载之三 人气:573
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐