jboss 4.0 ejb 部署

來源:互聯網
上載者:User

jboss 4.0 Ejb helloworld

開發環境: java sdk 1.4.2,jboss 4.0,windows 2003

開發sessionbean EJB最少也需要三個class,remote interface,home interface,and bean implementation(bean行為).

1. remote interface 用來揭示EJB對外的一些方法.

package helloWorld; import javax.ejb.EJBObject;

import java.rmi.RemoteException;
public interface RemoteHello extends EJBObject
{ public String HelloEcho(String inputString) throws RemoteException; }
2.home interface 是用來規定怎樣建立一個實現remote interface的bean.
package helloWorld;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface HomeHello extends EJBHome

{ RemoteHello create() throws RemoteException, CreateException; }

3.bean implementation 是提供方法的實現,這些方法在上述兩種interface中都有規定了.

package helloWorld;

import java.rmi.RemoteException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

public class HelloBean implements SessionBean
{

   public String HelloEcho(String inputString) 
{

     System.out.println("Someone called 'Hello Echo Successed!'");

     return "*********" + inputString + "*********"; }

     /** Empty method body  */

     public void ejbCreate() {

        System.out.println("Ejb 4 is creating!...");}

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

public void ejbPostCreate() {}

/** Empty method body   */

public void ejbRemove() {

    System.out.println("Ejb 4 is removing!...");}

 /** Empty method body */

public void ejbActivate() {

   System.out.println("Ejb 4 is activating!...");}

/** Empty method body */

public void ejbPassivate()

 {}

/** Empty method body   */

public void setSessionContext(SessionContext sc)

 {}

}

部署jar

這些classes必須打包進一個JAR檔案中,JAR檔案中包含了目錄結構和包的層次.在本例中, 這些classes是在包helloworld,這樣他們需要在目錄helloWorld/ 下.

部署發布描述器ejb-jar.xml和jboss.xml
在JAR文檔建立之前,還需要一個叫META-INF的目錄,這是存放部署發布描述器的(一般叫ejb-jar.xml).

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "
http://java.sun.com/dtd/ejb-jar_2_0.dtd

">
<ejb-jar>
 <description>Neosue Helloworld Sample Application</description>
 <display-name>Helloworld EJB</display-name>
 <enterprise-beans>
  <session>
   <ejb-name>Helloworld</ejb-name>
   <!-- home interface -->
   <home>helloWorld.HomeHello</home>
   <!-- remote interface -->
   <remote>helloWorld.RemoteHello</remote>
   <!-- bean implementation -->
   <ejb-class>helloWorld.HelloBean</ejb-class>
   <session-type>Stateless</session-type>
   <transaction-type>Bean</transaction-type>
  </session>
 </enterprise-beans>
</ejb-jar>

jboss.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "
http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd

">
<jboss>
 <enterprise-beans>
  <session>
   <ejb-name>Helloworld</ejb-name>
   <jndi-name>Helloworld/Hello</jndi-name>
  </session>
 </enterprise-beans>
</jboss>
雖然有了上面你的應用程式和JNDI name的梆定,但是一旦部署發布到JBoss伺服器上,你還需要一個jndi.properties檔案,以告訴調用你程式的用戶端請求到哪裡去初始化JNDI naming service.

測試程式:

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import helloWorld.*;

public class MyTest {

   public static void main(String[] args) {

      try{ Context ctx=new InitialContext();

           Object ref=ctx.lookup("Helloworld/Hello");

           HomeHello home=(HomeHello)PortableRemoteObject.narrow(ref,HomeHello.class);

           RemoteHello user=home.create(); System.out.println(user.HelloEcho("So easy!"));

        }catch(Exception e) {

          e.printStackTrace();

        }

   }

}

Jboss EJB 部署步驟 建立 remote interface-->home interface-->and bean implementation--> ejb-jar.xml-->jboss.xml--打包(package[jar cvf packageName.jar .])-->複製到jboss deploy目錄.

---------------------------------------------------------
原文 URL
http://blog.blogchina.com/refer.159508.html


---------------------------------------------------------
補充說明如下:

OS: windows 2000;
JDK: 1.5.0rc;
JBoss: 4.0


HelloWorld.jar
|--META-INF
|      |--jboss.xml
|      |--ejb-jar.xml
|      |--MANIFEST.MF (自動產生)
|--helloWorld
       |--RemoteHello.class
       |--jndi.properties
       |--HomeHello.class
       |--HelloBean.class

其中 測試檔案 MyTest.class 同目錄拷貝一份 jndi.properties 檔案過來.

jndi.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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.