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”對象??蛻舳瞬恍枰仨毢虴JB類在同一個包中。所以它必須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 找個美女做老婆 閱讀(987) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

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

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 嘉荫县| 宜兴市| 德令哈市| 杨浦区| 应用必备| 龙川县| 醴陵市| 三台县| 武功县| 穆棱市| 泗水县| 谷城县| 韶关市| 崇州市| 永顺县| 遂溪县| 常山县| 樟树市| 全椒县| 扎赉特旗| 垦利县| 安图县| 土默特左旗| 周至县| 衡东县| 项城市| 安溪县| 元氏县| 定边县| 东安县| 页游| 周宁县| 凉山| 安义县| 凤山县| 昌宁县| 亚东县| 同德县| 历史| 固原市| 印江|