CISH6960: Developing Enterprise Applications - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

CISH6960: Developing Enterprise Applications

Description:

Thousands of connections per hour, accessing limited resources ... Gamma, Helm, Johnson, and Vlissides, Design Patterns: Elements of Reusable, ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: CISH6960: Developing Enterprise Applications


1
CISH-6960 Developing Enterprise Applications
  • Lecture 7
  • July 10, 2004

2
Enterprise JavaBeans
  • Fundamentals and Session Beans

3
Introduction
  • Web-based Java applications
  • Many companies currently use
  • Java classes
  • Model
  • JavaServer Pages
  • View
  • Java servlets
  • Controller
  • JDBC to talk to databases

4
J2EE Services
  • This is often good enough, but what about
  • Security
  • Persistence
  • Lifecycle management
  • Transactions
  • Distributed programs
  • Remote access
  • Concurrency
  • These are collectively known as services

5
Container Managed Services
  • Some services
  • Concurrency
  • Thousands of connections per hour, accessing
    limited resources
  • Handles threading and resource issues
  • Security
  • XML files declare authentication and
    authorization info
  • Transactions
  • XML files declare which methods must be in
    transactions
  • Method-by-method control available

6
Container Managed Services
  • Other services
  • Location transparency
  • Objects accessed through a searchable registry
  • Object locations can move without affecting the
    client
  • Distributed systems
  • Server operates across systems using RMI
  • Persistence
  • Container can provide object/relational mappings
    and update them as necessary
  • Can even ensure referential integrity
  • Can be controversial, but is getting better

7
Container Managed Services
  • How do we make all this happen?
  • Create components, instead of classes
  • Install components inside application servers
  • Configure components, servers, and services using
    deployment descriptors
  • Its no longer simple to do simple things
  • Its relative easy to do complicated things
  • If you dont want or need these services, you
    dont need EJBs

8
Containers
9
The EJB Architecture
  • Existing technologies predating EJBs
  • Transaction managers
  • Distribution services
  • Persistence mechanisms
  • Common Object Request Broker Architecture (CORBA)
  • Specifications governed by the Object Management
    Group (OMG)
  • EJBs borrow heavily from this technology, along
    with RMI

10
The EJB Architecture
  • CORBA services incorporated into EJBs
  • Naming
  • Lifecycle management
  • Security
  • Persistence
  • Distribution structures
  • CORBA is language neutral, using interfaces
    written in Interface Definition Language (IDL)
  • CORBA uses Object Request Brokers (ORBs) for
    networking and data marshalling
  • Data marshalling between languages could be
    difficult

11
The EJB Architecture
  • Java has Remote Method Invocations (RMI)
  • Relatively easy to work with
  • Requires Java on both ends
  • Supports communication with the JVM
  • Does not offer many enterprise services
  • EJB architecture combines features of CORBA and
    RMI
  • Tries to eliminate any weaknesses they may have
  • Loses language independence
  • Maintains the services
  • Data marshalling handled using RMI and Java

12
The EJB Architecture
  • Developers must provide a way to access objects
    in a remote, managed environment
  • The EJBs must be written in a manner the
    environment understands
  • EJB container provides services to the beans
  • Must be a well-defined relationship between the
    EJB component and the EJB container
  • Developers take advantage of container services

13
The EJB Architecture
  • Object interface
  • Provides client access to the bean
  • Method calls execute business logic on the bean
  • Specific to a given EJB
  • Ensures the container is involved in every
    request sent to the EJB
  • Home interface
  • Provides lifecycle methods for the EJB
  • Can create, remove, and/or find beans
  • The home and object interfaces are client views
    on the EJB

14
The EJB Architecture
  • Each of the interfaces can have two types
  • Remote
  • Used for accessing beans outside the JVM managing
    the bean class
  • Restricts access to pass by value, and parameters
    must be Serializable
  • Local
  • Used by clients that share a JVM with the bean
  • Do not involve networking
  • Use pass by reference semantics
  • Allow association with other EJBs

15
Architecture
16
Enterprise JavaBeans
  • Session beans
  • Stateless
  • Stateful
  • Entity beans
  • CMP
  • BMP
  • Message-driven beans (EJB 2.0)

17
EJBs
Enterprise JavaBeans
Message Driven Beans
Session Beans
Entity Beans
Stateless
Stateful
Container Managed
Bean Managed
18
EJBs
  • Session beans represent processes
  • Travel Agent
  • Credit card validator
  • Entity beans represent business objects
  • Ship
  • Cruise line
  • Cabin
  • Message-driven beans are consumers in a
    message-driven application

19
EJBs
  • Session beans
  • Perform specific tasks
  • Are not persistent
  • Stateful session beans use lightweight
    persistence, i.e., they are saved to secondary
    storage
  • Stateless session beans are pooled by the
    container

20
EJBs
  • Entity beans
  • Represent data in a database
  • Are shared among clients
  • Creates, stores, and refreshes all handled by the
    container
  • Represent business objects

21
EJBs
  • Entity beans
  • Container managed
  • Container generates all the required SQL
  • Developer writes abstract class with only
    abstract get and set methods
  • Container infers what attributes are required
  • Bean managed
  • Developer provides SQL calls as callback methods
  • In either case
  • The container calls load() and store()

22
EJB Components
Home Interface
EJB Container
EJB Home Object
lifecycle methods
Bean Class
Client
EJB Object
callback methods
Object Interface
objects createdby the container
business methods
23
EJB Components
  • All EJBs require
  • A Home interface
  • Contains lifecycle methods, like create() or
    find()
  • Accessible to client through JNDI
  • A Remote interface
  • Contains business logic
  • Acquired via the home interface
  • Client uses an RMI stub to access
  • A bean class
  • Contains callback methods
  • A deployment descriptor
  • An EJB Container in which to run

24
Stateless Session Bean
  • Home interface
  • Required create() method

package beans import java.rmi.RemoteException i
mport javax.ejb.EJBHome import
javax.ejb.CreateException public interface
SimpleSessionHome extends EJBHome // The
create method for the SimpleSession bean public
SimpleSession create() throws CreateException,
RemoteException
25
Stateless Session Bean
  • Remote interface
  • Business logic

package beans import java.rmi.RemoteException i
mport javax.ejb.EJBObject public interface
SimpleSession extends EJBObject // The public
business method on the SimpleSession bean public
String getEchoString(String clientString) throws
RemoteException
26
Stateless Session Bean
  • Bean class

package beans import javax.ejb.SessionBean impo
rt javax.ejb.SessionContext public class
SimpleSessionBean implements SessionBean
public String getEchoString(String
clientString) return clientString " - from
session bean" public void ejbActivate()
public void ejbPassivate() public void
ejbRemove() public void ejbCreate()
public void setSessionContext(SessionContext
context)
27
Stateless Session Bean
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD
Enterprise JavaBeans 2.0//EN' 'http//java.sun.com
/dtd/ejb-jar_2_0.dtd'gt ltejb-jargt
ltdisplay-namegtSimpleSessionlt/display-namegt
ltenterprise-beansgt ltsessiongt
ltdisplay-namegtSimpleSessionEjblt/display-namegt
ltejb-namegtSimpleSessionEjblt/ejb-namegt
lthomegtSimpleSessionHomelt/homegt
ltremotegtSimpleSessionlt/remotegt
ltejb-classgtSimpleSessionBeanlt/ejb-classgt
ltsession-typegtStatelesslt/session-typegt
lttransaction-typegtBeanlt/transaction-typegt
ltsecurity-identitygt ltdescriptiongtlt/descrip
tiongt ltuse-caller-identitygtlt/use-caller-id
entitygt lt/security-identitygt
lt/sessiongt lt/enterprise-beansgtlt/ejb-jargt
28
Session Beans
  • Components that provide a transactional,
    distributed, managed resource without persistence
  • Session beans may have state, but the values will
    not be persisted by the container
  • Session beans are frequently task processors or
    flow controllers
  • Session beans represent the ability to perform a
    task
  • Drive-up teller at a bank
  • Travel agent
  • Session beans use other, persistent objects to
    get the job done, but are not persistent
    themselves

29
The Role of Session Beans
  • Usually serves as a gateway, or process handler
  • Encapsulates actions with the business logic
  • Serves as transaction boundaries
  • Groups operations

30
Stateless Session Bean
  • Everything needs to be installed
  • jar file to contain classes
  • war file to contain JSPs, servlets,
  • Presentation logic
  • Often use servlet test clients
  • ear file to contain everything else
  • Add XML deployment descriptors
  • Usually contains app server specific info as well

31
EJBs
  • Enterprise JavaBeans require an act of faith on
    the part of the developer
  • Developer provides interfaces
  • Developer provides some callback methods
  • Developer trusts that the container will
    automatically create the actual bean classes and
    call everything at the right time

32
Stateless Session Beans
  • No state
  • Beans may have attributes
  • Values are not assumed to be preserved between
    calls
  • Beans are pooled

33
Stateless Session Bean
34
Stateless Session Bean
35
Stateless Session Beans
  • Most common and most simple type
  • Managed by the container as if they have no state
  • Class can declare variables, but container
    assumes that all stateless session beans are
    identical
  • Allows instance pooling
  • Any instance can be used to satisfy a request

36
Stateful Session Beans
  • Additional callback methods
  • ejbActivate()
  • Called by the container when restoring a session
    bean from temporary storage
  • ejbPassivate()
  • Called by the container when saving a session
    bean to temporary storage

37
Stateful Session Beans
38
Stateful Session Beans
39
Stateful Session Beans
  • State is assumed to be client specific, even
    though it isnt persisted
  • Maintains a conversation with the client
  • Each client has its own instance
  • Cannot be pooled
  • Application server can manage the lifecycle to
    help efficiency

40
Session Bean Lifecycle
  • Does Not Exist
  • Bean unavailable for service
  • The ejbCreate() method instantiates it, ties it
    to the container, and initializes it
  • Pooled
  • Available for service
  • For stateless session beans, all method calls are
    matched to available instances
  • Home interface has a create() method
  • Container may use available instance rather than
    instantiate a new one

41
Session Bean Lifecycle
  • For stateful session beans
  • Instances will be passivated and activated as
    necessary
  • Essentially switches clients state in and out of
    instances on demand
  • Instances are serialized to a disk location
  • Known a lightweight persistence
  • Object references are unique and can be saved by
    the client

42
SessionContext
  • Context classes are used by the beans to
    communicate with the container
  • SessionContext
  • EntityContext
  • MessageDrivenContext

43
SessionBean Interface
44
Session Beans
  • Developer must create a session bean that
    implements the javax.ejb.SessionBean interface
  • Defines most of the lifecycle methods
  • Provided methods are called by the container
  • Known as callback methods, can do additional work
    to the standard lifecycle management provided by
    the container
  • Note absence of a creation method
  • Stateless session beans are pooled and identical
  • Stateful session beans define their own create
    methods

45
Creating Session Beans
  • All lifecycle methods begin with the prefix ejb
  • The promoted methods will automatically remove it
  • Stateless session beans have a single create
    method
  • public void ejbCreate() throws javax.ejb.CreateExc
    eption
  • Promoting this to the home interface generates
    the appropriate create() method there
  • Stateful session beans may have multiple creation
    methods, using parameters to set attributes
  • public void createAtValue( String value ) throws
    CreateException

46
Proxy Pattern
47
The Proxy Design Pattern
  • The Proxy design pattern described in the
    so-called Gang of Four book
  • Gamma, Helm, Johnson, and Vlissides, Design
    Patterns Elements of Reusable, Object Oriented
    Software, 1995
  • EJB implementation of Proxy pattern
  • Two classes implement the same interface
  • Proxy class does networking to reach Subject
    class, along with marshalling arguments and
    unmarshalling return values
  • Subject class does actual behavior

48
The Proxy Design Pattern
49
The Proxy Design Pattern
  • Developers create interfaces
  • Container generates Proxy/Subject pairs
  • Subject has a reference to developers
    implementation class
  • Proxy instances retrieved via JNDI registry
  • Enables the container to intercept all EJB method
    calls and apply required services

50
Issues
  • jar and war files are platform and server
    independent, ear files are not
  • Assembling an ear file is usually done through a
    wizard
  • Creating the deployment descriptor is usually
    done through a wizard
  • Deployment descriptor settings affect application
    server
  • Declarative programming
  • Nothing needs to be recompiled
Write a Comment
User Comments (0)
About PowerShow.com