短裙黑色:GlassFish下使用EJB的简单例子

来源:百度文库 编辑:中财网 时间:2024/05/04 09:26:31
使用工具: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 ejbCreate method whose
  * signature matches the signature of the create method invoked
  * by the client. The input parameters sent from the client are passed to
  * the ejbCreate method. Each session bean class must have at
  * least one ejbCreate method. The number and signatures
  * of a session bean?s create 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 

 EjbProj21
 
  
   MyFirstSessionBean
   ASessionBeanHome
   ASessionBeanRemote
   ASessionBean
   Stateless
   Bean   
  
  
 



5. META-INF/sun-ejb-jar.xml(for GlassFish)


 
  
   MyFirstSessionBean
   jndi/ASessionBean
  

 
 

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