Enterprise Java Beans - PowerPoint PPT Presentation

1 / 98
About This Presentation
Title:

Enterprise Java Beans

Description:

Home Interface for create, find, and remove. Remote Home Interface extends ... { PersonRemote create(String id) throws CreateException, ... home.create(pValues) ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 99
Provided by: daniel85
Category:

less

Transcript and Presenter's Notes

Title: Enterprise Java Beans


1
Enterprise Java Beans
  • Entity Beans

2
Enterprise Bean
  • Component that can be deployed in any
    EJB-compliant application server
  • Component interface for its operations
  • Remote Interface extends javax.ejb.EJBObject
  • Local Interface extends javax.ejb.EJBLocalObject
  • Home Interface for create, find, and remove
  • Remote Home Interface extends javax.ejb.EJBHome
  • Local Home Interface extends javax.ejb.EJBLocalHom
    e
  • Bean Implementation
  • implements javax.ejb.SessionBean or
    javax.ejb.EntityBean
  • Required container lifecycle callbacks
  • implements business methods
  • implements finder (BMP Entity), home (2.0
    Entity), and Select (2.0 CMP Entity) methods

3
Bean Types
  • Entity Beans
  • Represents persistent data in a database
  • Account and deposit()
  • Exists indefinitely
  • Bean-managed and Container-managed persistence
  • Session Beans
  • Represents business logic operations
  • Teller and transfer()
  • Stateless and Stateful
  • Exist for lifetime of client session
  • Message Beans
  • Stateless component invoked using asynchronous
    message (JMS)

4
Container and Beans
create find
JVM
Container
Remote Home
Remote Home Stub
Remote Client
Remote Object
Remote Object Stub
EJB Class
EJB Class
EJB Class
Local Home
Bean Pool
deposit transfer
Local Client
Local Object
5
Entity Bean (from Spec)
  • Provides an object view of data in the database
  • Allows shared access from multiple users
  • Can be long-lived (lives as long as the data in
    the database)
  • The entity, its primary key, and its remote
    reference survive the crash of the EJB container

6
Entity Bean Support
  • Optional in EJB 1.0
  • Required in EJB 1.1
  • Revised in EJB 2.0
  • Added Abstract Schemas
  • Added EJB Query Language which is portable across
    different database technologies (even though it
    is similar to SQL)
  • Added Container Managed Relationships
  • EJB 1.1 support still a requirement in EJB 2.0

7
Bean Characteristics
8
Creating a Person Entity Bean
  • Create the Primary Key Class
  • Create the Business Interface
  • Create the Remote Object Interface
  • Create the Local Object Interface
  • Create the Remote Home Interface
  • Create the Local Home Interface
  • Create the Bean Class
  • Create the Deployment Descriptor

9
ltltinterfacegtgt
EntityBean
ejbActivate () void
ltltinterfacegtgt
ejbPassivate () void
ltltinterfacegtgt
PersonLBI
ejbLoad () void
PersonBI
ejbStore () void
ejbRemove () void
getId () String
setEntityContext (EntityContext ctx) void
getValues () Person
getFirstName () String
setValues (values Person) void
setFirstName (name String) void
ltltabstractgtgt
PersonEJB
setId (String id) void
getId () void
getFirstName () String
setFirstName (name String) void
getValues () Person
setValues (values Person) void
ejbCreate (...) PersonPK
ejbPostCreate (...) void
ejbActivate () void
ejbPassivate () void
ejbStore () void
ejbRemove () void
setEntityContext (ctx EntityContext) void
unsetEntityContext () void
Container_PersonEJB_Impl
setId (String id) void
getId () String
getFirstName () String
setFirstName (name String) void
10
(No Transcript)
11
PrimaryKey Class
  • Two Objects are considered the same if they have
    the same Home and their PrimaryKey objects are
    equal
  • PrimaryKey classes
  • single primary keys can be mapped to existing
    classes (e.g. String)
  • compound primary keys must be encapsulated by a
    custom class
  • key values
  • must be legal RMI-IIOP types
  • must match in type and name with declaration in
    the bean class
  • must be public
  • class must implement a Default Constructor,
    hashCode() and equals() methods
  • must be Serializable

12
Create the PrimaryKey Class
  • package ejava.examples.personnel.ejb20
  • import java.io.Serializable
  • public class PersonPK implements Serializable
  • public String id
  • public PersonPK()
    //this one is required
  • public PersonPK(String id) this.id id
    //this one is optional
  • public String toString() return id
  • public int hashCode() return
    id.hashCode()
  • public boolean equals(Object rhs)
  • try
  • return ((PersonPK)rhs).id.equals(id)
  • catch (ClassCastException ex)
  • return false

13
Remote Business Interface
  • Defines the business methods to be implemented by
    the Bean that can be invoked by an external
    client
  • Is implemented by the Remote Object and Bean
    Classes
  • Methods must declare throws java.rmi.RemoteExcept
    ion
  • Arguments and return types must be legal RMI-IIOP
    types
  • Does NOT extend java.rmi.Remote
  • doing so makes the Bean Class a Remote Object
    (bad)
  • Does NOT extend any EJB Interfaces
  • doing so would limit its use to only that type of
    object
  • Consider larger-grain accessors

14
Create the Remote Business Interface
  • package ejava.examples.personnel.ejb20
  • import ejava.examples.personnel.Person
  • import java.rmi.RemoteException
  • public interface PersonBI
  • / returns a bulk-state snapshot of the
    Person object /
  • Person getValues() throws RemoteException
  • / sets values of the person, except id, in
    single call /
  • void setValues(Person values) throws
    RemoteException

15
Create Value Objects
  • package ejava.examples.personnel
  • import java.io.Serializable
  • public class Person implements Serializable
  • public Person() this("","","","","")
  • public Person(String id, String firstName,
    String lastName,
  • String address, String
    phoneNumber) ...
  • public void setFirstName(String fname)
    this.firstName fname
  • public void setLastName(String lname)
    this.lastName lname
  • public void setAddress(String address)
    this.address address
  • public void setPhoneNumber(String pnum)
    this.phoneNumber pnum
  • public String getId() return
    this.id
  • public String getFirstName() return
    this.firstName
  • public String getLastName() return
    this.lastName
  • public String getAddress() return
    this.address
  • public String getPhoneNumber() return
    this.phoneNumber
  • private String id, firstName, lastName,
    address, phoneNumber

16
Remote Object Interface
  • Must extend javax.ejb.EJBObject
  • Extends Business Interface
  • package ejava.examples.personnel.ejb20
  • import javax.ejb.EJBObject
  • public interface PersonRemote extends PersonBI,
    EJBObject

17
Local Business Interface
  • Defines the business methods to be implemented by
    the Bean that may only be invoked by a local
    client
  • Is implemented by the Local Object and Bean
    Classes
  • Methods CANNOT throw java.rmi.RemoteException
  • Arguments are passed by reference (caution!)
  • Does NOT extend any EJB Interfaces
  • doing so would limit its use to only that type of
    object
  • Consider fine-grain accessors

18
Create the Local Business Interface
  • package ejava.examples.personnel.ejb20
  • public interface PersonLBI
  • / read-only primary key field /
  • String getId()
  • String getFirstName()
  • void setFirstName(String fname)
  • String getLastName()
  • void setLastName(String lname)
  • String getAddress()
  • void setAddress(String address)
  • String getPhoneNumber()
  • void setPhoneNumber(String number)

19
Local Object Interface
  • Must extend javax.ejb.EJBObject
  • Extends Business Interface
  • package ejava.examples.personnel.ejb20
  • import javax.ejb.EJBLocalObject
  • public interface PersonLocal extends PersonLBI,
    EJBLocalObject

20
Remote Home Interface
  • Extends javax.ejb.EJBHome
  • Only works with Remote Interface (PersonRemote)
  • createltSuffixgt() and createltSuffixgt( args )
  • creates row in database
  • optional with entity beans
  • must have matching ejbCreateltSuffixgt() and
    ejbPostCreateltSuffixgt() methods in bean
    implementation if present
  • find()
  • findByPrimaryKey(ltprimary key classgt) is
    mandatory
  • other finders can be implemented
  • ex. PersonRemote findByName(String firstName,
    String lastName)
  • ex. Collection findByAddress(String address)
  • return types include java.util.Collection and
    java.util.Set

21
Create Remote Home Interface
  • package ejava.examples.personnel.ejb20
  • import ejava.examples.personnel.Person
  • import javax.ejb.EJBHome
  • import javax.ejb.CreateException
  • import javax.ejb.FinderException
  • import java.rmi.RemoteException
  • import java.util.Collection
  • public interface PersonRemoteHome extends EJBHome
  • PersonRemote create(String id) throws
    CreateException, RemoteException
  • PersonRemote create(Person values)
  • throws CreateException, RemoteException
  • PersonRemote findByPrimaryKey(PersonPK pkey)
  • throws FinderException, RemoteException
  • PersonRemote findByName(String firstName,
    String lastName)
  • throws FinderException, RemoteException
  • Collection findAll()
  • throws FinderException, RemoteException
  • Collection findByAddress(String address)
  • throws FinderException, RemoteException

22
Remote Creation Example
  • Context jndi getInitialContext() // possibly
    new InitialContext()
  • Object object jndi.lookup(getHomeContextName())
  • PersonRemoteHome home (PersonRemoteHome)
  • PortableRemoteObject.narrow(object,
    PersonRemoteHome.class)
  • Person pValues new Person(id, fName, lName,
    address, pNumber)
  • PersonRemote p1 home.create(pValues)
  • System.out.println(inserted person
    p1.getValues().toString())
  • PersonRemote p2 home.findByPrimaryKey(new
    PersonPK(id))
  • System.out.println(found person
    p2.getValues().toString())
  • Collection people home.findByAddress(findAddress
    )
  • for(Iterator ipeople.iterator() i.hasNext() )
  • PersonRemote p3 (PersonRemote)i.next()
  • System.out.println(p3.getValues())

23
Local Home Interface
  • Extends javax.ejb.EJBLocalHome
  • Only works with Local Interface (PersonLocal)
  • createltSuffixgt() and createltSuffixgt( args )
  • creates row in database
  • optional with entity beans
  • must have matching ejbCreateltSuffixgt() and
    ejbPostCreateltSuffixgt() methods in bean
    implementation if present
  • find()
  • findByPrimaryKey(ltprimary key classgt) is
    mandatory
  • other finders can be implemented
  • ex. PersonLocal findByName(String firstName,
    String lastName)
  • ex. Collection findByAddress(String address)
  • return types include java.util.Collection and
    java.util.Set

24
Create Local Home Interface
  • package ejava.examples.personnel.ejb20
  • import javax.ejb.EJBLocalHome
  • import javax.ejb.CreateException
  • import javax.ejb.FinderException
  • import java.util.Collection
  • public interface PersonLocalHome extends
    EJBLocalHome
  • PersonLocal create(String id)
  • throws CreateException
  • PersonLocal create(String id,
  • String firstName, String lastName, String
    address, String phoneNumber)
  • throws CreateException
  • PersonLocal findByPrimaryKey(PersonPK pkey)
  • throws FinderException
  • PersonLocal findByName(String firstName,
    String lastName)
  • throws FinderException
  • Collection findAll() throws
    FinderException
  • Collection findByAddress(String address)
    throws FinderException

25
Local Creation Example
  • Context jndi new InitialContext() //can only
    look local
  • Object object jndi.lookup(getHomeContextName())
  • PersonLocalHome home (PersonLocalHome)
  • PortableLocalObject.narrow(object,
    PersonLocalHome.class)
  • PersonLocal p1 home.create(id, fName, lName,
    address, pNumber)
  • System.out.println(inserted person
    p1.getFirstName() p1.getLastName())
  • PersonLocal p2 home.findByPrimaryKey(new
    PersonPK(id))
  • System.out.println(found person
    p2.getFirstName() p2.getLastName())
  • Collection people home.findByAddress(findAddress
    )
  • for(Iterator ipeople.iterator() i.hasNext() )
  • PersonLocal p3 (PersonRemote)i.next()
  • System.out.println(p3.getValues())

26
Create the Bean Class
  • package ejava.examples.personnel.ejb20
  • import ejava.examples.personnel.Person
  • import javax.ejb.EntityBean
  • import javax.ejb.EntityContext
  • import javax.ejb.EJBException
  • import javax.naming.Context
  • import javax.naming.InitialContext
  • import javax.naming.NamingException
  • public abstract class PersonEJB implements
    PersonLBI, PersonBI, EntityBean
  • ...

27
Define the Abstract Accessors (CMP)
  • public abstract String getId()
  • public abstract void setId(String id)
  • public abstract String getFirstName()
  • public abstract void setFirstName(String
    fname)
  • public abstract String getLastName()
  • public abstract void setLastName(String
    lname)
  • public abstract String getAddress()
  • public abstract void setAddress(String
    address)
  • public abstract String getPhoneNumber()
  • public abstract void setPhoneNumber(String
    number)

28
Implement the Bean Class Business Methods
  • public Person getValues()
  • return new Person(getId(), getFirstName(),
    getLastName(),
  • getAddress(),
    getPhoneNumber())
  • public void setValues(Person values)
  • setFirstName(values.getFirstName())
  • setLastName(values.getLastName())
  • setAddress(values.getAddress())
  • setPhoneNumber(values.getPhoneNumber())
  • Real business methods would be much more
    complicated

29
Implement Bean ClassCreate Methods
  • //called prior to creating the Remote Object and
    inserting into the database
  • public PersonPK ejbCreate(String id)
  • setId(id) //primary key field must be
    set here!
  • return null //must return null in CMP
  • //called after associating with Remote Object and
    stored in database
  • public void ejbPostCreate(String id)

30
Implement Bean ClassCreate Methods (cont.)
  • public PersonPK ejbCreate(Person values)
  • setId(values.getId())
  • setFirstName(values.getFirstName())
  • setLastName(values.getLastName())
  • setAddress(values.getAddress())
  • setPhoneNumber(values.getPhoneNumber())
  • return null
  • public void ejbPostCreate(Person id)
  • public PersonPK ejbCreate(String id, String
    firstName, String lastName,
  • String address, String
    phone)
  • setId(id)
    setFirstName(firstName)
  • setLastName(lastName)
    setAddress(address)
  • setPhoneNumber(phone)
  • return null
  • public void ejbPostCreate(String id, String
    firstName, String lastName, String address,
    String phone)

31
Implement Bean ClassRemove Method
  • //called before object dis-associated from Remote
    Object and removed from database
  • public void ejbRemove()

32
Implement Bean ClassResource Management Methods
  • //Called after associated with a
    RemoteObject/removed from the bean pool and
  • // placed in the cache. Its not yet synchronized
    with database
  • public void ejbActivate()
  • //allocate any resources required for object
  • //Called prior to dis-associating from
    RemoteObject/returning the instance back to
  • // the bean pool and removing from cache. It has
    already been synchronized with
  • // the database.
  • Public void Passivate()
  • //release any allocated resources

33
Implement Bean ClassDatabase Synchronization
Methods
  • //Called at the start of a transaction, prior to
    the business method invocation
  • public ejbLoad()
  • //CMP - process public attributes that have
    been synchronized from the database
  • //BMP - obtain database connection and
    manually synchronize from database
  • //Called at the end of a transaction when
    container recognizes bean state change.
  • // weblogic augments this behavior with an
    isDirty() inspector.
  • public void ejbStore()
  • //CMP - prepare public attributes for
    synchronization to the database
  • //BMP - obtain database connection and
    manually synchronize to database

34
Implement Bean Class Finder Methods (BMP)
  • //Container returns Customer Remote Object to
    client
  • public CustomerPK ejbFindByPrimaryKey(CustomerPK
    key)
  • throws FinderException
  • // Perform database select statement to see if
    key.name is present
  • // if present return primary key, else throw
    FinderException
  • public javax.util.Collection ejbFindAllByLikeName(
    String name)
  • // Perform database select statement to locate
    all like matches
  • CMP finder methods are defined in the deployment
    descriptor and implemented by the Containers EJB
    compiler

35
Create Deployment Descriptor(ejb-jar.xml)
  • lt?xml version"1.0"?gt
  • lt!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems,
    Inc.//DTD Enterprise JavaBeans 2.0//EN'
    'http//java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd'gt
  • ltejb-jargt
  • ltdescriptiongt
  • This component is used to track basic
    information about people.
  • lt/descriptiongt
  • ltdisplay-namegtPersonnel Componentlt/display-name
    gt
  • ltenterprise-beansgt
  • ...
  • ltenterprise-beansgt
  • ltassembly-descriptorgt
  • ...
  • ltassembly-descriptorgt
  • lt/ejb-jargt

36
Define the Individual Beans
  • ltentitygt
  • lt!-- ejb-name
  • This field provides a unique name within
    this deployment
  • descriptor. There is no direct
    relationship between this
  • value and the resultant jndi context it is
    stored.
  • --gt
  • ltejb-namegtPersonlt/ejb-namegt
  • lthomegtejava.examples.personnel.ejb20.PersonRemot
    eHomelt/homegt
  • ltremotegtejava.examples.personnel.ejb20.PersonRem
    otelt/remotegt
  • ltlocal-homegtejava.examples.personnel.ejb20.Perso
    nLocalHome
  • lt/local-homegt
  • ltlocalgtejava.examples.personnel.ejb20.PersonLoca
    llt/localgt
  • ltejb-classgtejava.examples.personnel.ejb20.Person
    EJBlt/ejb-classgt

37
Define the Primary Key Class
  • ltpersistence-typegtContainerlt/persistence-typegt
  • lt!-- prim-key-class - lists the primary key
    class for the bean.
  • Note that this class is only _required_ when
    more than one
  • of the bean's fields makeup the primary key
    for the object.
  • Its public attributes must contain
    attributes that match in
  • name and type with the primary key
    attributes of the object.
  • One can use the primkey-field property if
    only a single
  • attribute is necessary.
  • --gt
  • ltprim-key-classgtejava.examples.personnel.ejb20.P
    ersonPKlt/prim-key-classgt
  • ltreentrantgtFalselt/reentrantgt

38
Define the Container ManagedFields (CMP)
  • ltabstract-schema-namegtPersonlt/abstract-sch
    ema-namegt
  • lt!-- cmp-field - describes a container
    managed attribute --gt
  • ltcmp-fieldgt ltfield-namegtidlt/field-namegt
    lt/cmp-fieldgt
  • ltcmp-fieldgt ltfield-namegtfirstNamelt/field-namegt
    lt/cmp-fieldgt
  • ltcmp-fieldgt ltfield-namegtlastNamelt/field-namegt
    lt/cmp-fieldgt
  • ltcmp-fieldgt ltfield-namegtaddresslt/field-namegt
    lt/cmp-fieldgt
  • ltcmp-fieldgt ltfield-namegtphoneNumberlt/field-namegt
    lt/cmp-fieldgt
  • lt!-- primkey-field - lists a single attribute
    within the bean
  • that acts as the primary key for the
    object. This is not
  • used when a primarykey-class is used
  • ltprimkey-fieldgtlt/primkey-fieldgt
  • --gt

39
Define Environment Properties
  • lt!-- env-entry - lists properties that will be
    placed in the
  • bean's environment at run-time.
  • --gt
  • ltenv-entrygt
  • ltdescriptiongtThis is a sample env
    entry.lt/descriptiongt
  • ltenv-entry-namegtexamplelt/env-entry-namegt
  • ltenv-entry-typegtjava.lang.Stringlt/env-entry-t
    ypegt
  • ltenv-entry-valuegtSample Environment
    Valuelt/env-entry-valuegt
  • lt/env-entrygt

40
Define EJB References
  • lt!-- ejb-ref
  • This contains the value in the code that
    the bean will use
  • to locate other beans. It won't be used in
    this bean.
  • --gt
  • lt!--
  • ltejb-refgt
  • ltdescriptiongtlt/descriptiongt
  • ltejb-ref-namegtlt/ejb-ref-namegt
  • ltejb-ref-typegtSession or Entitylt/ejb-ref-type
    gt
  • lthomegtlt/homegt
  • ltremotegtlt/remotegt
  • ltejb-linkgtlt/ejb-linkgt
  • lt/ejb-refgt
  • --gt

41
Define Security Role ReferencesHard Coded into
the Bean Class
  • lt!-- security-role-ref
  • Lists names or security roles specifically
    tested with the
  • code. This allows the assembler to map the
    code's value to
  • a role defined in the security-role
    section. This bean will
  • be using declarative security and thus
    will not use this
  • feature.
  • --gt
  • ltsecurity-role-refgt
  • ltdescriptiongtMyAdmin is hardcoded within
    bean.lt/descriptiongt
  • ltrole-namegtMyAdminltrole-namegt
  • ltrole-linkgtAdminltrole-linkgt
  • lt/security-role-refgt

42
Define Resource ReferencesHard Coded into the
Bean Class
  • lt!-- resource-ref
  • Lists resources the bean code is coded to
    search for in the
  • the environment. Examples of this would
    include connection
  • pools for Bean Managed Persistence. This
    will not be used
  • in this bean.
  • --gt
  • lt!--
  • ltresource-refgt
  • ltres-ref-namegtjdbc/personnelDBlt/res-ref-namegt
  • ltres-typegtjavax.sql.DataSourcelt/res-typegt
  • ltres-authgtContainerlt/res-authgt
  • lt/resource-refgt
  • --gt

43
Define Queries (CMP)
  • lt!-- query - defines queries to the container
    in terms of the java classes so that the
  • deployment descriptor stays
    portable while allowing the conatiner to map the
  • query to its persistence
    mechanism. --gt
  • ltquerygt
  • ltquery-methodgt
  • ltmethod-namegtfindByNamelt/method-namegt
  • ltmethod-paramsgt
  • ltmethod-paramgtjava.lang.Stringlt/method-
    paramgt lt!-- fname --gt
  • ltmethod-paramgtjava.lang.Stringlt/method-
    paramgt lt!-- lname --gt
  • lt/method-paramsgt
  • lt/query-methodgt
  • ltejb-qlgt
  • SELECT OBJECT(p) FROM Person p
  • WHERE p.firstName ?1 AND p.lastName
    ?2
  • lt/ejb-qlgt

44
Define Queries (CMP) (cont.)
  • ltquerygt
  • ltquery-methodgt
  • ltmethod-namegtfindAlllt/method-namegt
  • ltmethod-params/gt
  • lt/query-methodgt
  • ltejb-qlgt
  • SELECT OBJECT(p) FROM Person p
  • lt/ejb-qlgt
  • lt/querygt

45
Define Queries (CMP) (cont.)
  • ltquerygt
  • ltquery-methodgt
  • ltmethod-namegtfindByAddresslt/method-namegt
  • ltmethod-paramsgt
  • ltmethod-paramgtjava.lang.Stringlt/method-
    paramgt lt!-- addr --gt
  • lt/method-paramsgt
  • lt/query-methodgt
  • ltejb-qlgt
  • SELECT OBJECT(p) FROM Person p
  • WHERE p.address ?1
  • lt/ejb-qlgt
  • lt/querygt
  • lt/entitygt

46
Define Security Roles
  • lt!-- assembly-descriptor
  • This contains optional information that
    may be used during
  • application assembly.
  • --gt
  • ltassembly-descriptorgt
  • lt!-- security-role
  • This contains one or more names of
    security roles that will
  • interface with the bean.
  • --gt
  • ltsecurity-rolegt
  • ltrole-namegtAdminlt/role-namegt
  • lt/security-rolegt
  • ltsecurity-rolegt
  • ltrole-namegtUserlt/role-namegt
  • lt/security-rolegt

47
Define Method Restrictions
  • ltmethod-permissiongt
  • ltdescriptiongtThese are the writable
    methods.lt/descriptiongt
  • ltrole-namegtAdminlt/role-namegt
  • ltmethodgt
  • lt!-- ejb-name - names a bean from the
    deployment descriptor --gt
  • ltejb-namegtPersonlt/ejb-namegt
  • lt!-- method-intf - Remote, Home, Local, or
    LocalHome. Required
  • only if appears in multiple interfaces
    and must
  • distringuish between them.
  • --gt
  • ltmethod-intfgtHomelt/method-intfgt
  • ltmethod-namegtcreatelt/method-namegt
  • lt!-- method-params - Required only of method
    overloaded. --gt
  • ltmethod-paramsgt
  • ltmethod-paramgtjava.lang.Stringlt/method-par
    amgt lt!-- id --gt
  • lt/method-paramsgt
  • lt/methodgt

48
Define Method Restrictions
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtcreatelt/method-namegt
  • ltmethod-paramsgt
  • ltmethod-paramgtjava.lang.Stringlt/method-para
    mgt lt!-- id --gt
  • ltmethod-paramgtjava.lang.Stringlt/method-para
    mgt lt!-- firstName --gt
  • ltmethod-paramgtjava.lang.Stringlt/method-para
    mgt lt!-- lastName --gt
  • ltmethod-paramgtjava.lang.Stringlt/method-para
    mgt lt!-- address --gt
  • ltmethod-paramgtjava.lang.Stringlt/method-para
    mgt lt!-- phNumber --gt
  • lt/method-paramsgt
  • lt/methodgt
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtcreatelt/method-namegt
  • ltmethod-paramsgt
  • ltmethod-paramgtejava.examples.personnel.Pers
    onlt/method-paramgt
  • lt/method-paramsgt
  • lt/methodgt

49
Define Method Restrictions
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtremovelt/method-namegt
  • ltmethod-paramsgt
  • ltmethod-paramgtjava.lang.Objectlt/method-para
    mgt
  • lt/method-paramsgt
  • lt/methodgt
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtremovelt/method-namegt
  • ltmethod-paramsgt
  • ltmethod-paramgtjavax.ejb.Handlelt/method-para
    mgt
  • lt/method-paramsgt
  • lt/methodgt
  • ...
  • lt/method-permissiongt

50
Define Method Restrictions
  • lt/method-permissiongt
  • ltmethod-permissiongt
  • ltdescriptiongtThese are the read-only
    methodslt/descriptiongt
  • ltrole-namegtAdminlt/role-namegt
  • ltrole-namegtUserlt/role-namegt
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtfindByPrimaryKeylt/method-namegt
  • lt/methodgt
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtfindByNamelt/method-namegt
  • lt/methodgt
  • ...
  • lt/method-permissiongt

51
Define Transaction Properties
  • lt!-- container-transaction
  • These properties allow the assembler
    to determine the transactional properties of each
    bean method. --gt
  • ltcontainer-transactiongt
  • ltdescriptiongtlt/descriptiongt
  • ltmethodgt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltmethod-namegtlt/method-namegt
  • lt/methodgt
  • lt!-- trans-attribute
  • NotSupported, Supports, Required,
    RequiresNew, Mandatory, or Never
  • --gt
  • lttrans-attributegtRequiredlt/trans-attributegt
  • lt/container-transactiongt
  • lt/assembly-descriptorgt

52
Define Client Jar File
  • lt!-- ejb-client-jar
  • Specifies the jar file that contains
    classes for use by remote
  • clients of the component.
  • --gt
  • ltejb-client-jargtPersonnelClient.jarlt/ejb-client
    -jargt
  • lt/ejb-jargt

53
Defining Database-Specific Mapping(weblogic-cmp-r
dbms-jar.xml)
  • lt!DOCTYPE weblogic-rdbms-jar PUBLIC
  • '-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB
    RDBMS Persistence//EN'
  • 'http//www.bea.com/servers/wls600/dtd/weblogic-r
    dbms20-persistence-600.dtd'gt
  • ltweblogic-rdbms-jargt
  • ltweblogic-rdbms-beangt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltdata-source-namegtweblogic.jdbc.dataSource.MyJ
    DBC Data Source
  • lt/data-source-namegt
  • lttable-namegtPersonlt/table-namegt
  • lt/weblogic-rdbms-beangt
  • ltcreate-default-dbms-tablesgtFalselt/create-default
    -dbms-tablesgt
  • lt/weblogic-rdbms-jargt

54
Map Fields to Columns
  • ltfield-mapgt
  • ltcmp-fieldgtidlt/cmp-fieldgt
  • ltdbms-columngtidlt/dbms-columngt
  • lt/field-mapgt
  • ltfield-mapgt
  • ltcmp-fieldgtfirstNamelt/cmp-fieldgt
  • ltdbms-columngtfirstNamelt/dbms-columngt
  • lt/field-mapgt
  • ltfield-mapgt
  • ltcmp-fieldgtlastNamelt/cmp-fieldgt
  • ltdbms-columngtlastNamelt/dbms-columngt
  • lt/field-mapgt
  • ...

55
Map Container-Specific Resources(weblogic-ejb-jar
.xml)
  • lt?xml version"1.0"?gt
  • lt!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA
    Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN'
    'http//www.bea.com/servers/wls600/dtd/weblogic-ej
    b-jar.dtd'gt
  • ltweblogic-ejb-jargt
  • ltweblogic-enterprise-beangt...
  • lt/weblogic-enterprise-beangt
  • ltsecurity-role-assignmentgt
  • ltrole-namegtAdminlt/role-namegt
  • ltprincipal-namegtejava-instructorlt/principal-
    namegt
  • lt/security-role-assignmentgt
  • ltsecurity-role-assignmentgt
  • ltrole-namegtUserlt/role-namegt
  • ltprincipal-namegteveryonelt/principal-namegt
  • lt/security-role-assignmentgt
  • lt/weblogic-ejb-jargt

56
Define Bean(weblogic-ejb-jar.xml)
  • ltweblogic-enterprise-beangt
  • ltejb-namegtPersonlt/ejb-namegt
  • ltentity-descriptorgt
  • ltpersistencegt
  • ltpersistence-typegt
  • lttype-identifiergtWebLogic_CMP_RDBMSlt/type-
    identifiergt
  • lttype-versiongt6.0lt/type-versiongt
  • lttype-storagegtMETA-INF/weblogic-cmp-rdbms-
    jar.xmllt/type-storagegt
  • lt/persistence-typegt
  • ltpersistence-usegt
  • lttype-identifiergtWebLogic_CMP_RDBMS
    lt/type-identifiergt
  • lttype-versiongt6.0lt/type-versiongt
  • lt/persistence-usegt
  • lt/persistencegt
  • lt/entity-descriptorgt
  • ltjndi-namegtPersonRemoteHomelt/jndi-namegt
  • ltlocal-jndi-namegtPersonLocalHomelt/local-jndi
    -namegt
  • lt/weblogic-enterprise-beangt

57
Entity Bean Resource Management
  • Bean instances are managed by the container to
    conserve resources and increase performance
  • pooling, caching
  • Our CustomerBean must implement required
    lifecycle callbacks
  • mimic CustomerHome create,find,remove
  • implement EntityBean
  • Cache
  • Activation/Passivation

58
Database Synchronization
  • As with all EJBs, only a single thread can be
    running within a bean instance
  • Multiple entity bean instances can be created to
    represent same data in database
  • Consistency ?
  • ejbLoad() and ejbStore() are called to
    synchronize bean with underlying storage

59
State Management
  • State synchronization methods
  • ejbLoad
  • ejbStore
  • Resource Management methods
  • ejbActivate
  • ejbPassivate

60
Entity Bean Lifecycle
  • Does Not Exist
  • Just in the jar file. Before creation or after
    finalization
  • Pooled
  • Exists but does not represent any particular
    database entity i.e. not associated with an
    EJBObject
  • service find() requests
  • Ready
  • assigned to an EJBObject
  • can handle client request for specific PK

61
Lifecycle (Cont)
  • Bean callback methods are invoked as the
    container moves the bean instance through these
    states
  • Action required depends on entity bean type
  • Container Managed Persistence (CMP)
  • Bean Managed Persistence (BMP)

62
Entity Bean Types
  • Bean can have total control over loading and
    storing from database
  • Bean Managed Persistence (BMP)
  • Container can take over this responsibility
  • Container Managed Persistence (CMP)
  • Specialized Implementations
  • Toplink for relational databases
  • CICS

63
Bean Managed Persistence
  • BMP

64
Bean Managed Persistence (BMP)
  • The CustomerBean used BMP
  • implemented all database interaction
  • create, remove, and find
  • Have to do object-relational mapping
  • Synchronize bean state with database when
    requested by container

65
Creation
  • Container populates pool with unassociated bean
    implementations
  • calls no-argument constructor
  • sets entity context
  • Client calls create on home
  • create sent to bean impl. Populates attributes
    and inserts row in database
  • EJBObject created for client

66
Creation (Cont)
  • Bean implementation is associated with EJBObject
  • ejbPostCreate() called on bean instance
  • bean is now in the ready state
  • Client invokes business method on EJBObject
  • EJBObject manages security, transactions, etc.
  • Call delegated to bean instance

67
(No Transcript)
68
Removal
  • Client calls remove on Home or EJBObject
  • Container manages security, transactions, etc.
  • remove delegated to beans ejbRemove() method
  • removes data from database
  • bean moves to pooled state

69
(No Transcript)
70
State Synchronization
  • When in the ready state, beans frequently need to
    be synchronized with the underlying data store
  • typically at transactional boundaries
  • ejbLoad() is called when bean state needs to
    re-read from database
  • ejbStore() is called when bean state needs to be
    written to database

71
(No Transcript)
72
Load and Store
  • ejbLoad
  • Use EntityContext.getPrimaryKey()
  • read attributes from database
  • ejbStore
  • write attributes to database
  • Only occur when the bean is in the ready state
  • Associated with an EJBObject/PrimaryKey

73
Activation and Passivation
  • Beans can be moved to the pooled state
  • Passivation
  • A few bean instance can service many clients
  • As a result of a remove() call
  • Can transition from pooled to ready state
  • create()
  • find()
  • activation()

74
(No Transcript)
75
Activation and Passivation
  • When Passivated, bean should give up any
    resources held on behalf of the data represented
    by the Primary Key
  • When Activated, bean can acquire and resources
    specific to the Primary Key

76
BMP Summary
  • Have to handle all database interaction
  • except transactions
  • ejbLoad() and ejbStore() called when bean
    instance state must be synchronized with database
  • ejbActivate() and ejbPassivate() called when bean
    is moved between the ready state and pooled state

77
Container Managed Persistence
  • CMP

78
Container Managed Persistence CMP
  • Controlled by deployment descriptor
  • Create, find, and remove can be implemented by
    the container
  • Container can synchronize bean state and the
    database for us
  • Affects lifecycle operations
  • Callbacks tell us something has occurred vs. you
    must do something now

79
CMP Creation
  • Notification that instance is about to be created
    in database
  • Simply need to set attributes
  • public CustomerPK ejbCreate( String name, String
    address )
  • this.name name
  • this.address address
  • return null

80
CMP Creation (Cont)
  • ejbPostCreate( String name, String address )
  • called after association with EJBObject
  • EntityContext is now meaningful
  • action is not required

81
(No Transcript)
82
CMP Remove
  • Notification that client is removing the data for
    the associated Primary Key.
  • Prepare for removal
  • Container will actually remove the data from the
    database

83
(No Transcript)
84
State Synchronization
  • Synchronize bean state with database
  • Usually at transactional boundaries
  • ejbLoad and ejbStore invoked by container
  • CMP
  • Informational message that container has either
  • just loaded state from database
  • is ready to store state to database
  • Opportunity to pre or post-process attributes

85
(No Transcript)
86
Activation/Passivation
  • Resource Management
  • Called when bean is moved from ready state to
    pooled state.
  • Disassociated with EJBObject/Primary Key
  • Release/Acquire resources for specific entity

87
CMP Finders
  • Declared in home interface
  • Implementation can be automatically generated by
    the container

88
CMP Summary
  • Eliminates a lot of code, but capability is still
    limited
  • less code means fewer bugs
  • Problems
  • persistence declaration not sufficiently
    standardized
  • many containers can only map a bean to one table

89
Entity Bean Performance Issues
90
Performance Issues
  • When could Entity Beans be expensive
  • CMP performs eager database synchronization on
    each transaction
  • BMP allows for lazy database synchronization
  • Who should call the Entity Beans Remote methods
    ?
  • Clients should call session bean operations that
    operate on persistent data
  • Should you utilize entity beans at all ?
  • Performance hit - all calls go through the
    container via the EJBObject
  • Higher performance going directly to the
    databases vs. functionality of container

91
Entity Bean Summary
  • Models persistent state in database
  • Facilitates declarative persistence
  • BMP vs. CMP
  • Performance implications
  • wrap calls to entity beans with session beans

92
Packaging EJBs and Enterprise Applications
93
JAR File Architecture
myManifest.mf Class-Path ejava.jar
  • Utility JARs
  • contain classes that are common to several EJB
    and WAR archives
  • EJB JARs
  • contain deployable EJB code and deployment
    descriptors
  • Web WARs
  • contain deployable Web code and deployment
    descriptors

//build EJB jar file with required manifest
dependency cd ltstaging dirgt jar cvfm
.ltproduction dirgt/Personnel20.jar myManifest.jar

94
Utility.jar File(s) (ejava.jar)
  • jar tf ejava.jar
  • META-INF/
  • META-INF/MANIFEST.MF
  • ejava/examples/personnel/Person.class
  • ejava/examples/personnel/ejbclient/PersonAccessor
    1.class
  • ejava/examples/personnel/ejbclient/PersonAccessor.
    class

95
EJB.jar File(s) (Personnel20.jar)
  • META-INF/MANIFEST.MF
  • META-INF/ejb-jar.xml
  • META-INF/weblogic-ejb-jar.xml
  • META-INF/weblogic-cmp-rdbms-jar.xml
  • ejava/examples/personnel/ejb20/PersonLBI.class
  • ejava/examples/personnel/ejb20/PersonLocal.class
  • ejava/examples/personnel/ejb20/PersonBI.class
  • ejava/examples/personnel/ejb20/PersonRemote.class
  • ejava/examples/personnel/ejb20/PersonEJB.class
  • ejava/examples/personnel/ejb20/PersonRemoteHome.cl
    ass
  • ejava/examples/personnel/ejb20/PersonPK.class
  • ejava/examples/personnel/ejb20/PersonLocalHome.cla
    ss
  • ejava/examples/personnel/ejb20/PersonEJB_vjvtzf_Ho
    meImplRTD.xml
  • ejava/examples/personnel/ejb20/PersonEJB_vjvtzf_EO
    ImplRTD.xml
  • ejava/examples/personnel/ejb20/Person_WebLogic_CMP
    _RDBMS.class
  • ejava/examples/personnel/ejb20/PersonEJB_vjvtzf_EL
    OImpl.class
  • ejava/examples/personnel/ejb20/PersonEJB_vjvtzf_EO
    Impl.class
  • ejava/examples/personnel/ejb20/PersonEJB_vjvtzf_Ho
    meImpl.class
  • ejava/examples/personnel/ejb20/PersonEJB_vjvtzf_Lo
    calHomeImpl.class

96
Web.war File(s) (personnel.war)
  • jar tf personnel.war
  • META-INF/MANIFEST.MF
  • CoreLoginForm.jsp
  • ExceptionPage.jsp
  • InsertPerson.jsp
  • InsertPersonEJB.jsp
  • InsertPersonEntity.jsp
  • LoginErrorForm.jsp
  • LoginForm.jsp
  • PersonEntryForm.jsp
  • WEB-INF/classes/ejava/examples/personnel/Persons.c
    lass
  • WEB-INF/classes/ejava/examples/personnel/dao/
  • WEB-INF/classes/ejava/examples/personnel/dao/Perso
    nJDBC.xml
  • WEB-INF/web.xml
  • WEB-INF/weblogic.xml
  • welcome.html

97
EAR File (PersonnelApp.ear)
  • jar tf PersonnelApp.ear
  • META-INF/
  • META-INF/MANIFEST.MF
  • META-INF/application.xml
  • Personnel20.jar
  • personnel.war
  • ejava.jar

98
EAR Deployment Descriptor(application.xml)
  • cat META-INF/application.xml
  • lt!DOCTYPE application PUBLIC "-//Sun
    Microsystems, Inc.//DTD J2EE Application 1.
  • 2//EN" "http//java.sun.com/j2ee/dtds/application_
    1_2.dtd"gt
  • ltapplicationgt
  • ltdisplay-namegtPersonnel Applicationlt/display-na
    megt
  • ltmodulegtltejbgtPersonnel20.jarlt/ejbgtlt/modulegt
  • ltmodulegt
  • ltwebgt
  • ltweb-urigtpersonnel.warlt/web-urigt
  • ltcontext-rootgtpersonnelapplt/context-rootgt
  • lt/webgt
  • lt/modulegt
  • lt/applicationgt
Write a Comment
User Comments (0)
About PowerShow.com