`
jackle_liu
  • 浏览: 145854 次
文章分类
社区版块
存档分类
最新评论

jboss4.x下的第一个ejb2.0程序(1)---一个简单的无状态sessionBean

阅读更多
了解这个例子的前提是对jboss的熟悉,和对ejb的略微了解。
 
Hello.jar的文件组织是这样的:
1.三个类
 com.rox.Hello(SessionBean)
   com.rox.HelloHome(EJBHome,接口)
   com.rox.HelloWorld(EJBObject,接口)
2.配置文件
 META-INF
   ejb-jar.xml
       jboss.xml
直接把Hello.jar放入jboss的运行目录(deploy)中,配置成功.
以下是各个文件的内容:
Hello.java:
package com.rox;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
 * XDoclet-based session bean.  The class must be declared
 * public according to the EJB specification.
 *
 * To generate the EJB related files to this EJB:
 *  - Add Standard EJB module to XDoclet project properties
 *  - Customize XDoclet configuration for your appserver
 *  - Run XDoclet
 *
 * Below are the xdoclet-related tags needed for this EJB.
 *
 * @ejb.bean name="Hello"
 *           display-name="Name for Hello"
 *           description="Description for Hello"
 *           jndi-name="ejb/Hello"
 *           type="Stateless"
 *           view-type="remote"
 */
public class Hello implements SessionBean {
    /** The session context */
    private SessionContext context;
    public Hello() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * Set the associated session context. The container calls this method
     * after the instance creation.
     *
     * The enterprise bean instance should store the reference to the context
     * object in an instance variable.
     *
     * This method is called with no transaction context.
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */
    public void setSessionContext(SessionContext newContext)
        throws EJBException {
        context = newContext;
    }
    public void ejbCreate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       System.out.println("ejb create");
    }
    public void ejbRemove() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    /**
     * An example business method
     *
     * @ejb.interface-method view-type = "remote"
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */
    public String sayIt() throws EJBException {
        System.out.println("hello didi");
        return "hello didi.";
    }
}

HelloHome.java:
package com.rox;
/**
 * Home interface for Hello.
 * @xdoclet-generated at ${TODAY}
 * @copyright The XDoclet Team
 * @author XDoclet
 * @version ${version}
 */
public interface HelloHome
   extends javax.ejb.EJBHome
{
   public static final String INITIAL_NAME="ejb/HelloWorld";
   public static final String COMP_NAME="java:comp/env/ejb/HelloWorld";
   public static final String JNDI_NAME="ejb/HelloWorld";
   public com.rox.HelloWorld create()
      throws javax.ejb.CreateException,java.rmi.RemoteException;
}
 
HelloWorld.java:
 
package com.rox;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface HelloWorld extends EJBObject{
  public String sayIt() throws RemoteException;
}
 
ejb-jar.xml:
<!---->
<!---->http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar></ejb-jar>
   <description></description>
   <display-name></display-name>Generated by XDoclet
   <enterprise-beans></enterprise-beans>
      <!---->
      <session></session>
         <description></description>
         <display-name></display-name>Name for Hello
         <ejb-name></ejb-name>HelloWorld
         <home></home>com.rox.HelloHome
         <remote></remote>com.rox.HelloWorld
         <ejb-class></ejb-class>com.rox.Hello
         <session-type></session-type>Stateless
         <transaction-type> </transaction-type> Container
     
     <!---->
      <!---->
     <!---->
      <!---->
     <!---->
  
   <!---->
   <!---->
     <!---->
   <assembly-descriptor></assembly-descriptor>
     <!---->
   <!---->
     <!---->
   <!---->
     <!---->
   <!---->
   <!---->
     <!---->
   <!---->
     <!---->
  
 
jboss.xml:
<!---->
<!---->http://www.jboss.org/j2ee/dtd/jboss_2_4.dtd">
<jboss></jboss>
   <enterprise-beans></enterprise-beans>
     <!---->
      <session></session>
         <ejb-name></ejb-name>HelloWorld
         <jndi-name></jndi-name>ejb/HelloWorld
     
    <!---->
  
   <resource-managers></resource-managers>
  
  <!---->
 
只是配置完了,服务器没有报错,并不能说明问题.下面我们写一个测试的客户端程序EjbTest.java:
package com.rox;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.NamingException;
public class EjbTest {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        javax.naming.InitialContext initialContext = null;
        Properties environment = new Properties();
        environment.put(Context.PROVIDER_URL, "localhost:1099");
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        try {
            initialContext = new javax.naming.InitialContext(environment);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        }
        HelloHome helloHome = null;
        try {
            Object obj = initialContext.lookup("ejb/HelloWorld");
            helloHome = (HelloHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);
        } catch (ClassCastException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            HelloWorld hello = helloHome.create();
            System.out.println(hello.sayIt());
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CreateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
注意:由于上面environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
这句话中用到了org.jnp.interfaces.NamingContextFactory类,所以应当确保{JBOSS_HOME}/client下的jbossall-client.jar位于classpath中.
运行EjbTest,输出结果:hello didi.
稍后我将结合该例子研究log4j,据说很简单的
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics