1 / 21

Enterprise JavaBeans Umer Farooq

Enterprise JavaBeans Umer Farooq. February 25, 2002. CS6704: Design Patterns & Component Frameworks. Enterprise JavaBeans (EJB).

jafari
Download Presentation

Enterprise JavaBeans Umer Farooq

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enterprise JavaBeansUmer Farooq February 25, 2002 CS6704: Design Patterns & Component Frameworks

  2. 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.”

  3. 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

  4. 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

  5. J2EE Application Model

  6. 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?

  7. Life Cycles Stateful Session Bean

  8. Life Cycles Stateless Session Bean

  9. Life Cycles Entity Bean

  10. Life Cycles Message-Driven Bean

  11. 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

  12. 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

  13. 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(); }

  14. 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) {} }

  15. 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; }

  16. 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; }

  17. 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!"); } } }

  18. 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.

  19. 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!

  20. 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)

  21. 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

More Related