建立你的第一个无状态会话Bean--HelloWorld之二

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

企业JavaBeansTM学习指南:
建立你的第一个无状态(stateless)会话Bean


  • 关于本学习指南

  • 关于示例

    • 步骤1: 安装企业JavaBeans 服务器
    • 步骤2: 指定企业JavaBeans 远程界面
    • 步骤3: 指定宿主界面
    • 步骤4: 编写企业JavaBean 类
    • 步骤5: 创建ejb-jar 文件
    • 步骤6: 配置DemoBean企业JavaBeans
    • 步骤7: 编写企业JavaBean 客户程序
    • 步骤8: 运行客户程序


3. 如何建立你的第一个无状态(stateless)会话Bean



步骤 1: 安装企业 JavaBeans服务器

企业JavaBeans 组件在企业JavaBeans 容器内运行,后者是由企业JavaBeans 服务器厂商提供的。下面的说明将有助于你下载并安装一个BEA Weblogic Tengah 服务器,HelloWord Bean 将运行在该服务器上。

注意:以下的安装步骤是为 Sun Solaris 提供的, 但它与在 Windows NT 上的安装过程类似。

1)从 BEA WebLogic 的网址下载 BEA WebLogic's Tengah 服务器: http://www.weblogic.com/download.html

2)使用 jar 或 unzip 解压缩。在本书中,假定 Tengah 被安装在 /export 目录下, 例如, /export/weblogic.

3)从Java Development Kit for Solaris TM 的网址下载最新版Java开发工具包JDK TM : http://www.sun.com/solaris/java

4)在服务器主机上设置你的类路径(CLASSPATH):
添加 weblogic/classes 和 weblogic/lib/weblogicaux.jar到Java CLASSPATH上。确认 "." 或当前目录也在CLASSPATH上。 在LD_LIBRARY_PATH 上添加weblogic/lib/solaris http://www.sun.com/solaris/java 5)编辑 BEA WebLogic 的属性文件 weblogic.properties 设置系统口令, weblogic.password.system

6)用startTengah.sh 脚本启动 Tengah 服务器.
在建立并配置示例 bean 之后, 要检查企业 JavaBeans 的 bean 是否已被正确配置, 可通过服务器命令行窗口来检查,或通过打开控制台以检查在"分布式对象"下的"Enterprise JavaBeans Transaction Server",你将看到 DemoBean 已被配置,也可以监测它的活动。

步骤2: 指定企业JavaBeansTM 远程界面

在这一部分,你将创建企业JavaBeans 远程界面。
  • 请阅读下面的解释;
  • 将代码样本存储到指定的文件里。

远程界面是企业 JavaBeans 的客户端视图,企业JavaBeans 开发人员的任务是:

用 Java TM RMI 语法声明该界面。生成该界面的代码是企业JavaBeans的容器工具供应商的责任。

注意: 在这个界面中,对所能够指定的内容有一定的限制。完整的限制清单,请参见Enterprise JavaBeans Specification(企业JavaBean规范)第16节。重要的一点是,所有使用的对象、参数、返回值和异常在"Java to IDL Mapping Specification(Java对IDL的映射规范)"中都应该是有效的类型。 为简单的DemoBean编写的远程界面源程序如下:







 /**



  * Demo -- this is the "remote" interface of



  * our enterprise JavaBean, it



  * defines only one simple method called



  * demoSelect(). As this is meant to be



  * the simplest of examples demoSelect()



  * never goes to a database, it just



  * returns a string



  *



  * Note: The implementation of this interface is



  * provided by the container tools



  * but the demoSelect() method and any



  * other methods in this interface



  * will need to have equivalent



  * implementations in the demobean.java



  * which is supplied by the bean writer



  * ..i.e., you!



  */



  



  package ejb.demo;



  



  import java.rmi.RemoteException;



  import java.rmi.Remote;



  import javax.ejb.*;



  



  public interface Demo extends EJBObject, Remote {



  



   // NB this simple example does not even do a



   // lookup in the database



  public String demoSelect() throws RemoteException;



  



  }

步骤3: 指定宿主界面 (Home Interface)

一个会话 bean 的宿主界面提供了一种机制,用这种机制,容器可以代表客户端创建新的会话 bean。就象远程界面一样,宿主界面是由 bean 的开发人员用RMI语法声明的,并同样由容器供应商所提供的工具来实现。它基本上不需要程序员进行编码----所要作的仅是声明性的工作。

1) 阅读如下宿主界面的有关描述并注意在代码片段中的注释。
2) 将示例源代码存储到指定文件,并进行下一步。

这里是DemoBean EJB的源代码:



  /**



   * DemoHome.java - This is the Home interface it must



   * extend javax.ejb.EJBHome and define one or more



   * create() methods for the bean.



   *



   * Note: The implementation of this interface is 



   * generated by the container tools. 



   */ 







   package ejb.demo;







   import javax.ejb.*;



   import java.rmi.Remote;



   import java.rmi.RemoteException;



   import java.util.*;







  /**



   * This interface is extremely simple it declares only



   * one create method.



   */



   public interface DemoHome extends EJBHome {







    public Demo create() throws CreateException, 



                                RemoteException;



   



   }

步骤4: 编写企业JavaBeanTM

这一步将向你演示如何编写应用程序代码(事务逻辑)。到现在为止,你已经声明了容器工具将为其生成代码的界面,但是,这里演示的是JavaBean的功能是如何编码的。

关于此bean的实现值得注意的一点是,几乎没有什么代码需要你来编写。大部分代码都简单地通过企业JavaBeans 规范来实现。很容易想见这里有许多模板和工具,它们为你完成大部分实现工作。这与GUI beans 环境中所使用的工具的演变过程是相似的。

1)阅读如下 DemoBean 代码的描述并注意在示例代码中的注释,特别是那些在标题"Business Logic"下面的注释。

2)将示例源代码存储到指定文件,并转向下一步。

这里是 DemoBean 企业JavaBeans DemoBean.java 的源代码:



    /**



    * DemoBean -- This is implemented by the EnterPrise



    * Bean author This class must extend



    * javax.ejb.SessionBean and implement



    * the methods in this interface as well as providing



    * the implementation of the business methods.



    *



    */



    package ejb.demo;



  



    import javax.ejb.*;



    import java.io.Serializable;



    import java.util.*;



    import java.rmi.*;



  



    public class DemoBean implements SessionBean {



     static final boolean verbose = true;



  



     private transient SessionContext ctx;



     private transient Properties props;



  



     // Implement the methods in the SessionBean



     // interface



     public void ejbActivate() {



     if (verbose)



     System.out.println("ejbActivate called");



     }



  



     public void ejbRemove() {



     if (verbose)



     System.out.println("ejbRemove called");



     }



  



     public void ejbPassivate() {



     if (verbose)



     System.out.println("ejbPassivate called");



     }



  



    /**



     * Sets the session context.



     *



     * @param SessionContext



     */



     public void setSessionContext(SessionContext ctx) {    



     if (verbose)



     System.out.println("setSessionContext called");



     this.ctx = ctx;



     props = ctx.getEnvironment();



     }



    



     /**



     * This method corresponds to the create method in



     * the home interface DemoHome.java. 



     * The parameter sets of the two methods are 



     * identical. When the client calls 



     * DemoHome.create(), the container allocates an 



     * instance of the EJBean and calls ejbCreate(). 



     */ 



     public void ejbCreate () {



     if (verbose)



     System.out.println("ejbCreate called");



     }



  



    /**



     * **** HERE IS THE BUSINESS LOGIC *****



     * Do the demoSelect() but don't even go to



     * the database in this eg but instead just



     * return a String.



     * The really BIG thing to notice here is that



     * this is the only code we have invented at all



     * the rest of the code has been declarations



     * or simply implementing methods which are



     * part of the EJB interfaces and in this example



     * are not even used.



     */



     public String demoSelect()



     throws RemoteException



     {



     return("hello world");



     }



  



}
建立你的第一个无状态会话Bean--HelloWorld之一 建立你的第一个无状态会话Bean--HelloWorld之二 建立你的第一个无状态会话Bean--HelloWorld之三
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值