210 likes | 339 Views
Enterprise JavaBeans Umer Farooq. February 25, 2002. CS6704: Design Patterns & Component Frameworks. Enterprise JavaBeans (EJB).
E N D
Enterprise JavaBeansUmer Farooq February 25, 2002 CS6704: Design Patterns & Component Frameworks
Enterprise JavaBeans (EJB) “Enterprise JavaBeans is the server-side component architecture for the J2EE platform. EJB enables rapid and simplified development of distributed, transactional, secure and portable Java applications.”
Agenda • Overview of J2EE platform • EJB and J2EE? • Types of EJB • Life Cycles of EJB • Client access to EJB • Code example of an EJB • Applications using EJB • Comparison of EJB with Microsoft’s technology! • Comments/Questions/Discussion
J2EE Platform • Approach to developing highly scalable internet or intranet based applications • Transaction management, life-cycle management, resource pooling automatically handled • J2EE application model encapsulates the layers of functionality in specific types of components
Types of EJB • Session: Performs a task for a client • Entity: Represents a business entity object that exists in persistent storage • Message-Driven: Acts as a listener for the Java Message Service API, processing messages asynchronously • Examples?
Life Cycles Stateful Session Bean
Life Cycles Stateless Session Bean
Life Cycles Entity Bean
Life Cycles Message-Driven Bean
Client access to EJB • Client access only through interfaces • Remote access • May run on a different JVM • Web component, J2EE client, EJB, etc. • Location is transparent
A Session Bean Example • The CartEJB session bean represents a shopping cart in an online bookstore: • Session bean class (CartBean) • Home interface (CartHome) • Remote interface (Cart) • Two helper classes: BookException and IdVerifier
CartBean.java import java.util.*; import javax.ejb.*; public class CartBean implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) throw new CreateException("Null person not allowed."); else customerName = person; customerId = "0"; contents = new Vector(); } public void ejbCreate(String person, String id) throws CreateException { if (person == null) throw new CreateException("Null person not allowed."); else customerName = person; IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) customerId = id; else throw new CreateException("Invalid id: "+ id); contents = new Vector(); }
CartBean.java (cont) public void addBook(String title) { contents.addElement(title); } public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) throw new BookException(title + "not in cart."); } public Vector getContents() { return contents; } public CartBean() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }
CartHome.java import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; Cart create(String person, String id) throws RemoteException, CreateException; }
Cart.java import java.util.*; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; public Vector getContents() throws RemoteException; }
Client code import java.util.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class CartClient { public static void main(String[] args) { try { Context initial = new InitialContext(); Object objref = initial.lookup("MyCart"); CartHome home = (CartHome)PortableRemoteObject.narrow (objref, CartHome.class); Cart shoppingCart = home.create("Duke DeEarl","123"); shoppingCart.addBook("The Martian Chronicles"); shoppingCart.removeBook("Alice in Wonderland"); shoppingCart.remove(); } catch (BookException ex) { System.err.println("Caught a BookException: " + ex.getMessage()); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); } } }
Industry Applications for EJB • Ford Financial Saves Money, Achieves Business Goals With Sun – February 20, 2002 • Amazon.com's ObjectStore Deployment Ensures Best-in-Class Experience for Customers And Merchants – February 19, 2002 • Borland Wins Again With Web Services Solution for Linux – February 12, 2002 • PointBase Demonstrates World's First Enterprise Data Synchronization Across Multiple Devices And Networks – February 12, 2002 • Over 40 licencees (who can ship J2EE products) including Nokia, Oracle, IBM, NEC, Compaq, BEA, etc.
Comparison with Microsoft • 78 percent viewed J2EE (Java 2 Enterprise Edition) server software as the most effective platform for building and deploying Web services to Microsoft’s .Net (http://www.infoworld.com/articles/hn/xml/01/12/21/011221hnjavasurvey.xml) • What is Microsoft’s corresponding technology? • Read handout!
Discussion • When to use which EJB? • When to use local and remote interfaces? • What would you choose: Sun or Microsoft? (Remember Windows had crashed on the last day of your project submission and you lost it all) Thank you (ufarooq@vt.edu)
References • http://www.java.sun.com/j2ee\ • http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/J2eeTutorialTOC.html • Special Edition Using EJB 2.0 by Dan Chuck Cavaness and Brian Keeton