分享

GlassFish下使用EJB的简单例子

 漂泊的燕子 2011-06-14
使用工具:MyEclipse9,GlassFish3
应用:单独的java程序访问远程EJB

一、Client
1. create java project EJBClient;
  Add gf-client.jar  (\glassfish3\glassfish\lib) to java build path
2. create client class FirstEjbTest:
   public static void main(String[] args) {
  Context initial;
  try {
   System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
   System.setProperty("java.naming.provider.url","http://localhost:8080");
   initial = new InitialContext();  
   ASessionBeanHome home = (ASessionBeanHome)initial.lookup("jndi/ASessionBean");
   ASessionBeanRemote remote = home.create();
   remote.sayHello();
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (CreateException e){
   e.printStackTrace();
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
二、EJB服务端
1. create EJB2.1 project EJB21Proj;
2. create session bean ASessionBean;
      public class ASessionBean implements SessionBean {
 /** The session context */
 private SessionContext context;
 public ASessionBean() {
  // TODO Auto-generated constructor 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 ejbRemove() throws EJBException, RemoteException {
  // TODO Auto-generated method 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;
 }
 /**
  * An ejbCreate method as required by the EJB specification.
  *
  * The container calls the instance?s <code>ejbCreate</code> method whose
  * signature matches the signature of the <code>create</code> method invoked
  * by the client. The input parameters sent from the client are passed to
  * the <code>ejbCreate</code> method. Each session bean class must have at
  * least one <code>ejbCreate</code> method. The number and signatures
  * of a session bean?s <code>create</code> methods are specific to each
  * session bean class.
  *
  * @throws CreateException Thrown if method fails due to system-level error.
  *
  * @ejb.create-method
  *
  */
 public void ejbCreate() throws CreateException {
  // TODO Add ejbCreate method implementation
 }
 /**
  * An example business method
  *
  * @ejb.interface-method view-type = "both"
  *
  * @throws EJBException Thrown if method fails due to system-level error.
  */
 public void replaceWithRealBusinessMethod() throws EJBException {
  // rename and start putting your business logic here
 }
 
 public void sayHello(){
  System.out.println("Hello EJB2.1!");
 }
}
2. create home interface ASessionBeanhome;
      public interface ASessionBeanHome extends EJBHome {
 public ASessionBeanRemote create() throws RemoteException,CreateException;
}
3. create remote interface ASessionBeanRemote;
public interface ASessionBeanRemote extends EJBObject {
 public void sayHello() throws RemoteException;
}
4. META-INF/ejb-jar.xml
 <?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/j2ee http://java./xml/ns/j2ee/ejb-jar_2_1.xsd">
 <display-name>EjbProj21</display-name>
 <enterprise-beans>
  <session>
   <ejb-name>MyFirstSessionBean</ejb-name>
   <home>ASessionBeanHome</home>
   <remote>ASessionBeanRemote</remote>
   <ejb-class>ASessionBean</ejb-class>
   <session-type>Stateless</session-type>
   <transaction-type>Bean</transaction-type>   
  </session>  
 </enterprise-beans>
</ejb-jar>

5. META-INF/sun-ejb-jar.xml(for GlassFish)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Application Server 8.1 EJB 2.1//EN' 'http://www./software/appserver/dtds/sun-ejb-jar_2_1-1.dtd'>
<sun-ejb-jar>
 <enterprise-beans>
  <ejb>
   <ejb-name>MyFirstSessionBean</ejb-name>
   <jndi-name>jndi/ASessionBean</jndi-name>
  </ejb>
 </enterprise-beans> 
</sun-ejb-jar>

三、Deploy project Ejb21Proj to GlassFish server and start up the server
四、run FirstEjbTest.main()方法
五、Console output
INFO: Hello EJB2.1!


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多