Presentation is loading. Please wait.

Presentation is loading. Please wait.

©1996-2000 jGuru.com Enterprise JavaBeans Fundamentals.

Similar presentations


Presentation on theme: "©1996-2000 jGuru.com Enterprise JavaBeans Fundamentals."— Presentation transcript:

1 ©1996-2000 jGuru.com Enterprise JavaBeans Fundamentals

2 2 Agenda Enterprise JavaBeans defined EJB and Distributed Computing EJB Architecture Entity beans Session bean Deployment EJB Clients

3 ©1996-2000 jGuru.com Enterprise JavaBeans: Defined

4 4 What is Enterprise JavaBeans The Enterprise JavaBeans architecture is a component architecture for the development and deployment of object-oriented distributed enterprise-level applications. Applications written using the Enterprise JavaBeans architecture is scalable, transactional and multi-user secure. These applications may be written once, and deployed on any server platform that supports the Enterprise JavaBeans specification Sun Microsystems Enterprise JavaBeans™ Specification, v1.1, Copyright 1999 by Sun Microsystems, Inc.

5 5 In English Please! Enterprise JavaBeans is a specification –(a piece of paper) not a product –As is XML, CORBA, TCP/IP Enterprise JavaBeans is Java based –Defined by Sun Microsystems –Applications are written in Java Enterprise JavaBeans is distributed objects –Similar to CORBA | Java RMI (JRMP) | Microsoft MTS Enterprise JavaBeans is components –Similar to regular JavaBeans (similar but not the same) –Similar to COM/DCOM/COM+ Enterprise JavaBeans products are Transactional Monitors –Similar to CICS | TUXEDO –A complete environment for a distributed object

6 6 WORA Write-Once, Run Anywhere within Middleware Middleware provides all services –Instance management, transactions, concurrency, persistence Beans stay simple!

7 7 EJB Spec Spec developed by Sun and dozens of competing vendors –IBM, Oracle, BEA, WebLogic,... –Interoperable -- a miracle! Why did they cooperate? –Common Enemy phenomenon

8 8 EJB Advantages With EJB, you can write a business object and easily make it –Persistent –Distributed –Transactional –Secure –Multithreaded

9 9 EJB Advantages Can also provide wrappers for legacy apps –Mainframes –Native code –Proprietary code –Corporate databases

10 10 The benefits Client scalability User performance Reusability Time to market Security Preservation of Heritage data and systems

11 11 What’s in a name? Enterprise Java Beans has absolutely nothing to do with JavaBeans –Except that both are Java-based component architectures EJB is server-side, JB is client-side –EJB has no GUI, JB usually has GUI JB is basically naming conventions for fully powered Java classes EJB rules are much more rigid EJB classes are less powerful on their own, but more powerful in a container

12 12 ’Tis but thy name that is my enemy EJB is all about really poor names Architecture makes sense, object names don’t –“Home” is a factory –“Container” is a helper –“EJB Object” is an object but is not an EJB –"Enterprise JavaBean" is not a JavaBean

13 13 Beaneo and Appliet 'Tis but thy name that is my enemy... What's in a name? That which we call a rose By any other name would smell as sweet. So Java would, were he not Java call'd, Retain that dear perfection which he owes Without that title. Java, doff thy name; And for that name, which is no part of thee, Take all myself. - Gosling, _Beaneo and Appliet_, 1595

14 14 Persistent 2x2 Grid Entity Bean vs. Session Bean Container-Managed vs. Bean-Managed

15 15 Transactional Support for distributed transactions (more later) You can let the Server manage transactions –You will if you know what’s good for you

16 16 Secure SSL/RMI protects transmitted data Client-Server architecture protects –proprietary code –backend database SSL authenicates client, server ACLs provide fine-grained control of access to objects, methods, services

17 17 Multithreaded Programmer delegates all responsibility for multithreading to server –Programmer literally can’t spawn a thread Program designer and/or sysadmin establishes multithreading policies Server enforces them invisibly

18 18 Naming Services JNDI Wrapper for many naming services –CORBA, RMI, etc.

19 19 EJB Architecture (Sneak Preview) Client Server Home Interface (Factory) EJB Object (Wrapper) Enterprise Java Bean (Biz Logic) Remote Interface Container RMI Implements Invokes Creates / uses Naming Service

20 ©1996-2000 jGuru.com Distributed Computing

21 21 Two-Tier Computing

22 22 Two-Tier Computing Client: Presentation and Application logic –Text, Graphs, Widgets –Business logic –Data Access Backend: Database –Data –Security, Transactions –Resource Management

23 23 Multitier Computing

24 24 3-Tier Computing Client: Presentation Logic –Text, Graphs, Widgets Middle-Tier: Application Logic –Business Logic –Security, Transactions –Resource Management –Data Access Backend: Database –Data

25 25 Motivations for using 3-tier over 2-tier Quality of Service –Scalability –Availability –Performance Versatility –Flexibility –Extensibility

26 26 Disadvantages of N-Tier Learning curve $$ cost of products $$ cost of extra servers to achieve performance Object-relational mapping Basically, app servers are good for large, general solutions (e.g. multiple data sources, multiple applications)

27 27 COM+ Simple Programming Model –Attribute based programming –Focus on Business Logic –Simple API –Well defined Lifecycle Limited Portability –Only on Windows Platform –One vendor: Microsoft

28 28 CORBA Complex Programming Model –Different API for each Service –Explicit use of service APIs –Lifecycle depends on services used Fairly Portable –Many Languages, many platforms (pro and con) –Non-Uniform support of Services CORBA and EJB work together pretty well (more later)

29 29 Enterprise JavaBeans Simple Programming Model –Attribute based programming –Focus on Business Logic –Simple API –Well defined Lifecycle Portable –Specification ensures basic services –Component Packaging (JARs and XML DD) –Java

30 ©1996-2000 jGuru.com EJB Architecture

31 31 EJB-Roles Bean developer: creates JAR with: –Remote interface with business functions –Home for accessing instances of bean –Bean itself –Properties and descriptor Assembler: integrates beans into clients Deployer: modifies and deploys beans –Organization-specifics, ie security

32 32 EJB Roles (cont.) Server provider provides Server run-time Container provider: provides tools and containers –Creates container classes that wrap bean –Manage installed beans

33 EJS EJS Container(s) EJB: Tools and Components Client Enterprise Java Bean JAR Enterprise Java Bean JAR Database Deployment Tool Deployment Tool Assembler tool Assembler tool EJB Authoring Tool EJB Authoring Tool Run-time Design/Deploy time

34 34 EJB Container model CORBA, COM model has objects aggregating behavior –Leads to very large components EJB = Next step in evolution of frameworks Objects gain services by attaching to container Easier to build, maintain and adapt

35 35 EJB Container Model cont. Object specifies required services from container –Fits into the framework –Services including persistence, transactions, security, etc. Leads to Business objects and Service Objects

36 36 EJB Runtime environment EJB Server and EJB Container EJB Server co-ordinates resources for Containers EJB Container vendors implement core services –Object Wrapping, Life-cycle management, Transaction co-ordination, Security, Naming 1 EJB Container for every Class –but more than one class per container

37 37 The Container: The Foundation of EJB The EJB Container Enterprise bean components

38 38 The EJB Container Containers host enterprise beans Containers isolate beans from clients Containers manage enterprise beans Bean-Container interface –callback methods –EJBContext –JNDI ENC

39 39

40 40 Enterprise Bean Components Implements the EJB API Defined by an XML Deployment Descriptor Adheres to the EJB specification. Packaged in a JAR file

41 41 Component API: Interfaces and Classes Home Interface –Extends javax.ejb.EJBHome –Provides remote access to create, find, remove beans Remote Interface –Extends javax.ejb.EJBObject –Provides remote access to business methods Bean Class –Extends a javax.ejb.EnterpriseBean type –Implements business logic and other functionality

42 42

43 43 Bean provider: What to write? Remote Interface –extend EJBObject interface –Define business method signatures Home Interface –extend EJBHome interface –Define create signatures –May define findBy signatures for entities

44 44 What to write (cont.)? Enterprise Bean Class –implement EntityBean or SessionBean –Implement business method signatures –Does not need to implement Remote Interface –not abstract –Implement ejbCreate methods matching Home create 1 ejbCreate for every Home.create N.B.: “create” in home interface, “ejbCreate” in Bean

45 45 Remote Interface import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Customer extends EJBObject { public Name getName() throws RemoteException; public void setName(Name name) throws RemoteException; public Address getAddress() throws RemoteException; public void setAddress(Address address) throws RemoteException; }

46 46 Home Interface import javax.ejb.EJBHome; import javax.ejb.CreateException; import javax.ejb.FinderException; import java.rmi.RemoteException; public interface CustomerHome extends EJBHome { public Customer create(Integer customerNumber) throws RemoteException, CreateException; public Customer findByPrimaryKey(Integer customerNumber) throws RemoteException, FinderException; public Enumeration findByZipCode(int zipCode) throws RemoteException, FinderException; }

47 47

48 48 Bean Class import javax.ejb.EntityBean; public class CustomerBean implements EntityBean { Address myAddress; Name myName; CreditCard myCreditCard; public Name getName() { return myName; } public void setName(Name name) { myName = name; } public Address getAddress() { return myAddress; } public void setAddress(Address address) { myAddress = address; }... }

49 49

50 50 Types of Beans Entity Beans –Represent data in the database –Include behavior (business methods) Session Beans –Represent tasks or processes –Do not represent data, but can access data –Manage the interactions of other beans

51 51 Home Interface Defined by Bean developer Implemented by server tools (autogenerated) Must extend interface EJBHome –EJBMetaData getEJBMetaData() –void remove(Handle ejbHandle) –void remove(Object primaryKey) Must provide your own create() methods –Foo create() –Foo create(Bar b, Baz z)…

52 52 Home Interface: Entity Beans Entity Beans are persistent, therefore they need more than a “create” method Need findXXX methods –public Foo findByPrimaryKey(Object key); –public Foo findByBar(Bar bar); –public Enumeration findOverdrawnAccounts(); Implement ejbFindXXX methods in bean –N.B.: “find” in home interface, “ejbFind” in Bean

53 53 Remote Interface Written by developer Defines methods accessible by client –Your business methods go here extends javax.ejb.EJBObject –standard methods provided by all EJBs –getEJBHome(), getPrimaryKey(), getHandle(), remove(), isIdentical(EJBObject obj)

54 54 Remote Interface vs. EJBObject vs. EJB Developer writes Remote Interface Tool uses RI to automatically generate the EJBObject class Developer writes the EJB source file N.B.: The EJB does not implement the Remote Interface –However, you still must implement all the business methods –Lame-o-rama: no type safety provided by compiler –Better: if EJB tool auto-created a new interface for the EJBObject (oh well)

55 55 Implementing the EJB Implements all your business methods Must also implement –ejbCreate() methods –ejbFind() methods (for Entity Beans) Also callback methods –ejbRemove() –ejbActivate() –ejbPassivate() Implement which interface? –javax.ejb.SessionBean –javax.ejb.EntityBean

56 56 Interfaces and Implementations EJBObject getEJBHome() getPrimaryKey() getHandle() isIdentical() remove() deposit() getBalance() Remote Interface deposit() getBalance() EJB ejbCreate() ejbRemove() ejbActivate() ejbPassivate() deposit() getBalance() Home Interface create() remove()

57 ©1996-2000 jGuru.com Entity Beans

58 58 Characteristics of Entity beans Model entities in a system –represent their data and associated behavior one or many relational database tables an object in an object database an entity in a legacy system –Nouns: People, Places or Things Customer, Employee, Student City, Building, Hotel Room Order, Organization, Health Benefit –Two Kinds of Persistence Container-Managed Persistence Bean-Managed Persistence

59 59 Container-Managed Persistence (CMP) Persistence is automatic –inserts, updates, and deletes are automatic –transactions managed automatically Focus on business logic, not persistence Components more portable –not backend specific –shrink wrapped components Requires sophisticated data mapping tools

60 60 public interface javax.ejb.EntityBean { public void setEntityContext(); public void unsetEntityContext(); public void ejbLoad(); public void ejbStore(); public void ejbActivate(); public void ejbPassivate(); public void ejbRemove(); } import javax.ejb.EntityBean; public class CustomerBean implements EntityBean { Address myAddress; Name myName; CreditCard myCreditCard;... public void setEntityContext(){} public void unsetEntityContext(){} public void ejbLoad(){} public void ejbStore(){} public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} }

61 61 Callback Methods in CMP setEntityContext() / unserEntityContext() –EntityContext is an interface to container client identity, transactions, self ejbLoad() / ejbStore( ) –database synchronization notifications ejbLoad() just after bean fields refreshed from database ejbStore() just prior to database write

62 62 Callback Methods in CMP (cont..) ejbCreate()/ ejbRemove() –In response to client invocations –ejbCreate() just before inserting a new record(s) –ejbRemove() just before deleting record(s) ejbActivate( ) / ejbPassivate( ) –Resource Conservation Passivated instances are evicted or pooled Most container pool entity beans –Notify bean when passivation is occurring ejbPassivate() just before passivation ejbActivate() just after bean is restored

63 63 Bean-Managed Persistence Persistence is not automatic –Database access coded directly into bean Callback methods dictate when to insert, update, delete Database connections managed automatically Transactions and connections managed automatically Focus on business logic and persistence More control over persistence Don’t need Sophisticated data mapping tools

64 64 Callback Methods in BMP ejbLoad() / ejbStore( ) –Database synchronization notifications ejbLoad() tells bean to write state to database ejbStore() tells bean to read state from database ejbCreate()/ ejbRemove() –In response to client invocations –ejbCreate() tells bean to insert a new record(s) –ejbRemove() tells bean to delete record(s)

65 65 Callback method in BMP (cont.…) setEntityContext() / unserEntityContext() –Same as CMP ejbActivate( ) / ejbPassivate( ) –Same as CMP

66 66 Characteristics of Session beans Not persistent, but can access database Model tasks, process or agents –Charge a credit card –Process a stock purchase –Perform hotel reservations Manage interactions of other beans Two Kinds of Session beans –Stateless –Stateful

67 67 Stateless Session Beans Perform transitive tasks Independent business methods –Act on method arguments, not bean state –Similar to procedures in traditional TPM systems Most performant bean type Not dedicated to one client

68 68 Callback methods in stateless beans setSessionContext() –SessionContext is an interface to container Client identity, transactions, self ejbCreate()/ ejbRemove() –Not in response to client invocations –ejbCreate() beginning of bean instance life –ejbRemove() end of bean instance life (before eviction) ejbPassivate()/ejbActivate() –Not used in stateless beans

69 69 Stateful Session Beans Act on behalf of one client –Extension of the client –Agent that performs work for the client Interdependent business methods –Bean has conversational state –Method depend on conversational state Dedicated to exactly one client

70 70 Callback methods in stateful beans setSessionContext() –SessionContext is an interface to container Client identity, transactions, self ejbCreate()/ ejbRemove() –In response to client invocations –ejbCreate() initialize conversational state –ejbRemove() just before bean is destroyed

71 71 Callback methods in stateful beans (cont.…) ejbPassivate()/ejbActivate() –Passivation to secondary storage –ejbPassivate() just before passivation –ejbActivate() just after passivation

72 ©1996-2000 jGuru.com EJB Deployment

73 73 Function of a Deployment Descriptor Describe bean(s) to the container –interfaces and class –type of bean (entity or session) –identify primary key & container-managed fields Declare runtime behavior –transaction attributes of methods –authorization access to method –persistence type (BMP vs. CMP) Written in XML

74 74 XML Deployment Descriptors XML Document –uses a DTD to verify structure –text document can view with any text editor –mark-up language start and end tags similar to HTML DTD (Document Type Definition) –defines all valid tags –defines nesting of tags

75 ©1996-2000 jGuru.com EJB Clients

76 76 Types of Clients Enterprise beans J2EE Application Components Java Applications Java Applets Servlets JSP

77 77 Example: J2EE Application Component CustomerHome home; Object ref; // Obtain a reference to the CustomerHome ref = jndiContext.lookup("java:comp/env/ejb/Customer"); // Cast object returned by the JNDI lookup to the // appropriate datatype home = PortableRemoteObject.narrow(ref, CustomerHome.class); // Use the home interface to create a // new instance of the Customer bean. Customer customer = home.create(customerID); // Use a business method on the Customer. customer.setName(someName);

78 78 Java RMI-IIOP EJB 1.1 clients use the Java RMI-IIOP API Underlying protocol doesn’t have to be IIOP EJB 2.0 will require IIOP as the protocol Requirement smoothes transition to EJB 2.0 PortableRemoteObject.narrow( ) because CORBA doesn’t support native casting.

79 ©1996-2000 jGuru.com EJB vs. Other Technologies

80 80 CORBA and EJB Transport –EJB uses RMI interface, RMI uses IIOP CORBA 3.0 promises object compatibility with EJB –Not quite sure what that means –CORBA Components Some EJB Servers contain an ORB –In these servers, all EJB Objects are also CORBA objects

81 81 CORBA and EJB (Cont.) All EJB Servers use CORBA Transactions (via JTS) –That means that any client can make a distributed transaction that includes both CORBA and EJB Objects Not an either-or decision –You can have both EJB and CORBA working together in a single system

82 82 EJB/ MS comparison

83 83 FUD: COM Persistence MS Claimed that Entity objects are bad –Session objects for scalability Assumption #1: –MS knows more than IBM/Sun/Oracle about scalability Assumption #2 –Sessions = more scalability No caching News to CPU and OS vendors Makes sense if you sell small servers Assumption #3 –Entity objects never allowed Why then is there a COM in memory database? –So Entity objects are bad for scalability, but the upcoming imdb is somehow different? Look for MS Entities soon :-)

84 84 Topic Review Defined Enterprise JavaBeans EJB and Distributed Computing EJB Architecture Entity beans Session bean Deployment EJB Clients

85 85 Online Resources jGuru EJB FAQ –http://www.jguru.com/faq/EJB Sun EJB Page –http://java.sun.com/products/EJB EJBNow –http://www.ejbnow.com/

86 86 Thanks To Richard Monson-Haefel Dave Orchard Random Walk Computing


Download ppt "©1996-2000 jGuru.com Enterprise JavaBeans Fundamentals."

Similar presentations


Ads by Google