CISH6960: Developing Enterprise Applications - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

CISH6960: Developing Enterprise Applications

Description:

Represent persistent features of ... Navigability. Customer to Address but not back ... Can enable or disable navigability. 7/18/09. 42. Entity Beans Summary ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 61
Provided by: kenneth116
Category:

less

Transcript and Presenter's Notes

Title: CISH6960: Developing Enterprise Applications


1
CISH-6960 Developing Enterprise Applications
  • Lecture 8
  • July 17, 2004

2
Enterprise JavaBeans
  • Entity Beans

3
Entity Beans
  • Represent the persistence layer in J2EE
    applications
  • As with session beans, entity beans require
  • Home interface
  • Object interface
  • Bean class
  • In addition, entity beans require
  • Primary key class

4
Role of Entity Beans
  • Represent persistent features of J2EE
    applications
  • Essentially an Object Oriented view of a
    persistence structure
  • The persistence structure is most often a
    relational database
  • Other components rely on entity beans as an
    object-oriented persistence layer
  • EJB Container manages the beans

5
Lifecycle of Entity Beans
  • Entity beans are required to be robust and
    support concurrent operations
  • Closely associated with database operations
  • Method calls on interface for create, find,
    update, and remove
  • Code to support these operations can be
  • Supplied by the developer
  • Generated by the application server
  • In either case, the operations themselves will be
    called by the container

6
Lifecycle of Entity Beans
7
Lifecycle of Entity Beans
  • Pooled state
  • Container manages when instances are created or
    destroyed
  • Pooled instances have not yet been loaded with
    database data
  • When clients make requests, instances are removed
    from the pool and associated with data
  • Pooled instances can be used to service requests
    that do not require database data already present
  • Can do queries, for example

8
Lifecycle of Entity Beans
  • Ready state
  • Can accept client requests for business methods
  • Generally client requests come from session beans
    (the Session Façade design pattern)
  • When a create call is received
  • The container removes an instance from the pool
  • Creates an EJBObject interface
  • Calls create lifecycle methods on the bean
  • Associates the bean with a primary key object
  • Moves the bean to the Ready state

9
Lifecycle of Entity Beans
  • If a query is performed
  • An instance is pulled from the pool for every
    record returned
  • Each is bound to an EJBObject interface
  • Activated when clients call methods on them
  • Entity beans can also be activated after having
    been previously passivated
  • Complementary processes used by the container to
    manage the pool of available instances
  • Switching between pooled and ready states allows
    the container to act like it has more instances
    available than it really does

10
Types of Entity Beans
  • Entity beans come in two types
  • Container Managed Persistent (CMP)
  • Container generates code to persist the objects
    state
  • Developer chooses fields to be persisted
  • Container produces code to do the job
  • Very flexible and easy to create
  • Bean Managed Persistent (BMP)
  • Developer provides persistence code
  • Allows complex schemas to be represented

11
Entity Beans
12
Entity Beans
  • Support both local and remote interfaces
  • In most cases, local interfaces are preferred
  • Do more than represent data
  • They manage state
  • Carry the state of the object through operations
    that modify it
  • Ensure that integrity is maintained
  • Bean class for BMP must implement the
    javax.ejb.EntityBean interface

13
EntityBean Interface
14
EntityBean Interface
  • Context methods similar to session beans
  • Enable the container to bind or unbind a specific
    instance
  • Still have activate and passivate methods
  • Not used the same way, since database is
    available
  • Can still be used to notify an instance that it
    is being moved into or out of instance pool
  • Can close or restore connections to transient
    resources
  • Three import persistence methods
  • The ejbLoad() method
  • The ejbStore() method
  • The ejbRemove() method
  • Implemented in the bean class for BMP beans

15
EntityBean Interface
  • The ejbLoad() method
  • Uses the SQL SELECT statement
  • The ejbStore() method
  • Uses the SQL UPDATE statement
  • The ejbRemove() method
  • Uses the SQL DELETE statement
  • Insertion handled through create() methods
  • Use SQL INSERT statements
  • Also needed ejbPostCreate() methods
  • One for each create() with matching arguments
  • Called immediately after create() finishes

16
EntityBean Interface
  • The ejbPostCreate() methods
  • Some operations cannot be performed until the
    bean is fully available
  • This method is for those operations
  • Example is for a bean to acquire its own object
    reference
  • Cant be done until the bean fully exists

17
Example EntityBean
18
Example Bean Class 1
19
Example Bean Class
  • Create methods
  • Return reference to primary key class
  • Use JDBC to perform a SQL INSERT using a
    PreparedStatement
  • The ejbPostCreate() methods are empty (very
    common)
  • The load() method uses the EntityContext to get
    the primary key, then does a SQL SELECT
  • Additional home method added, to be discussed
    shortly
  • Business methods promoted to Object interface

20
Example Bean Class
  • Entity beans also use finder methods
  • Query statements to select beans matching
    particular criteria
  • The ejbFindByPrimaryKey() is required
  • Promoted to Home interface
  • Argument is of type primary key class
  • Multiple finders can be written, following the
    same naming convention
  • Finders return a single object references, or a
    reference of type java.util.Collection

21
Example Bean Class
22
Example Bean Class
  • New in EJB v 2.0 are Home methods
  • Declare methods added to Home interface
  • Can be used for methods that do not involve a
    particular bean, but focus on an entire set
  • In bean class, must begin with ejbHome and have
    a corresponding method in the Home interface
  • public int ejbHomeGetPersonCount()
  • public int getPersonCount()

23
Primary Key Class
  • Java representation of the primary key
  • Container compares instances to identify
    individual beans
  • Must override the boolean equals( Object )
    method
  • Must override the int hashCode() method
  • Must be Serializable
  • Can be compound, i.e., involve combinations of
    fields
  • String and the wrapper classes (Integer, Double,
    etc) all work already
  • If the primary key in the database table is an
    integer, can use java.lang.Integer as the primary
    key class

24
Example Bean Class 2
25
EntityBean Interfaces
  • Home interface represents lifecycle operations
  • Since entity beans are an object representation
    of the database data, must support many stateful
    operations
  • Standard CRUD methods (create, retrieve, update,
    delete)
  • Must implement javax.ejb.EJBHome or
    javax.ejb.EJBLocalHome
  • Can have many create() methods
  • Each must have a corresponding ejbCreate() and
    ejbPostCreate() in the bean class, with matching
    arguments

26
EntityBean Interfaces
  • In EJB v 2.0, can have multiple create methods
    with different suffixes
  • Still must have corresponding ejbCreate and
    ejbPostCreate methods

27
EntityBean Interfaces
  • Home interface also has finder methods
  • In BMP, add logic for finder to bean class and
    signature to Home interface
  • Must have ejbFindByPrimaryKey() in bean class
  • Corresponding findByPrimaryKey() in Home
    interface is optional, in case dont want client
    access
  • Finders have parameters
  • Finders can use suffixes as above
  • All finders begin with ejbFind
  • Finders return either a single EJB reference or a
    Collection

28
Sample Finder Method
29
Other Methods
  • Home methods involve multiple instances
  • Remove method actually removes data from the
    database
  • Different from SessionBean behavior
  • Object interface often has getters and setters,
    but may have other methods
  • Entity beans primarily use local interfaces for
    efficiency
  • Session beans accessed remotely provide a façade
    to the entity beans

30
Deployment Descriptor
  • Very similar to that for session bean, but with
    additional information
  • Persistence type
  • Primary key class
  • CMP beans do much more with the deployment
    descriptor

31
Container Managed Persistence
  • CMP relies on the container for much more of the
    basic structure
  • CMP bean class is abstract
  • Container subclasses it to generate persistence
    code
  • Information required by the container is supplied
    in the deployment descriptor

32
Generated Bean Class
  • Bean class is abstract
  • Contains abstract get and set methods, but not
    the corresponding attributes
  • Methods available for getting, setting, and
    unsetting the entity context
  • The create() method calls the setter for the
    argument
  • Finder methods are generated from the deployment
    descriptor

33
Adding Methods
  • Finder methods allow definition of the query
    using EJBQL, a SQL-like query language
  • Discussed in more detail below
  • Select methods are similar to finder methods
  • Finder returns object reference or a Collection
  • Select methods return a Collection of fields that
    meet the criteria
  • selectLastNames() returns the set of all last
    names in all the beans of this type
  • Private to the bean, unlike finder methods
  • Relationships can be defined as well
  • Discussed further below

34
Deployment Descriptor for CMP
35
Vendor Specific Files
  • Vendor specific information
  • Binding
  • Contains JNDI binding information
  • Datasources used for CMP
  • Extensions
  • Used for additional functionality provided by
    vendor beyond the specification

36
EJB Query Language
  • With BMP, the finders are implemented directly in
    the bean class
  • May not be portable
  • CMP is generated along with the mapping to the
    database
  • EJB Query Language (EJBQL) is designed to be
    portable across schemas and databases

37
EJB Query Language
  • EJBQL is a subset of SQL
  • A few features have been added to EJBQL beyond
    SQL
  • Many SQL features not supported
  • Example
  • SELECT object(o) from PersonCMP o WHERE o.name
    ?1
  • SELECT, FROM, and WHERE all work as expected
  • Queries are object-oriented, rather than
    relational based
  • Use names from the beans rather than those in the
    database
  • OBJECT used to return beans or collections
  • Can return a specific field
  • SELECT o.bday FROM PersonCMP WHERE o.name ?1
  • Returns the birthday (int) from all people
    matching the name
  • Arguments follow question marks ?1, ?2, ?3

38
EJB Query Language
39
EJB Query Language
  • WHERE operator has many options
  • BETWEEN for a range
  • IN for a choice in a group
  • NULL, NOT NULL
  • IS EMPTY
  • MEMBER OF
  • LIKE
  • SELECT DISTINCT results in a single return value
    rather than multiples in a collection
  • Using the wizard, EJBQL statements are added to
    the deployment descriptor for CMP beans

40
CMP Relationships
  • Relationships can be defined in the deployment
    descriptor
  • Cardinality
  • One-to-one
  • Employee to (primary mailing) Address
  • One-to-Many
  • Company to Employee
  • Many-to-Many
  • Restaurant to Customer
  • Navigability
  • Customer to Address but not back
  • Database stores relations in foreign keys
    references

41
Creating a Relationship
  • Beans must be in the same EJB module
  • Only CMP beans can be selected
  • After selecting the beans, can move to the second
    stage, which defines the relationship
  • Can select multiplicity
  • Address stores key reference to Person
  • Container generates code for getting or setting
    Address for a particular Person
  • Methods are named using the Role Name dialog
  • Can enable or disable navigability

42
Entity Beans Summary
  • Entity beans are an object interface to
    persistent data
  • Normally use local interfaces, which are accessed
    by session beans or other entity beans
  • CMP beans are abstract classes
  • Persistence information defined in deployment
    descriptor
  • BMP beans have SQL code in the bean class
  • Home methods work across beans
  • Select methods query across beans
  • EJB Query Language used to supply SQL for CMP
  • Relationships between CMP beans can be defined

43
Enterprise JavaBeans
  • Message-Driven Beans

44
Objectives
  • Design and Develop Message-Driven EJBs
  • Configure JMS connection factories and
    destinations

45
Introduction
  • Message-Driven Beans (MDB) were added in EJB v
    2.0
  • Decouple logic from the request-response paradigm
  • A MDB is a Java Message Service (JMS) consumer
  • Never exercised directly by client
  • Work asynchronously with application logic to
    consume messages sent to certain destinations

46
A JMS Primer
  • Messaging
  • Means for robustly delivering and consuming
    asynchronous messages in a Java environment
  • JMS presents a number of APIs for use as
    interfaces for working with message services
  • Product providers implement the interfaces
  • Provides a vendor neutral means for accessing JMS
    providers
  • JMS is analogous to JDBC in this way

47
JMS Clients
  • Two types of clients
  • Producers
  • Create messages and send them to a destination
  • Consumers
  • Receive messages and process their contents
  • A client can be both a producer and a consumer
  • Message service decouples producers from
    consumers
  • Asynchronous processing
  • The producer does not have to wait for a response
    from the consumer after sending a message

48
JMS
  • Producers send messages to a destination and
    continues on with its processing
  • Consumers are linked to the destination and
    receive messages from it

49
JMS Destinations
  • A destination is an abstract item
  • Represents a message service established to
    receive and deliver related messages
  • Destinations are administered objects
  • System administrators create them and bind them
    to servers under a JNDI name
  • Clients use JNDI to find the destinations
  • Two standard types of destinations
  • Topics
  • Queues

50
Basic Administration
  • Messaging is configured in the application server
  • Add destinations
  • Add connection factories
  • Single provider can support multiple destinations
  • Similar to how a single database server can
    support multiple tables
  • To acquire a destination, clients use a
    connection factory
  • Specific to servers and destination types

51
Topics and Queues
  • Topics are one-to-many destinations
  • Also called publish/subscribe, or pub/sub
  • Consumers subscribe to a topic
  • Producers publish messages to it
  • Queues are one-to-one destinations
  • Also called point-to-point (p2p)
  • Whoever gets the message first consumes it

52
Pooling
  • Application servers can manage pools of consumers
    for a particular destination
  • Can rapidly manage large queues
  • Bean instances managed to receive messages as
    required

53
Messages
  • A JMS message has a header and a body
  • The header contains some optional,
    producer-defined properties
  • Body contents depends on type of message
  • Each message type has methods for extracting and
    processing the contents

54
Message Types
  • javax.jms.ObjectMessage
  • Encapsulates a single, Serialized object
  • Has getObject() and setObject(Serializable)
    methods
  • javax.jms.ByteMessage
  • Contains a stream of uninterrupted bytes
  • javax.jms.MapMessage
  • Used to send name/value pairs
  • Names are Strings, values are primitives
  • javax.jms.StreamMessage
  • Used for a stream of primitive types
  • javax.jms.TextMessage
  • Used to send a message containing an instance of
    java.lang.String

55
Developing an MDB
  • Message-driven beans are stateless, transactional
    objects managed by the EJB container
  • Do not present a client view
  • Are linked to a destination
  • Receive messages from that destination as sent by
    producers
  • Has a lifecycle very similar to a stateless
    session bean
  • MDB requires bean class and entry in deployment
    descriptor only
  • No interfaces required

56
MDB Lifecycle
57
The Bean Class
  • MDBs implement two interfaces
  • javax.jms.MessageListener
  • public void onMessage( Message msg )
  • This method is called when a new message is
    received
  • The argument is cast to the proper message type
    before extracting its contents
  • javax.ejb.MessageDrivenBean
  • public void setMessageDrivenContext(
    MessageDrivenContext ctx) throws EJBException
  • public void ejbRemove() throws EJBException
  • Must also be a single ejbCreate() method with no
    arguments
  • Called by the container on creation

58
Example MDB
59
Deployment Descriptor
  • No other entries or interfaces required
  • MDBs are consumers attached to a particular
    destination

60
Message-driven Beans Summary
  • Message-driven beans are consumers attached to
    destinations
  • Not accessible to clients, so no additional
    interfaces required
  • MDBs allow asynchronous communication
  • MDBs implement two interfaces
  • javax.jms.MessageListener
  • javax.ejb.MessageDrivenBean
  • Two types of Destinations
  • Topics for public/subscribe operations
  • Queues for point-to-point operations
  • Five types of messages ObjectMessage,
    ByteMessage, MapMessage, StreamMessage,
    TextMessage
  • Destinations are configured on the server using
    connection factories
  • Topics and Queues are bound to the server under
    JNDI names
Write a Comment
User Comments (0)
About PowerShow.com