Orion EJB tutorialThis tutorial is supposed to explain how to make simple enterprise java beans and how to deploy them in the Orion Application Server. First we will make a simple stateful session bean without using any of the GUI tools. Making your first Enterprise bean (a stateful session bean)
import java.util.*; import javax.ejb.*; import java.rmi.RemoteException; After these imports we put the definition of the interface. It should look like: public interface Cart extends EJBObject
public void addProduct(String product) throws RemoteException; public Collection getProducts() throws RemoteException;
import java.rmi.RemoteException; import javax.ejb.*;
public interface CartHome extends EJBHome
public Cart create() throws CreateException, RemoteException;
public class CartEJB implements SessionBean
private Collection products = new ArrayList();
� public void ejbCreate() Called when the instance of the bean gets created. � public void ejbActivate() Called when the instance of the bean gets activated. � public void ejbPassivate() Called when the instance of the bean gets passivated. � public void ejbRemove() Called when the instance of the bean gets invalidated. � public void setSessionContext(SessionContext context) Gives the bean access to its context/environment. In this simple bean we don�t care to implement these but just give them empty bodies.
public void addProduct(String product) { ����� products.add(product); }
public Collection getProducts() { ����� return products; }
Deploying your bean
<? xml version="1.0" ?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1. 2//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_2.dtd"> Building a simple client1. Now we will build a simple application client to test our bean. Start with creating a file called CartClient.java. 2. As usual we start with some imports: import java.rmi.RemoteException; import javax.ejb.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; 3. The client is a normal java application so it does not have to extend or implement any class or interface. Simply define it as: public class CartClient 4. This client will only contain one method, the main method: public static void main(String[] args) 5. The first thing you have to do to use an EJB is to get the naming context to be able to do lookups: Context context = new InitialContext(); 6. Now, look up the CartHome, narrow it to the right class and create a new Cart (and narrow it too): Object homeObject = context.lookup("MyCart"); CartHome home = (CartHome)PortableRemoteObject.narrow(homeObject, CartHome.class); Cart cart = (Cart)PortableRemoteObject.narrow(home.create(), Cart.class); 7. The cart object is now ready to be used. From now on we simply call the methods on the Cart in a normal fashion, for example we could add some products: cart.addProduct("Shirt"); cart.addProduct("Shoes"); ����������� or get the contents of the Cart: Collection products = cart.getProducts(); 8. Improvise and let your client do whatever you want. But before you compile you have to make sure the body of your main method is enclosed in a try-block to handle exceptions. Make sure you catch RemoteException (can happen on any remote invocation), NamingException (can occur when looking up the home) and CreateException (can happen when the Cart is created). 9. Now your client is done. An example client can be found here. 10. To be able to look up objects correctly using JNDI we must specify a few things in jndi.properties (you can put this file in the same directory as your client):
java.naming.factory.initial=com.evermind.server.rmi.RMIInitialContextFactory java.naming.provider.url=ormi://localhost java.naming.security.principal=admin java.naming.security.credentials=123
The file can also be found here.
11. Now compile your client and run it. Make sure orion/ejb.jar is in your classpath. You have now made and used your first EJB! Next lesson: A simple Entity bean (coming soon) |