Enterprise Java Beans Umer Farooq February 25 2002
Enterprise Java. Beans Umer Farooq February 25, 2002 CS 6704: Design Patterns & Component Frameworks
Enterprise Java. Beans (EJB) “Enterprise Java. Beans is the server-side component architecture for the J 2 EE platform. EJB enables rapid and simplified development of distributed, transactional, secure and portable Java applications. ”
Agenda ¨ Overview of J 2 EE platform ¨ EJB and J 2 EE? ¨ 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
J 2 EE Platform ¨ Approach to developing highly scalable internet or intranet based applications ¨ Transaction management, life-cycle management, resource pooling automatically handled ¨ J 2 EE application model encapsulates the layers of functionality in specific types of components
J 2 EE Application Model
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 q Web component, J 2 EE client, EJB, etc. q Location is transparent q
A Session Bean Example ¨ The Cart. EJB session bean represents a shopping cart in an online bookstore: Session bean class (Cart. Bean) q Home interface (Cart. Home) q Remote interface (Cart) q Two helper classes: Book. Exception and Id. Verifier q
Cart. Bean. java import java. util. *; import javax. ejb. *; public class Cart. Bean implements Session. Bean { String customer. Name; String customer. Id; Vector contents; public void ejb. Create(String person) throws Create. Exception { if (person == null) throw new Create. Exception("Null person not allowed. "); else customer. Name = person; customer. Id = "0"; contents = new Vector(); } public void ejb. Create(String person, String id) throws Create. Exception { if (person == null) throw new Create. Exception("Null person not allowed. "); else customer. Name = person; Id. Verifier id. Checker = new Id. Verifier(); if (id. Checker. validate(id)) customer. Id = id; else throw new Create. Exception("Invalid id: "+ id); contents = new Vector(); }
Cart. Bean. java (cont) public void add. Book(String title) { contents. add. Element(title); } public void remove. Book(String title) throws Book. Exception { boolean result = contents. remove. Element(title); if (result == false) throw new Book. Exception(title + "not in cart. "); } public Vector get. Contents() { return contents; } public Cart. Bean() {} public void ejb. Remove() {} public void ejb. Activate() {} public void ejb. Passivate() {} public void set. Session. Context(Session. Context sc) {} }
Cart. Home. java import java. io. Serializable; import java. rmi. Remote. Exception; import javax. ejb. Create. Exception; import javax. ejb. EJBHome; public interface Cart. Home extends EJBHome { Cart create(String person) throws Remote. Exception, Create. Exception; Cart create(String person, String id) throws Remote. Exception, Create. Exception; }
Cart. java import java. util. *; import javax. ejb. EJBObject; import java. rmi. Remote. Exception; public interface Cart extends EJBObject { public void add. Book(String title) throws Remote. Exception; public void remove. Book(String title) throws Book. Exception, Remote. Exception; public Vector get. Contents() throws Remote. Exception; }
Client code import java. util. *; import javax. naming. Context; import javax. naming. Initial. Context; import javax. rmi. Portable. Remote. Object; public class Cart. Client { public static void main(String[] args) { try { Context initial = new Initial. Context(); Object objref = initial. lookup("My. Cart"); Cart. Home home = (Cart. Home)Portable. Remote. Object. narrow (objref, Cart. Home. class); Cart shopping. Cart = home. create("Duke De. Earl", "123"); shopping. Cart. add. Book("The Martian Chronicles"); shopping. Cart. remove. Book("Alice in Wonderland"); shopping. Cart. remove(); } catch (Book. Exception ex) { System. err. println("Caught a Book. Exception: " + ex. get. Message()); } 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 Object. Store Deployment Ensures Bestin-Class Experience for Customers And Merchants – February 19, 2002 Borland Wins Again With Web Services Solution for Linux – February 12, 2002 Point. Base Demonstrates World's First Enterprise Data Synchronization Across Multiple Devices And Networks – February 12, 2002 Over 40 licencees (who can ship J 2 EE products) including Nokia, Oracle, IBM, NEC, Compaq, BEA, etc.
Comparison with Microsoft ¨ 78 percent viewed J 2 EE (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/01122 1 hnjavasurvey. 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/j 2 ee ¨ http: //java. sun. com/j 2 ee/tutorial/1_3 - fcs/doc/J 2 ee. Tutorial. TOC. html ¨ Special Edition Using EJB 2. 0 by Dan Chuck Cavaness and Brian Keeton
- Slides: 21