Java学习

java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已经搬家了,新的地址是 http://www.javaly.cn 如果有对文章有任何疑问或者有任何不懂的地方,欢迎到www.javaly.cn (Java乐园)指出,我会尽力帮助解决。一起进步

 

JBoss文档(二)??JBoss开发、打包、部署

第一章补充


我看了自己写第一章后,对于build.xml没有显示全面,现在补充如下:


<!-- Simple Ant build script to test an Ant installation -->

<project name="TestInstall" default="run" basedir=".">

<target name="init">

<available file="ASimpleHelloObject.java" property="ASimpleHelloObject"/>

</target>

<target name="ASimpleHelloObject" unless="ASimpleHelloObject" depends="init">

<echo file="ASimpleHelloObject.java">

public class ASimpleHelloObject

{

public static void main(String[] args)

{

System.out.println("ASimpleHelloObject.main was called");

}

}

</echo>

<echo message="Wrote ASimpleHelloObject.java" />

</target>

<target name="compile" depends="ASimpleHelloObject">

<javac destdir="." srcdir="." debug="on" classpath=".">

<include name="ASimpleHelloObject.java"/>

</javac>

</target>

<target name="run" depends="compile">

<java classname="ASimpleHelloObject" classpath="." />

<echo message="Ant appears to be successfully installed" />

</target>

</project>




1、下载例子源程序 


  所有例子的源代码,都在文件documentation-example.zip(windows平台) or documentation-example.tar.gz (Unix/Lunix平台)。你可以直接从 www.jboss.org进行下载。下载完后放在一个目录下。下载网址:http: //www.jboss.org/docs/manual/files/documentation-example.zip  


1.1 建立 BEAN

    此节主要是建立一个简单的EJB,可以查看代码,这个“Interest”例子,是一个简单无状态的会话EJB。它的目的是根据说明的利息率,来对借的所有钱计算利息。实际上在整个包代码中只有一行功能。




1.2 回顾EJBs 

    在我们查看代码之前,我们先对EJB进行复习一下。在EJB最小类型,也必须有三个类:remote interface, home interface和bean实现类。

    remote interface是会把EJB中方法提供给外边世界,让外边的代码来进行调用,在这个例子中类名称是org.jboss.interest.Interrest。

    home interface是管理remote interface类的类。包括建立、删除等操作。在这个例子中类名称是org.jboss.interest.InterrestHome。

    bean实现类提供home interface和remote interface所有方法的实现。在这个例子中类名称是org.jboss.interest.InterrestBean。




    当然一个Bean可能还包括其他类,甚至其他包。但是必须有此三个类,其他类是在此三个类之上建立的。所有类被打包进一个JAR文件,此文件是用一个目录 结构来反映出包的层次关系。在此例子中所有类都打包在org.jboss.interest包中,所以他们需要在目录 org/jboss/interest/下。


    在包含所有类的jar文件建立之前,必须有一个META-INF目录。此目录存放了部署描述符(通常叫“ejb-jar.xml”),和可选的其他XML文件。这些文件告诉服务器关于应用明确服务信息。对于JBoss 来讲,文件名必须叫“jboss.xml”。


    创建jar文件后部署到JBoss Server上。在客户端你需要一个jndi.properties文件,此文件告诉你的客户端程序从哪里初始化查找JNDI 命名服务。从这个服务,客户端将查找到Interest bean,并且返回bean的home interface。home interface用来得到bean的一个remote interface。它可以用远程接口来访问bean提供的商业方法。  


1.3 EJB类

    我们需要三个类:remote interface, home interface 和bean实现类。remote interface远程接口类,文件名Interest.java。代码如下:


package org.jboss.docs.interest;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

/**

This interface defines the `Remote' interface for the `Interest' EJB. Its

single method is the only method exposed to the outside world. The class

InterestBean implements the method.

*/

public interface Interest extends EJBObject

{

/** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ 

     public double calculateCompoundInterest(double principle, double rate, double periods) throws RemoteException;

}


   远程接口只有一个商业方法calculateCompoundInterest。Home interface 文件名InterestHome.java。代码如下:




package org.jboss.docs.interest; 

import java.io.Serializable;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;  

/**

  This interface defines the 'home' interface for the 'Interest' EJB.

*/

 public interface InterestHome extends EJBHome

 {

   /** Creates an instance of the `InterestBean' class on the server, and returns a remote reference to an Interest interface on the client. */ 

    Interest create() throws RemoteException, CreateException;

}


    最后我们给出bean实现类,文件名称:InterestBean.java。代码如下:


package org.jboss.docs.interest;  

import java.rmi.RemoteException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

/**

 This class contains the implementation for the 'calculateCompoundInterest' method exposed by this Bean. It includes empty method bodies for the methods prescribe by the SessionBean interface; these don't need to do anything in this simple example. */public class InterestBean implements SessionBean

{

/** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ 

    public double calculateCompoundInterest(double principle, double rate, double periods)   
{  

     System.out.println("Someone called `calculateCompoundInterest!'"); 

     return principle * Math.pow(1+rate, periods) - principle; 

    } 

    /** Empty method body */ 

    public void ejbCreate() {} 

    /** Every ejbCreate() method ALWAYS needs a corresponding ejbPostCreate() method with exactly the same parameter types. */ 

    public void ejbPostCreate() {} 

    /** Empty method body */ 

    public void ejbRemove() {} /** Empty method body */ 

    public void ejbActivate() {} /** Empty method body */ 

    public void ejbPassivate() {} /** Empty method body */ 

    public void setSessionContext(SessionContext sc) {}


}

    注意大部分方法是空的。因为这些方法在SessionBean接口中说明,所以必须在InterestBean中存在,但在这个简单例子中,不需要具体内容。  


2、部署描述符 


    当你编辑完类文件后,我们来看部署描述符。此文件告诉EJB Server是哪个类应该被唤醒bean、Home Interface 和remote Interface。如果一个包中有不止一个bean,它指明ejb同另外bean如何相和。在这
个简单例子中只有一个ejb,所以我们不用关心这方面问题。

    大部分商业EJB Server都提供图形化部署工具,来构造部署描述符,JBoss没有XML编辑器,但是它能够简单手工构造部署描述符。下面是Interest Bean部署描述符:




   <?xml version="1.0" encoding="UTF-8"?>

   <ejb-jar>

      <description>JBoss Interest Sample Application</description> 

      <display-name>Interest EJB</display-name> 

      <enterprise-beans>  

         <session> 

            <ejb-name>Interest</ejb-name> 

            <home>org.jboss.docs.interest.InterestHome</home> 

            <remote>org.jboss.docs.interest.Interest</remote> 

            <ejb-class>org.jboss.docs.interest.InterestBean</ejb-class> 

            <session-type>Stateless</session-type> 

            <transaction-type>Bean</transaction-type> 

         </session> 

      </enterprise-beans>

   </ejb-jar>




    部署描述符文件必须命名为“ejb-jar.xml”,并且必须放在目录META-INF下。一个大家容易犯的错误是目录名字,有的写成“META_INF”, “META~INF”或“meta-inf”所有这些都不能正常的工作。


    我们在server上部署一个应用程序,而不是一个ejb。在这个例子中我们的应用程序只有一个ejb。在部署描述符中<ejb-name> Interest</ejb-name>表明ejb的名称。JBoss缺省是把bean的home interface作为JNDI命名空间放在<ejb-name>中,除非你修改缺省值。实际上客户端应用程序不是必须使用这个名字的。开发 者不用为JNDI命名空间指明一个不同的名字而费心。然而一个由完整应用程序的产品包含多个beans,通常用一个和开发者说明不同的名字。所以会发现 “[application name]/[bean name]”等,关于这部分我们以后介绍。


    尽管部署描述符格式的文件ejb-jar.xml对所有ejb Server都是共有的。你可以从sun得到一个正确定义的DTD, 但是没有为一个特殊EJB Server指明每个必须要求。也没有说明如何从ejb-name影射到部署的JNDI名字,例如“[application name]/[bean name]”。通过JBoss提供一个缺省行为,来进行工作。这些工作来自于ejb-jar.xml。在高级配置的案例中,你必须定义JBoss说明行 为, 此行为用jboss.xml部署描述符。在高级配置中我们会谈到jboss.xml描述符的详细信息的。


     这里我们仅配置jboss.xml描述符的JNDI名字来用来访问Interest EJB home inrterface。通过缺省的JNDI名字用来访问EJB home inrterface, 需要同ejb-jar.xml的ejb-name相同。对于这个简单的例子,Interest bean通过JNDI初始化上下文(作为“Interest”)来定位。 我们想用“interest/Interest”使home interface 可以使用,所以我们必须用jboss.xml描述符来说明。在jboss.xml中,重新覆盖缺省的JNDI命名。为了重新编写缺省的行为,我们用ejb -jar.xml中的ejb-name元素值作为bean home接口的JNDI命名,必须说明jndi命名,来写jboss.xml描述符如下:


   <?xml version="1.0" encoding="UTF-8"?>

   <jboss>

      <enterprise-beans>

         <session> 

            <ejb-name>Interest</ejb-name> 

            <jndi-name>interest/Interest</jndi-name> 

         </session> 

      </enterprise-beans>

   </jboss>




    这个文件说明调用的Interest BEAN被绑定在interest/Interest的JNDI名称下。我们建立的标准ejb-jar.xml部署描述符同JBoss说明 jboss.xml共同设置Interest EJB home接口的JNDI名称为“interest/Interest”。我们现在有了EJB类,这些类是建立ejb 文件包必须的文件。




3、打包和部署BEAN


    jar包是建立一个JAR文件,该文件包含EJB类文件和部署描述符。在下载的例子可能和你设置环
境不同,所以需要修改examplesuilduild.xml的内容。关于各个ant方面的内容,我会在Ant文档中介绍给大家。关于如何修改,下面我一步步地来告诉大家。以下是windows平台的。至于Unix/Lunix平台基本相似,这里就不在叙述了。


1) 在修改之前,比首先设定JBOSS_DIST环境变量,指定到你安装的Jboss的目录。我安装在C:jboss-3.0.6_tomcat-4.1.18, 所以 JBOSS_DIST设定为C:jboss-3.0.6_tomcat-4.1.18。


2) 你是否安装了tomcat或jetty等web服务器。如果你没有安装,建议你安装jboss-3.0.6_tomcat-4.1.18, JBoss和tomcat直接结合在一起啦,不需要再单独安装,当然也可以安装jboss-jetty( JBoss和jetty直接结合在一起)。取决你喜欢什么web服务器。


 3) 完成上面两步后,我们来讨论修改examplesuilduild.xml文件。在原有的文件中的部分代码:


<target name="validate-servlet">

<!-- Override with your web server servlet jar location. 

The default assumes that JBOSS_DIST points to a 

JBoss/Tomcat bundle distribution

-->

<available property="servlet.jar" value="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar" file="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar"/>

<available property="servlet.jar" value="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar" file="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar"/>

<available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar"/>

<available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.TOMCAT_HOME}/lib/servlet.jar"/>

<property name="servlet.jar" value="COULD_NOT_FIND_SERVLET_JAR"/>

<path id="base.path_22">

<pathelement location="${jboss.dist}/client/ejb.jar"/>

<pathelement location="${jboss.dist}/client/jaas.jar"/>

<pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>

<pathelement location="${jboss.dist}/client/jboss-client.jar"/>

<pathelement location="${jboss.dist}/client/jnp-client.jar"/>

<pathelement location="${servlet.jar}"/>

</path>

<path id="base.path_24">

<pathelement location="${jboss.dist}/client/jboss-j2ee.jar"/>

<pathelement location="${jboss.dist}/client/jaas.jar"/>

<pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>

<pathelement location="${jboss.dist}/client/jboss-client.jar"/>

<pathelement location="${jboss.dist}/client/jnp-client.jar"/>

<pathelement location="${servlet.jar}"/>

</path>

</target>

  <target name="validate-jboss" depends="validate-servlet">

<available property="classpath_id" value="base.path_22" file="${jboss.dist}/client/ejb.jar" />

<available property="classpath_id" value="base.path_24" file="${jboss.dist}/client/jboss-j2ee.jar" />

  </target>

    由于此代码例子是jboss2.2中的所以对于我们不能适应,在此基础上我们来进行改动。在改动之前,来说明一下为什么改动,让大家清楚改动原因,不是一团雾水。


  <available property="servlet.jar" value="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar" file="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar"/>….

   此部分说明你运用的web服务器是什么,根据你使用的web服务器来设置servlet.jar。 
由于我使用的是jboss-tomcat.。

  <path id="base.path_24"> … </path>说明客户端要的CLASSPATH包。


修改以后的部分文件:

<target name="validate-servlet">

<!-- Override with your web server servlet jar location.

The default assumes that JBOSS_DIST points to a

JBoss/Tomcat bundle distribution

-->

<available property="servlet.jar" value="${env.JBOSS_DIST}/tomcat-4.1.x/common/lib/servlet.jar" file="${env.JBOSS_DIST}/tomcat-4.1.x/common/lib/servlet.jar"/>

<available property="servlet.jar" value="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar" file="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar"/>

<available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar"/>

<available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.TOMCAT_HOME}/lib/servlet.jar"/>

<property name="servlet.jar" value="COULD_NOT_FIND_SERVLET_JAR"/>



<path id="base.path_22">

<pathelement location="${jboss.dist}/client/ejb.jar"/>

<pathelement location="${jboss.dist}/client/jaas.jar"/>

<pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>

<pathelement location="${jboss.dist}/client/jboss-client.jar"/>

<pathelement location="${jboss.dist}/client/jnp-client.jar"/>

<pathelement location="${servlet.jar}"/>

</path>

<path id="base.path_306">

<pathelement location="${jboss.dist}/client/jboss-j2ee.jar"/>

<pathelement location="${jboss.dist}/client/jboss-jaas.jar"/>

<pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>

<pathelement location="${jboss.dist}/client/jboss-client.jar"/>

<pathelement location="${jboss.dist}/client/jnp-client.jar"/>

<pathelement location="${jboss.dist}/client/jbossall-client.jar"/>

<pathelement location="${jboss.dist}/client/log4j.jar"/>

<pathelement location="${servlet.jar}"/>

</path>

</target>



<target name="validate-jboss" depends="validate-servlet">

<available property="classpath_id" value="base.path_22" file="${jboss.dist}/client/ejb.jar" />

<available property="classpath_id" value="base.path_306" file="${jboss.dist}/client/jboss-j2ee.jar" />



</target>


4)修改完后存盘。然后到exaplesuild目录下运行以下命令:

         ant intro-interest-jar

         如果成功会提示 : BUILD SUCCESSFUL 。

     在编译EJB 类文件,建立ejb-jar文件时,如果有一个java.lang.NoClassDefFoundError异常

产生,你需要检查你的classpath。修改<path id="base.path_306">中的内容。


5)完成后你可以查看interest.jar文件中内容:

         jar ?tvf interest.jar为了把bean部署到server上,必须拷贝interest.jar文件到JBOSS_DIST/server/default/deploy目录下。服务器自己会发现发生变化的文件,并重新部署它。


6)当JBoss server运行时,通过在examlpesuild目录下运行ant intro-interest-deploy来部署jar。在你部署期间,你会看到在server控制台的一些信息。


7)如果你发现部署失败的信息,通常是部署描述符ejb-jar.xml文件结构坏了或丢失或在错误的目录下。


8)如果在Server上部署完后,我们来编写一个简单客户端来测试它是否正常工作。




4、 编码和编译客户端


    单 独一个ejb是不能用,我们至少需要一个简单客户端来用它的服务。一个EJB可以被其他的EJB,普通的JavaBean,一个JSP page,一个applet程序或是独立的应用程序调用。在这个例子中。我们编写一个简单的应用程序。这个应用程序将会建立Interest类的一个对 象。并且执行它的方法。当部署完bean后,server会绑定EJB home 接口到interest/Interest的JNDI名字下,并输出此home 接口,能通过RMI被调用。客户端代码如下:


package org.jboss.docs.interest;



import javax.naming.InitialContext;

import javax.naming.Context;

import javax.rmi.PortableRemoteObject;



import org.jboss.docs.interest.Interest;

import org.jboss.docs.interest.InterestHome;



/** This simple application tests the 'Interest' Enterprise JavaBean which is

implemented in the package 'org.jboss.docs.interest'. For this to work, the

Bean must be deployed on an EJB server.

*/

public class InterestClient

{

/** This method does all the work. It creates an instance of the Interest EJB on

the EJB server, and calls its `calculateCompoundInterest()' method, then prints

the result of the calculation.

*/

public static void main(String[] args)

{

// Enclosing the whole process in a single `try' block is not an ideal way

// to do exception handling, but I don't want to clutter the program up

// with catch blocks

try

{

// Get a naming context

InitialContext jndiContext = new InitialContext();

System.out.println("Got context");



// Get a reference to the Interest Bean

Object ref = jndiContext.lookup("interest/Interest");

System.out.println("Got reference");



// Get a reference from this to the Bean's Home interface

InterestHome home = (InterestHome)

PortableRemoteObject.narrow(ref, InterestHome.class);



// Create an Interest object from the Home interface

Interest interest = home.create();



// call the calculateCompoundInterest() method to do the calculation

System.out.println("Interest on 1000 units, at 10% per period, compounded over 2 periods is:");

System.out.println(interest.calculateCompoundInterest(1000, 0.10, 2));

}

catch(Exception e)

{

System.out.println(e.toString());

}

}

}


    客户端在JNDI命名“interest/Interest”下查找InterestHome接口。如果你提供了一个正确的jboss.xml文件, Bean home接口将会绑定到这个名字。否则,JNDI的名字是“Interest”。为了连接JBoss JNDI,必须建立一个初始化上下文,该上下文是根据在客户端的classpath路径下jndi.properties来的。这个 jndi.property文件我们放在了examples esourcesjndi.properties中。代码如下: 


java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

java.naming.provider.url=localhost:1099

java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces




    指定了InitialContextFactory(初始化上下文工厂),url,和工厂对象的包。在这里url是“localhost”,或运行 JBoss的主机名(IP),用的缺省端口是1099。为了不同RMI协议互相兼容,通过“narrow”方法来确保“ref”对象能转换为 “InterestHome”对象。客户端不需要必须和EJB类在同一个包中。所以它必须import ejb claess类。


   为了能运行Ant命令。我修改了examplesorgjbossdocsinterestuild.xml文件,原文件的部分内容:


<target name="deploy-ejb-jar" depends="ejb-jar">

<copy file="${build.interest.dir}/interest.jar" todir="${jboss.dist}/deploy" />

</target>

<target name="deploy-ear" depends="ear">

<copy file="${build.interest.dir}/interest.ear" todir="${jboss.dist}/deploy" />

</target>


修改后为:


<target name="deploy-ejb-jar" depends="ejb-jar">

<copy file="${build.interest.dir}/interest.jar" todir="${jboss.dist}/server/default/deploy" />

</target>

<target name="deploy-ear" depends="ear">

<copy file="${build.interest.dir}/interest.ear" todir="${jboss.dist}/server/default/deploy" />

</target>

   修改完后保存。编译和运行InterestClient,通过以下命令:

   ant intro-interest-client会出现正确的结果。 


5、补充说明


   各个jar文件:


   1) jboss-j2ee.jar :是标准的javax.ejb.* 包括EJB2.0接口和类。


   2) jboss-jaas.jar :是java鉴定和授权服务安全类。


   3) jbosssx-client.jar :是JbossSX安全扩展客户端类。


   4) jboss-client.jar :是Jboss EJB容器代理和stub客户端类。


   5) jnp-client.jar :是JBoss JNDI提供者客户端类。


现在隐藏代码,编译和部署EJB, 运行简单客户端进行测试。


6、 结论 


   最后来进行一个总结。


   Server:


     为每个EJB(包括有、无状态的Session,或实体Bean),都需要三个文件: bean实现类, remote接口类,home接口类。这些类和一个或多个部署描述符(ejb-jar.xml, jboss.xml等),这些描述符必须在META-INF目录下,一起打包进jar文件。为部署这个jar文件,你只需要拷贝到 jbossserverdefaultdeploy下。 


   Client:


     1)从JNDI服务,查找bean的home 接口;


     2)从home接口,建立一个新的bean或得到一个已经存在bean;


     3)用得到的remote接口来访问服务器上bean实现类中商业方法。


    在客户端查找home 接口之前,客户端必须知道如何从JNDI来定位。因此我们提供了一个jndi.properties文件。这个文件和一些jbossclientx下的jar,必须包含到CLASSPATH中,才能使客户端运行。

posted on 2008-10-06 15:56 找个美女做老婆 阅读(965) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

本blog已经搬到新家了, 新家:www.javaly.cn
 http://www.javaly.cn

常用链接

留言簿(6)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜