jboss4.x下的第一个ejb2.0程序(5)---本地EJB

EJB2.0规范中引入了本地EJB的概念,传统的EJB是走得rmi技术,是通过网络协议的( 通过网卡).如果采用本地EJB就不会多走一步网络,而是直接调用,效率会快很多.
     实际应用中,往往EntityBean都是放在本地,SessionBean是分布式的.所以EntityBean采用本地EJB.
     我们依然是针对前面的例子进行改动.同时我们讲在这里增加一个查询方法.
  首先是增加1个类,两个接口:
HelloLocalEntity.java:
package com.rox;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;
public abstract class HelloLocalEntity implements EntityBean {
    /** The entity context */
    private EntityContext context;
    public abstract Integer getHelloNo();
    public abstract void setHelloNo(Integer s);
    public abstract String getHelloName();
    public abstract void setHelloName(String s);
    public HelloLocalEntity() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Integer ejbCreate(Integer HelloNo,String HelloName) throws CreateException {
        setHelloNo(HelloNo);
        setHelloName(HelloName);
        return null;
    }
    public void ejbPostCreate(Integer HelloNo,String HelloName) throws CreateException {
    }
    public void setEntityContext(EntityContext newContext) throws EJBException {
        context = newContext;
    }
    public void unsetEntityContext() throws EJBException {
        context = null;
    }
    public void ejbRemove()
        throws RemoveException,
        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
    }
    public void ejbLoad() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    public void ejbStore() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
  
 
HelloLocalEntityHome.java:
package com.rox;
import java.util.Collection;
import javax.ejb.FinderException;
public interface HelloLocalEntityHome
   extends javax.ejb.EJBLocalHome
{
   public com.rox.HelloLocalEntityInterface create(Integer HelloNo,String HelloName)
   throws javax.ejb.CreateException;
  
   public com.rox.HelloLocalEntityInterface findByPrimaryKey(Integer pk)
      throws javax.ejb.FinderException;
  
   public Collection findAllEntitys() throws FinderException;//对应ejb-jar中<query-method></query-method>的定义.
}
 
HelloLocalEntityInterface.java:
package com.rox;
import java.util.Collection;
public interface HelloLocalEntityInterface
   extends javax.ejb.EJBLocalObject
{
    public String getHelloName();
    public void setHelloName(String s);
    public Integer getHelloNo();
    public void setHelloNo(Integer i);
}
 
然后是配置文件: 
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>
    
   
   <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
   <ejb-ref></ejb-ref>
    <ejb-ref-name></ejb-ref-name>com.rox.HelloEntityHome
    <ejb-ref-type></ejb-ref-type>Entity
    <home></home>com.rox.HelloEntityHome
    <remote></remote>com.rox.HelloEntityInterface
    <ejb-link></ejb-link>HelloEntityInterface
   
   <ejb-local-ref></ejb-local-ref>
    <ejb-ref-name></ejb-ref-name>com.rox.HelloLocalEntityHome
    <ejb-ref-type></ejb-ref-type>Entity
    <local-home></local-home>com.rox.HelloLocalEntityHome
    <local></local>com.rox.HelloLocalEntityInterface
    <ejb-link></ejb-link>HelloLocalEntityInterface
   
  
  <entity></entity>
   <description></description>
    
   
   <ejb-name></ejb-name>HelloEntityInterface
   <home></home>com.rox.HelloEntityHome
   <remote></remote>com.rox.HelloEntityInterface
   <ejb-class></ejb-class>com.rox.HelloEntity
    <persistence-type></persistence-type> Container
    <prim-key-class></prim-key-class> java.lang.Integer
   <reentrant></reentrant>False
   <cmp-version></cmp-version>2.x
   <abstract-schema-name></abstract-schema-name>HelloEntity
            <cmp-field></cmp-field>
            <field-name></field-name>helloNo
           
            <cmp-field></cmp-field>
            <field-name></field-name>helloName
           
            <primkey-field></primkey-field> helloNo
   <!---->
   <query></query>
    <query-method></query-method>
     <method-name></method-name>findAllEntitys
     <method-params></method-params>
    
    <ejb-ql></ejb-ql>SELECT OBJECT(a) FROM HelloLocalEntity AS a
   
  
  <entity></entity>
   <description></description>
    
   
   <display-name></display-name>Name for HelloLocalEntity
   <ejb-name></ejb-name>HelloLocalEntityInterface
   <local-home></local-home>com.rox.HelloLocalEntityHome
   <local></local>com.rox.HelloLocalEntityInterface
   <ejb-class></ejb-class>com.rox.HelloLocalEntity
    <persistence-type></persistence-type> Container
    <prim-key-class></prim-key-class> java.lang.Integer
   <reentrant></reentrant>False
   <cmp-version></cmp-version>2.x
   <abstract-schema-name></abstract-schema-name>HelloLocalEntity
            <cmp-field></cmp-field>
            <field-name></field-name>helloNo
           
            <cmp-field></cmp-field>
            <field-name></field-name>helloName
           
            <primkey-field></primkey-field> helloNo
   <query></query>
    <query-method></query-method>
     <method-name></method-name>findAllEntitys
     <method-params></method-params>
    
    <ejb-ql></ejb-ql>SELECT OBJECT(a) FROM HelloLocalEntity AS a
   
   <!---->
  

 

 <assembly-descriptor></assembly-descriptor>
 
红色部分是相对于之前的ejb-jar.xml增加部分.

jboss.xml:
<!---->
<!---->http://www.jboss.org/j2ee/dtd/jboss_2_4.dtd">
<jboss></jboss>
   <enterprise-beans></enterprise-beans>

      <entity></entity>
         <ejb-name></ejb-name>HelloEntityInterface
         <jndi-name></jndi-name>ejb/HelloEntity
     
     
      <entity></entity>
         <ejb-name></ejb-name>HelloLocalEntityInterface
         <jndi-name></jndi-name>ejb/HelloLocalEntity
     
     
      <session></session>
         <ejb-name></ejb-name>HelloWorld
         <jndi-name></jndi-name>ejb/HelloWorld
     
   
  
   <resource-managers></resource-managers>
  
 
jbosscmp-jdbc.xml:
<!---->
<!---->http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">
<jbosscmp-jdbc></jbosscmp-jdbc>
  <enterprise-beans></enterprise-beans>
    <entity></entity>
      <ejb-name></ejb-name>HelloEntityInterface
      <table-name> </table-name> HELLO
      <cmp-field></cmp-field>
        <field-name></field-name>helloNo
        <column-name></column-name>HELLO_NO
     
      <cmp-field></cmp-field>
        <field-name></field-name>helloName
        <column-name></column-name>HELLO_NAME
     
   
    <entity></entity>
      <ejb-name></ejb-name>HelloLocalEntityInterface
      <table-name> </table-name> HELLO
      <cmp-field></cmp-field>
        <field-name></field-name>helloNo
        <column-name></column-name>HELLO_NO
     
      <cmp-field></cmp-field>
        <field-name></field-name>helloName
        <column-name></column-name>HELLO_NAME
     
   
 
 
做完这些还要修改Hello.java:
package com.rox;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import org.apache.log4j.Logger;
public class Hello implements SessionBean {

    private SessionContext context;
    InitialContext initialContext = null;
    Object obj = null;
    Object localObj = null;
    HelloLocalEntityHome helloLocalEntityHome = null;
    HelloEntityHome helloEntityHome = null;
    private  final Logger log = Logger.getLogger(Hello.class);
    public Hello() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void setSessionContext(SessionContext newContext)
        throws EJBException {
        context = newContext;
        log.info("set SessionContext");
    }
    public void ejbCreate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
        log.info("ejb create");
        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 InitialContext();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            obj = initialContext.lookup("java:comp/env/"+HelloEntityHome.class.getName());
            localObj = initialContext.lookup("java:comp/env/"+HelloLocalEntityHome.class.getName());
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        helloEntityHome = (HelloEntityHome)PortableRemoteObject.narrow(obj,HelloEntityHome.class);
        helloLocalEntityHome = (HelloLocalEntityHome)localObj;
        log.info("helloEntityHome:"+helloEntityHome);
        log.info("helloLocalEntityHome"+helloLocalEntityHome);
    }
    public void ejbRemove() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
        log.info("ejb remove");
    }
    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
        log.info("ejb activate");
    }
    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
        log.info("ejb passivate");
    }
    public Collection getAllLocalEntity()
    {
        Collection originalCol = null;
        Collection newCol = new ArrayList();
        try {
            originalCol = helloLocalEntityHome.findAllEntitys();
        } catch (FinderException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Iterator it = originalCol.iterator();
        while(it.hasNext())
        {
            HelloLocalEntityInterface local = (HelloLocalEntityInterface)it.next();
            HelloObject obj = new HelloObject();
            obj.setHelloName(local.getHelloName());
            obj.setHelloNo(local.getHelloNo());
            newCol.add(obj);
        }
        return newCol;
    }
    public String sayIt() throws EJBException {
       
        HelloEntityInterface helloEntity = null;
        try {
          
            log.info("helloEntityHome:"+helloEntityHome);
            //helloEntityHome.create(1,"mysql first try");
            helloEntity = helloEntityHome.findByPrimaryKey(1);
            log.info("helloEntity:"+helloEntity);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FinderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        try {
            return helloEntity.getHello();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
           
            e.printStackTrace();
            return "sorry, exception";
        }
    }
}
注意红色部分的方法,HelloObject是一个pojo:
package com.rox;
import java.io.Serializable;
public class HelloObject implements Serializable{
   private String HelloName;
   private int HelloNo;
public String getHelloName() {
    return HelloName;
}
public void setHelloName(String helloName) {
    HelloName = helloName;
}
public int getHelloNo() {
    return HelloNo;
}
public void setHelloNo(int helloNo) {
    HelloNo = helloNo;
}
 
}
最后在EjbTest.java中加上如下语句就可以看到结果集了
            Collection localResultCol = hello.getAllLocalEntity();
            System.out.println(localResultCol.size());
            Iterator localResultIter = localResultCol.iterator();
            while(localResultIter.hasNext())
            {
                HelloObject helloLocal = (HelloObject)localResultIter.next();
                System.out.println(helloLocal.getHelloNo()+"\t"+helloLocal.getHelloName());
            }

你可能感兴趣的:(mysql,jboss,jdbc,ejb,网络应用)