[转] jboss 4.0 ejb 部署

类别:Java 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it12619.htm