The EJB Approach Deitel Santry Ch 14 j
The EJB Approach (Deitel. Santry, Ch. 14; j. Guru Tutorial) Ø Java servlets and JSP are one approach for implementing business and presentation logic multi-tier applications Ø Enterprise Java. Beans (EJBs) defines architecture for a § transactional § distributed object system based on § components, i. e. predefined software elements that can be assembled into an application; (this is typically done in a graphical programming and design environment). Ø Specification mandates a programming model, i. e. § conventions or protocols § set of classes and interfaces that make up EJB API Ø Provides bean developer and server vendors with a set of contracts that define a common development platform with the Ø Goals of § portability and § support of rich functionality Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 1
EJB Container Ø An EJB container provides the EJB run time and lifecycle management environment, i. e. the container hosts and manages EJBs Analogies: EJB container EJB Web server Servlet Web browser Applet Ø EJB container manages all aspects of enterprise bean at lifetime including § remote access to bean: EJB is isolated from direct access by client: client invokes method; container intercepts it and assures that § secuity; § persistence; are properly applied to all operations on bean § transactions; § concurrency – many beans are managed simultaneously; § access to everything bean needs: JDBC connections, another EJB, selfreferences, access to properties § pooling of resources and manages life cycle of EJB to reduce memory and processing, e. g. if bean is not used container puts it in pool to be used by another client or evicts bean from memory and brings it back only if/when needed Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 2
EJB interfaces and implementation EJBs consist of Ø remote interface – declares the business methods that EJB client can invoke; Ø home interface – provides create methods for new EJB instances, finder methods to find EJB instances, and remove methods for cleaning up EJB instances Ø EJB implementation – defines the methods in the remote and home interfaces Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 3
Bean, EJB Container, and client Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 4
Bean-EJB Container Interaction The enterprise bean interacts with its container through three mechanisms: (i) Every bean has an EJBContext object which is a reference to the container The EJB context interface provides methods for interacting with the container so that the bean can request information about environment, such as § client identity; § status of transaction § reference to bean itself (ii) Java Naming and Directory Interface (JNDI) – standard extension to access naming systems (e. g. LDAP, Net. Ware, file systems). Every bean has automatically access to a special naming system, called the Environment Naming Context (ENC), that is managed by container and accessed by beans using JNDI ENC allows access to resources such as JDBC connections, other enterprise beans, and properties specific to bean. (iii) Callbacks – methods called by container to notify bean to different events in its lifecycle, e. g. container is about to § activate bean § removed bean from memory § persist bean state in memory, § end transaction Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 5
Beans and Interfaces +interface(remote methods) +class Hotel. Clerk. EJB +interface(remote methods) Hotel. Clerk. Home Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 6
Home and Remote Interfaces + interface (remote methods) Customer Remote Interface represents business methods, e. g. accessor/mutators extends + interface javax. ejb. EJBObject extends + interface (remote methods) java. rmi. Remote Customer. Home Interface represents life cycle methods, e. g. create, destroy, find Fall 2003 extends + interface javax. ejb. EJBHome MET CS 667: 10. Servlets, JSPs, EJBs extends 7
EJB Interfaces and Implementations ØEJB remote interface must extend interface javax. ejb. EJBObject, e. g. public interface Customer extends EJBObject The EJB container creates a class that implements the remote interface. When a client invokes a remote method, the EJB container invokes the corresponding method of the implementation Customer. EJB of the remote interface Customer ØEJB home interface must extend interface javax. ejb. EJBHome, e. g. public interface Customer. Home extends EJBHome The EJB container provides an implementation of the home interface. Depending on the EJB type – session or entity bean – the container invokes the appropriate methods for creating, finding, and removing EJBs. ØEJB implementation defines the business methods; a session bean must implement interface javax. ejb. Session. Bean, e. g. public class Hotel. Clerk. EJB implements Session. Bean and an entity bean must implement interface javax. ejb. Entity. Bean, e. g. public class Customer. EJB implements Entity. Bean Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 8
Roles for J 2 EE implementers enterprise bean provider – implements Java classes for EJB; application assembler – constructs application components from EJBs implemented by the enterprise bean provider; deployer – deploys application to an EJB container, ensuring that all dependencies are met; EJB server provider – implement application server suitable for EJB container provider – deployment of J 2 EE applications; typically includes: • EJB container; • servlet container; and provides services such as • JNDI directories to locate data sources; • DB connection pooling; • integration with distributed systems; • resource management. Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 9
Entity EJBs: § represent persistent business objects such as are stored in a relational DB; § allow building an object based representations of data and behavior, and § provides interface to data that would be normally accessed by JDBC or other back -end API; There are two types of entity EJBs: container-managed persistence (CMP) – § container manages persistence automatically; § vendor tools are used to map the entity fields to the database no database access code written in the bean; § container synchronizes bean state with data base; § deployer must indicate what data fields are persistent, called container managed fields, and the corresponding persistent data sources when deploying the EJB; § easiest for developer to create, most difficult for EJB server to support restrictions on the types of the container managed field : container managed fields can be any primitive or serializable type that has corresponding types (columns) in the database directly or through Object Relational mapping (the latter is typically provided by the DB vendor) bean-managed persistence (BMP) – § bean includes database access code; § bean synchronizes its own state with database Fall 2003 more difficult for developer MET CSbut 667: great 10. Servlets, JSPs, 10 flexibility EJBs
Session EJBs – perform business logic for a client, e. g. access/manipulate DB. Session EJB do not represent a DB directly and are not persistent, i. e. instances are lost if EJB container crashes. There are two types of session EJBs: stateless – no state info is maintained between business method invocations. stateful – maintains conversational state information between business method invocations, e. g. maintain shopping cart info while customer browses on-line store; each time the customer adds item to shopping cart, item info (price, quantity, make, . etc. ) is stored. dedicated to one client, i. e. unlike stateless session beans clients do not share stateful session beans Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 11
Entity and Session EJB Class Hierarchy class Customer. EJB implements interface javax. ejb. Entity. Bean extends interface javax. ejb. Enterprise. Bean class Hotel. Clerk. EJB Fall 2003 implements interface javax. ejb. Session. Bean MET CS 667: 10. Servlets, JSPs, EJBs extends 12
Example: Remote Interfaces for Entity and Session EJB + interface (remote methods) Customer accessor/ mutator +get. Name(): Name +set. Name(Name name): void +get. Address(): Address +set. Address (Address address): void + interface (remote methods) Hotel. Clerk tasks / processes extends + interface javax. ejb. EJBObject +reserve. Room(Customer cust Room. Info ri , Date from, Date to): void +available. Rooms (Location loc Date from, Date to): Room. Info extends + interface java. rmi. Remote extends Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 13
Example: Implementation Classes for Entity and Session EJB + class Customer. EJB +get. Name() {…code. . } +set. Name(Name name) {…code. . } +get. Address() {…code. . } +set. Address (Address address) {…code. . } //Lifecycle methods //Callbacks + interface javax. ejb. Entity. Bean implements extends + interface javax. ejb. Enterprise. Bean + class extends Hotel. Clerk. EJB +reserve. Room(Customer cust, Room. Info ri , + interface Date from, Date to): {…code. . } javax. ejb. Session. Bean +available. Rooms (Location loc, Date from, implements Date to) {…code. . } //Lifecycle methods from Home interface //Callbacks Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 14
Notes on Bean Classes: EJB implementation classes are not required to implement the remote and home interface In fact this is discouraged because their base types of the remote and home inerface, EJBRemote and EJBHome, define many other methods that are automatically implemented by the container BUT: EJB class provides implementation for the business, lifecycle and callback methods Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 15
Example: Home Interface – creating, destroying, locating beans + interface (remote methods) Customer. Home +create(Integer id): Customer; +create(Integer id, Name name): Customer; //other create methods +find. By. Primary. Key (Integer id): Customer +find. By. Zip. Code (int zip. Code): Customer //other find methods Fall 2003 extends javax. ejb. EJBHome MET CS 667: 10. Servlets, JSPs, EJBs extends java. rmi. Remote 16
+ class Example: Customer – an Entity Bean implements Customer. Home customer. ID: int my. Address: Address ; my. Name: Name ; my. Credit. Card: Credit. Card ; // BUSINESS METHODS as in slide 25 +get. Name() { return my. Name; }: Name // CREATION METHODS +ejb. Create(Integer id) { customer. ID = id. int. Value(); return null; }: Customer + ejb. Post. Create(Integer id) { }: void + ejb. Create(Integer id, Name name) { my. Name = name; return ejb. Create(id); }: Customer + ejb. Post. Create(Integer id, Name name) { }: void //CALLBACKS + set. Entity. Context(Entity. Context cntx) { }: void + ejb. Load() { } void + ejb. Store() { } void + ejb. Activate() { } void + ejb. Passivate() { } void Fall 2003 MET CS 667: 10. Servlets, JSPs, + ejb. Remove(){ }: void EJBs Entity. Bean extends Enterprise. Bean 17
Notes on Lifecycle Methods: § Invoking create results in a new entity and a new record in the database; § The home interface can have zero or more create methods with different arguments; § Each create method must have a corresponding ejb. Create and ejb. Post. Create method in the bean implementation class; § When create is invoked, the container delegates the call to ejb. Create of the bean instance, and ejb. Create initializes the instance state before record is inserte in database (here customer. Id and Name); in the case of CMP, when the ejb. Create is finished the container reads the container managed fields and inserts a new record into the database according to the mapping of data to columns § the entity bean does not technically exist until its data is inserted into database; after that the bean can access its own primary key and remote references; § if a bean needs to access its primary key and remote references after it exists but before using any business methods it can do it through the ejb. Post. Create method that allows any post create processing before serving client requests § find methods query EJB server to locate specific entity beans; there is no standard query language and implementation is vendor specific; in CMP the find methods are not implemented with matching methods in the bean class; the deployer uses vendor specific tools to tell the container how they behave; § The EJBHome interface defines a number of other methods that Customer. Home inherits, among them a remove() method for destroying the bean Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 18
Client Accesses EJB Through Home and Remote Interfaces Customer. Home home = // Get a reference to the Customer. Home object Customer customer = home. create(new Integer(777)); Name name = new Name("Moll", "Flanders"); customer. set. Name(name); Address addr = customer. get. Address(); addr. zip = "02215"; customer. set. Address(addr); //change the zip code Enumeration enum. Of. Customers = home. find. By. Zip(02215); Customer customer 2 = home. find. By. Primary. Key( new Integer(777)); Name name 2 = customer 2. get. Name(); System. out. println(name); // output is " Moll Flanders " Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 19
Example: Credit. Service. EJB – a Stateless Session EJB + interface (remote methods) extends + interface Credit. Service javax. ejb. EJBObject +verify(Credit. Card card, double amount): void +charge(Credit. Card card, double amount): void + interface java. rmi. Remote + interface(remote methods) + interface Credit. Service. Home javax. ejb. EJBHome +create(): Credit. Service + class Credit. Service. EJB acme. URL: URL ; //URL resource factory acme. Con: Connection; //connection to credit //service obtained through acme. URL +ejb. Create() {. . code. . } +verify(Credit. Card card, double amount) {. . code. . }: void +charge(Credit. Card card, double amount) {. . code. . }: void //other methods Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs + interface javax. ejb. Session. Bean implements extends + interface javax. ejb. Enterprise. Bean 20
J 2 EE Platform Overview (Figure 1. 1 from Designing Enterprise Applications Using the J 2 EE platform) Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 21
J 2 EE Application Scenarios (Figure 1. 2 from Designing Enterprise Applications Using the J 2 EE platform) Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 22
Multitier Application Scenario (Figure 1. 3 from Designing Enterprise Applications Using the J 2 EE platform) Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 23
Stand-Alone Client Scenario (Figure 1. 3 from Designing Enterprise Applications Using the J 2 EE platform) Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 24
Stand-Alone Client Scenario (Figure 1. 4 from Designing Enterprise Applications Using the J 2 EE platform) Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 25
Stand-Alone Types: Direct Access (Figure 1. 5 from Designing Enterprise Applications Using the J 2 EE platform) Client accesses EJBs directly; RMI-IIOP used Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 26
Stand-Alone Client Types (continued) § Stand-alone clients, implemented in the Java language or another programming language, consuming dynamic Web content (usually XML data messages). ØWeb container essentially handles XML transformations and provides Web connectivity to clients. ØPresentation logic occurs in the client tier. ØThe Web tier handles business logic and may directly access EIS resources. Ø Ideally, business logic is implemented as enterprise beans to take advantage of the rich enterprise beans component model. § Stand-alone Java application clients accessing enterprise information system resources directly using JDBC or Connectors. Øpresentation and business logic are co-located on the client platform and may in fact be tightly integrated into a single application. Ø classic two-tier client-server architecture, with its associated distribution, maintenance, and scalability issues. Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 27
Web Centric Application Scenario (Figure 1. 6 from Designing Enterprise Applications Using the J 2 EE platform) . Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 28
Web Container in a Three-Tier Scenario (Figure 1. 7 from Designing Enterprise Applications Using the J 2 EE platform) . Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 29
Business-to-Business Scenario (Figure 1. 8 from Designing Enterprise Applications Using the J 2 EE platform) . Fall 2003 MET CS 667: 10. Servlets, JSPs, EJBs 30
References EJB Deitel, Santry: Advanced Java 2 Platforam –How to Program. Prentice Hall, 2002. Ch. 14. This book has large scope, many examples, can serve as a quick reference, but is not the best for teaching/learning the material. j. Guru Short Course: Enterprise Java. Beans Fundamentals (2000): good for a quick overview and acquiring a taste of coding EJBs http: //developer. java. sun. com/developer/online. Training/EJBIntro/ Designing Enterprise Applications Using the J 2 EE platform: Java Blueprints online book on designing distributed applications; focus is on how to make architectural decisions rather than on code http: //java. sun. com/blueprints/guidelines/designing_enterprise_applications_2 e /index. html Developing Enterprise Applications Using the J 2 EE platform: - detailed but basis tutorial to J 2 EE http: //developer. java. sun. com/developer/online. Training/J 2 EE/Intro 2/j 2 ee. html The J 2 EE Tutorial (2002): the updated Sun tutorial. http: //java. sun. com/j 2 ee/tutorial/1_3 -fcs/ Enterprise Java. Beans 2. 1 Documentation: what you need to read if you have a substantial project: http: //java. sun. com/products/ejb/javadoc-2_1 -pfd/ Sun Microsystems, Inc. EJB Home page: http: //java. sun. com/products/ejb/ Fall. Microsystems, 2003 CS 667: 10. http: //java. sun. com/j 2 ee/ Servlets, JSPs, Sun Inc. J 2 EEMET Home page: EJBs 31
- Slides: 31