构建“Hello,World!”远程接口 package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; import java.rmi.RemoteException; import java.rmi.Remote; /** * This is the Hellobeans remote interface. * * This interface is what clients Operate on when * they interact with EJB objects. The container * vendor will implement this interface; the * implemented object is the EJB object, which * delegates invocations to the actual beans. */ public interface Hello extends EJBObject { /** * The one method - hello - returns a greeting to the client. */ public String hello() throws java.rmi.RemoteException; } Source 4.1 Hello.java.
package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; import java.rmi.RemoteException; /** * This is the home interface for Hellobeans. This interface * is implemented by the EJB Server′s glue-code tools - the * implemented object is called the Home Object and serves * as a factory for EJB Objects. * * One create() method is in this Home Interface, which * corresponds to the ejbCreate() method in Hellobeans. */ public interface HelloHome extends EJBHome { /* * This method creates the EJB Object. * * @return The newly created EJB Object. */ Hello create() throws RemoteException, CreateException; } creat方法抛出了a java.rmi.RemoteException和aavax.ejb.CreateException.异常。
package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; import javax.naming.*; import java.rmi.*; import java.util.Properties; /** * This class is an example of client code that invokes * methods on a simple stateless session beans. */ public class HelloClient { public static void main(String[] args) { try { /* * Get System properties for JNDI initialization */ Properties props = System.getProperties(); /* * Form an initial context */ Context ctx = new InitialContext(props); /* * Get a reference to the home object * (the factory for EJB objects) */ HelloHome home = (HelloHome) ctx.lookup("HelloHome"); /* * Use the factory to create the EJB Object */ Hello hello = home.create(); /* * Call the hello() method, and print it */ System.out.println(hello.hello()); /* * Done with EJB Object, so remove it */ hello.remove(); } catch (Exception e) { e.printStackTrace(); } } }