1 EJB 2.0 : An Overview CS486 Spring 2001 Vijayanand Bharadwaj.
-
Upload
roderick-andrews -
Category
Documents
-
view
217 -
download
0
Transcript of 1 EJB 2.0 : An Overview CS486 Spring 2001 Vijayanand Bharadwaj.
1
EJB 2.0 : An Overview
CS486
Spring 2001
Vijayanand Bharadwaj
2
EJB Evolution (0.0 to 2.0)EJB :What do we know so far ?What’s new in EJB 2.0 ?Some Newer Features in EJB 2.0Analysis of EJB 2.0 : the good and the
“bad”.Conclusion and References
Overview
3
References: Primary -- class text.also Sun’s Java site and articles marked throughout as reference [# ] and
list available at the end.
Remarks
4
EJB specification release
– EJB 1.0 -- March 24th 1998– EJB 1.1 -- May 1999 time frame– EJB 2.0 -- June 2nd 2000 ( final draft)
Significant changes made between 1.0 and 1.1However EJB 1.1 totally compatible with 2.0.
Nothing deprecated . All 1.1 code should still deploy correctly.
Industry forecasts for EJB 2.0 [4] : “Supposed to enhance portability and flexibility, along with tightening some older features”. Lets see……...
EJB Evolution (0.0 to 2.0) [3]
5
EJB : What do we know so far ..? [1][5][6].Applications:
– Building portable software components which can run business logic on an Enterprise level.
Concept : What is an EJB (Enterprise Java Bean)?– A software component which allows a developer to
encapsulate business logic, freeing him/her from system level development concerns such as transactions, threading ,load balancing etc.
So far along the EJB trail……..
6
Anatomy and Working of an EJB
So far along the EJB trail……..
App Server
Container
Network
Client
Skeleton
EJB HOME
REMOTE
Stub
STORE
7
Types of EJB
So far along the EJB trail……..
Relationship to database managed by:
Maintain state across methods
Based on Main Purpose, Callback, Runtime lifecycles
EJB
ENTITY BEANS SESSION BEANS
STATEFUL SBSTATELESS SB
BMP EB CMP EB
?????
8
Areas in which EJB 2.0 has something new to offer [1][7]
1. Container managed Persistence
2. EJB Query Language
3. Message-Driven Beans
4. Business methods in the Home interface
5. Improved interoperability
Say how we divideExplain briefly each one EJB: What do we know so far ?
EJB 2.0 : What’s new ?
9
New type of Container-Managed Persistence which deals with the drawbacks of the CMP model of version 1.1.
Standard query language for CMP. Formerly developer provided a description of what the finder methods should do . Deployer provided the actual query according tot he target Application Server. Advantages of EJB QL– Query need be developed and tested only
once ( not at each new environment)– Developer can do it .More suited for the
purpose.
EJB 2.0 : What’s new ?
10
Integration of EJB with Java Message Service (JMS). Container on arrival of a JMS message invokes a “message-driven bean”. Need not create a separate front-end for messages.
Business methods in the home interface work support functionality independent of any entity-- can operate on a set of beans of an entity type. ( rather than in a separate session bean as in 1.1)
– Both the above features allow better modularity and design.
An interoperability protocol based on RMI/IIOP and CORBA to allow J2EE applications on different app servers to interact without bean developer intervention
EJB 2.0 : What’s new ?
11
This discussion will cover two aspects– CMP in EJB 2.0– Business methods in the home Interface
The remaining will be covered by our colleague
EJB 2.0 : What’s new ?
12
Container Managed Persistence [4]Reviewing CMP in version 1.1
– Bean developer responsible for declaring the persistent fields in the bean class.
– On an RDBMS primitive types(String, integer etc) map easily to their corresponding types
– Tell the container through the deployment descriptor (DD). The <cmp-field> tags identify such fields.
– Generally container tools allow you to map.
EJB 2.0: Some New Features
13
Container Managed Persistence [4]An example from EJB 1.1 // the Employee bean class
public class EmployeeBean implements java.ejb.EntityBean { // instance fields EntityContext ejbContext; // container-managed fields public int identity; public String firstName; public String lastName; public double salary; public Address address; //refers to dependent
CONTINUED…..
EJB 2.0: Some New Features
14
Container Managed Persistence [4]public Integer ejbCreate(int id, String fname,
String lname){ identity = id; firstName = fname; lastName = lname; return null; } ... } // The Address dependent class public class Address implements Serializable{ public String street; public String city; public String state; public String zip; }
EJB 2.0: Some New Features
15
Container Managed Persistence [4]Deployment Descriptor --version 1.1 (sample)<ejb-jar>
<enterprise-beans> <entity> <ejb-name>EmployeeEJB</ejb-name> ... <persistence-type>Container</persistence-type> ... <cmp-field><field-name>identity</field-name></cmp-field> <cmp-field><field-name>firstName</field-name></cmp-field> <cmp-field><field-name>lastName</field-name></cmp-field> <cmp-field><field-name>salary</field-name></cmp-field> <cmp-field><field-name>address</field-name></cmp-field>
...
EJB 2.0: Some New Features
16
Container Managed Persistence [4]Drawbacks of 1.1
– No standard mechanism to map serializable objects such as “Address” . Generally stored as a blob.
– For entity beans with a large schema problem grows. As an entity bean may have many dependent objects which in turn have dependent objects forming complex graphs.
– Entity bean to Entity bean relationship support not adequate. Since it always uses the other EB’s primary key as a link. Relationships are not always based on the primary key.
EJB 2.0: Some New Features
17
Container Managed Persistence [4]EJB 2.0 CMP
EJB 2.0: Some New FeaturesSo far along the EJB trail……..
App Server
Container
EJB HOME
REMOTE
STORE
Persistence Manager
18
Container Managed Persistence [4]EJB 2.0 CMP Features
– Allows you to define complex bean-to bean, bean to dependent object and dependent to dependent object relationships.
– Done with the help of new “PERSISTENCE MGR”
– Contract between container and Persistence Mgr.
– Contract known as Abstract Schema has two parts • Additional XML tags in DD• Code idioms (Rules for coding the bean )
– Rule) The bean class is declared abstract and all persistent and relationship fields are accessed using abstract accessor and mutator methods whose signatures map to special elements in the DD
EJB 2.0: Some New Features
19
Container Managed Persistence [4]EJB 2.0 CMP Features
EJB 2.0: Some New Features
How can you use an abstract bean class? After all … you have to extend an abstract class in Java and implement it’s methods to use it …….The client can’t do that…..how ???
Java Geek !
20
Container Managed Persistence [4]EJB 2.0 CMP Features ( figure from reference[4])
EJB 2.0: Some New Features
PM does it !!
21
Container Managed Persistence [4]EJB 2.0 CMP Features : Same Example now…..public abstract EmployeeBean implements javax.ejb.EntityBean {
// instance fields EntityContext ejbContext;
// container-managed persistent fields public abstract void setIdentity(int identity); public abstract int getIdentity(); public abstract void setFirstName(String firstName); public abstract String getFirstName(); public abstract void setLastName(String lastName); public abstract String getLastName();
// container-managed relationship fields public abstract void setContactInfo(ContactInfo info); public abstract ContactInfo getContactInfo(); ... }
EJB 2.0: Some New Features
22
Container Managed Persistence [4]A dependent object ( class)………..public abstract class ContactInfo {
// home address info public abstract void setStreet(String street); public abstract String getStreet(); public abstract void setState(String state); public abstract String getState(); public abstract void setZip(String zip); public abstract String getZip(); public abstract void setHomePhone(String phone); public abstract String getHomePhone();
// work address info public abstract void setWorkPhone(String phone); public abstract String getWorkPhone(); public abstract void setEMail(String email); public abstract String getEMail(); ... }
EJB 2.0: Some New Features
23
Container Managed Persistence [4]About dependent classes………..
– They form relationships with entity beans– live and die along with the entity bean– only visible to the bean class and not the client– Bean class must have additional methods to expose
dependent objects to the client since they are not suitable remote arguments.
Looking at an example …...
EJB 2.0: Some New Features
24
Container Managed Persistence [4]Remote Interface for Employee bean……….// remote interface for the Employee bean
public interface Employee extends javax.ejb.EJBObject { public Address getHomeAddress();//Note this ! public void setHomeAddress(Address address);
public int getIdentity() throws RemoteException; public void setFirstName(String firstName) throws RemoteException; public String getFirstName()throws RemoteException; public void setLastName(String lastName) throws RemoteException; public String getLastName() throws RemoteException; }
EJB 2.0: Some New Features
25
Container Managed Persistence [4]Corresponding implementation in bean class,……
// bean class for the Employee bean public abstract EmployeeBean implements javax.ejb.EntityBean { ... public Address getHomeAddress(){ ContactInfo info = getContactInfo(); Address addr = new Address(); addr.street = info.getStreet(); addr.city = info.getCity(); addr.state = info.getState(); addr.zip = info.getZip(); return addr; } CONTINUED…...
EJB 2.0: Some New Features
26
Container Managed Persistence [4]Corresponding implementation in bean class,……
public void setHomeAddress(Address addr){ ContactInfo info = getContactInfo(); info.setStreet(addr.street); info.setCity(addr.city); info.setState(addr.state); info.setZip(addr.zip); } .... // container-managed relationship fields public abstract void setContactInfo(ContactInfo info); public abstract ContactInfo getContactInfo(); ... }
EJB 2.0: Some New Features
27
Coding Relationships Entity Bean to Dependent Object (one-one or one-
many) eg: Employee bean is related to one benefit but has many contacts.
public abstract EmployeeBean implements javax.ejb.EntityBean { ... public abstract void setContactInfos(Collection addresses); public abstract Collection getContactInfos():
public abstract void setBenefit(Benefit benefit); public abstract Benefit getBenefit();
... } Use Collections to get many dependent objects
EJB 2.0: Some New Features
28
Coding Relationships Entity Bean to Entity Bean (one-one or one-many or
many to many) eg: Employee bean is related to one Spouse but has many Children ( Both are Person beans).
public abstract EmployeeBean implements javax.ejb.EntityBean { ... public abstract void setSpouse(Person manager); public abstract Person getSpouse();
public abstract void setChildren(Collection family); public abstract Collection getChildren(); ... }
Use Collections to get at many Entity Beans.
EJB 2.0: Some New Features
29
Coding Relationships Dependent Object to Dependent Object ( or another
entity bean) (one-one or one-many or many to many) eg: Benefit object is related to one Salary and many Investment objects
public abstract class Benefit { public abstract void setSalary(Salary salary); public abstract Salary getSalary();
public abstract void setInvestments(Collection investments); public abstract Collection getInvestments(); }
EJB 2.0: Some New Features
30
Deployment Descriptor<ejb-jar>
<enterprise-beans> <entity> <ejb-name>EmployeeEJB</ejb-name> ... <persistence-type>Container</persistence-type> ... <cmp-field><field-name>identity</field-name></cmp-field> <cmp-field><field-name>firstName</field-name></cmp-field> <cmp-field><field-name>lastName</field-name></cmp-field> ... </entity> </enterprise-beans>
CONTINUED………..
EJB 2.0: Some New Features
31
Deployment Descriptor
<dependents> <dependent> <dependent-class>ContactInfo</dependent-class> <dependent-name>ContactInfo</dependent-name> <cmp-field>street</cmp-field> <cmp-field>city</cmp-field> <cmp-field>state</cmp-field> <cmp-field>zip</cmp-field> <cmp-field>homePhone</cmp-field> <cmp-field>workPhone</cmp-field> <cmp-field>email</cmp-field> ... </dependent>
CONTINUED………..
EJB 2.0: Some New Features
32
Deployment Descriptor<relationships>
<ejb-relation> <ejb-relation-name>Employee-ContactInfo</ejb-relation-name> <ejb-relationship-role> <ejb-relationship-role-name> employee-has-contactinfo </ejb-relationship-role-name> <multiplicity>one</multiplicity> <role-source> <ejb-name>EmployeeEJB</ejb-name> </role-source> <cmr-field> <cmr-field-name>contactInfo</cmr-field-name> <cmr-field-type>ContactInfo</cmr-field-type> </cmr-field> </ejb-relationship-role>
CONTINUED………..
EJB 2.0: Some New Features
33
Deployment Descriptor……..<ejb-relationship-role>
<ejb-relationship-role> <ejb-relationship-role-name> contactinfo_belongsto_employee </ejb-relationship-role-name> <multiplicity>one</multiplicity> <role-source> <dependent-name>ContactInfo<dependent-name> </role-source> </ejb-relationship-role> </ejb-relation> </relationships> <ejb-jar>
EJB 2.0: Some New Features
34
Business methods in the home interface [8] Let us consider operations on multiple entity beans.
For Example: “deleting unused shopping cart entity beans”
Could be done using finder methods or session beans
Using “finder methods” implies moving a business function ( deletion)(to the client --not the best design
Session beans --problems in maintenance as you have to add such function to existing session bean or write another bean which results in extra beans --tedious.
EJB 2.0: Some New Features
35
Business methods in the home interface (continued)
Version 2.0 allows you to add business in the home interface. Not associated with any one particular instance of the entity bean.
To define a Home method, just add it to your Home interface. The only restriction on the method name is that it can't start with find, create, or remove.
EJB 2.0: Some New Features
36
Business methods in the home interface (continued)
For example, the method to expire shopping carts might be declared this way:
public void expireShoppingCarts() throws RemoteException
When you implement a Home method, put ejbHome in front of the method name in your implementation class. For example,
public void ejbHomeExpireShoppingCarts()
EJB 2.0: Some New Features
37
Business methods in the home interface (continued)
public interface EmployeeHome extends javax.ejb.EJBHome { // a home method public void applyCola(double increate) throws RemoteException; ... }
EJB 2.0: Some New Features
38
Business methods in the home interface (continued)
public abstract class EmployeeBean implements javax.ejb.EntityBean { ... // ejbHome method public void ejbHomeApplyCola (double increase )
{ Collection col = ejbSelectAllEmployees (); Iterator employees = col.iterator(); while(employees.next()){ ………………………………………….} } }
EJB 2.0: Some New Features
39
EJB 2.0: Analysis
Advantages of EJB 2.0 features !! Complex schemas involving relationships among entity beans and
dependent objects can be depicted with the help of persistence manager.Closer to intuition and better database design and use.
Business methods in the home interface provide option for better design by isolating business logic completely within the entity.
Entity beans can communicate using JMS at the business logic level itself --one more for better design !
New EJB QL makes standardized query possible and relives deployer ! Helps in correct creation of finder method queries.
Tightened some unresolved issues of 1.1
40
EJB 2.0: Analysis
SO EVERYBODY HAPPY... END OF STORY !!!!
41
EJB 2.0: Analysis
NOT SO FAST ! !
I’m not quite satisfied … . . . . .Not all is o.k with EJB 2.0 ! !
Another Java Geek !
42
EJB 2.0: Analysis “What’s wrong with EJB 2.0 Specification ?” by Tyler Jewell , February 28th 2001 [9]
– An article on the O’Reilly Network
This article lists a few reasons--why things are not alright– dependent objects. – Lack of support for fine grained objects– Some other inconsistencies.– Also new suggestions not implemented by
container vendors.
Suggests the removal of
dependent objects completely !!
43
[1] Professional Java Server Programming J2EE edition. (Wrox Press Ltd. ) September 2000. Chapters 18,19,20,23
[2] The EJB home page at Sun Microsystems
http://java.sun.com/products/ejb/
[3] http://java.sun.com/products/ejb/news.html
[4] Monson-Haefel R. “Read all about EJB 2.0 “An article. http://www.javaworld.com/javaworld/jw-06-2000/jw-0609-ejb.html
[5]EJB fundamentals short course by JGuru http://developer.java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.html
CONTINUED…..
References
44
[6]Johnson M. “A beginner's guide to Enterprise JavaBeans” .(article) http://www.javaworld.com/javaworld/jw-10-1998/jw-10-beans.html
[7]EJB 2.0 specification
http://java.sun.com/products/ejb/docs.html
[8] Wutka. M, “Whats new in EJB 2.0”( article)
http://www.informit.com
[9]Jewell T. “Whats wrong with the EJB 2 Specification?”. (Article) . The O’Reilly network. http://onjava.com/pub/a/onjava/2001/02/28/ejb.html
--------------------------------------*****--------------------------------------
Conclusion and References