Enterprise Java Beans - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Enterprise Java Beans

Description:

Java. Key Point ... Java. Resource Management ... throws java.rmi.RemoteException, TransferException; ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 63
Provided by: daniel85
Category:
Tags: beans | enterprise | java

less

Transcript and Presenter's Notes

Title: Enterprise Java Beans


1
Enterprise Java Beans
  • Session Beans and Resource Management

2
Topics
  • Container and Enterprise Bean review
  • Resource Management
  • Session Beans
  • Stateless
  • Stateful
  • Resources
  • References

3
Enterprise Bean
  • Component that can be deployed in any
    EJB-compliant application server
  • Remote interface for its operations
  • extends EJBObject
  • Home Interface for create,find, and remove
  • Bean Implementation
  • implement SessionBean or EntityBean
  • Required container lifecycle callbacks
  • implement business methods

4
Bean Types
  • Session Beans
  • Represents business logic operations
  • Teller and transfer()
  • Stateless and Stateful
  • Exist for lifetime of client session
  • Entity Beans
  • Represents persistent data in a database
  • Exist indefinitely
  • Bean-managed and Container-managed persistence

5
Containers and Beans
  • Provides implementation of Home and Remote
    interface
  • Container manages bean lifecycle
  • Creation, Pooling, Persistence
  • Registers bean for remote access
  • Intercepts all client calls to bean
  • Manages Security, Transaction Context, Threading

6
Server and Containers
Relational Database
Server
TopLink Container
CICS Container
CICS
7
Container and Beans
Home Interface
Container
Pool
Home
Home Stub
create
Client
Remote Object Stub
EJBObject
Teller Bean
transfer
Remote Interface
8
Key Point
  • All method invocations are intercepted by the
    container/EJBObject and then delegated to your
    bean implementation
  • Why ?
  • Manage Concurrency (threading)
  • Manage Security
  • Manage Transactions
  • Manage Resources
  • Remote Access

9
Resource Management
  • As the number of clients increases, the number of
    server objects increases
  • large systems need to manage resources
  • EJB suggests two mechanisms
  • Instance Pooling
  • Activation

10
Managing Beans
  • Bean characteristics require different management
    strategies
  • Responsibility of container
  • will call back bean at prescribed points in the
    lifecycle

11
Bean Characteristics
12
Stateless Session Beans
13
Stateless Session Beans
  • A stateless session bean holds no state on behalf
    of a client
  • Each bean instance is equivalent
  • Sufficient client information must be passed with
    each method invocation

14
Example
  • interface Teller extends javax.ejb.EJBObject
  • public void transfer( int from, int to, float
    amount )
  • throws java.rmi.RemoteException,
    TransferException
  • All required parameters are in method invocation
  • Any teller object is as good as any other

15
EJBObject
16
Example (Cont)
  • interface TellerHome extends javax.ejb.EJBHome
  • public Teller create() throws RemoteException,
    CreateException
  • Serves as factory for creating tellers
  • Stateless session beans must have a create()
    method that takes no arguments
  • Bean implementation needs ejbCreate()

17
EJB Home
18
Bean Implementation
  • public class TellerBean extends
    javax.ejb.SessionBean
  • public void ejbCreate()
  • public void ejbRemove()
  • public void ejbActivate()
  • public void ejbPassivate()
  • public void setSessionContext(SessionContext
    ctx)
  • public void transfer( int from, int to, float
    amount )
  • throws RemoteException, TransferException
  • // Perform transfer. JDBC, etc.

19
(No Transcript)
20
Deployment Descriptor(ejb-jar.xml)
  • Inc.//DTD Enterprise JavaBeans 1.1//EN'
    'http//java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'
  • This component is used manage the bank..
  • Bank Component
  • ...

21
Deployment Descriptor
  • Used to handle actions that span accounts.
  • Teller Bean
  • Teller
  • TellerHome
  • Teller
  • TellerBean
  • Stateless
  • Container

22
Deployment Descriptor
  • Used to manage specific
    accounts
  • ejb/AccountHome
  • Entity
  • AccountHome
  • Account
  • Account

23
Weblogic Deployment Descriptor(weblogic-ejb-jar.x
ml)
  • Teller
  • ejb/AccountHome-name
  • AccountHome
  • TellerHome

24
Client Interaction
  • Looks up home interface in JNDI
  • Invoke create() on Home stub
  • Invoke transfer() on EJBObject
  • Invoke remove() when done

25
Pooling
  • Stateless session beans can be very efficiently
    pooled to save resources
  • Pooling technique is vendor-specific. Pool can be
    pre-created as well as dynamically sized
  • Adding beans to the pool
  • Class.newInstance() to create TellerBean
  • setSessionContext()
  • ejbCreate

26
Why Pool ?
  • Efficient memory utilization
  • Larger systems can not afford to have a bean in
    memory dedicated to each client
  • Significant delay between client calls is
    possible
  • Just-In-Time paradigm. Bean is pulled
    dynamically from pool to handle method invocation
    and then returned to pool
  • Pool size can be adjusted dynamically to deal
    with changing client loads

27
(No Transcript)
28
Method Invocation
  • Bean is selected from pool and associated with
    clients EJBObject
  • EJBObject/Container manages transactions,
    security, threading, etc.
  • Method is then delegated to bean implementation

29
(No Transcript)
30
Stateless Session Behavior
  • Lifecycle of the EJBObject and the bean
    implementation are separate.
  • EJBHomecreate() creates an EJBObject for the
    client - not an instance of your bean
    implementation
  • EJBHomeremove() removes the clients EJBObject
    - not an instance of your bean implementation

31
(No Transcript)
32
(No Transcript)
33
Stateless Session Bean Summary
  • Models a business process that consists of one
    operation
  • transfer(), logError(), Math.sin()
  • Has no state between method invocations
  • Can be pooled efficiently
  • Usually the most efficient bean type

34
Stateful Session Beans
35
Stateful Session Beans
  • Represents a conversation between client and
    server
  • shopping cart
  • Exists for the clients lifetime (or timeout
    period)
  • Each client method invocation accesses the same
    bean implementation

36
Home Interface
  • public interface CartHome extends EJBHome
  • public Cart create()
  • throws RemoteException, CreateException
  • public Cart create(String product)
  • throws RemoteException, CreateException

37
Bean Implementation
  • Implements all ejb Create methods
  • public class CartBean implements SessionBean
  • // lifecycle methods omitted
  • public void ejbCreate() throws
    CreateException
  • public void ejbCreate(String product) throws
    CreateException
  • addProduct( product)
  • public void addProduct( String product )
    // put product in Vector
  • public Vector products_ new Vector()

38
Resource Management
  • Can not be pooled the same way as stateless
    session beans
  • bean instance is for a specific client
  • Activation and Passivation are used
  • Beans are passivated (swapped to disk) when
    resources run low in server
  • object serialization typically used

39
Deployment Descriptor (ejb-jar.xml)
  • Holds items while shopping
  • Shopping Cart Bean
  • ShoppingCart
  • CartHome
  • Cart
  • CartBean
  • Stateful
  • Container

40
Weblogic Deployment Descriptor(weblogic-ejb-jar.x
ml)
  • ShoppingCart
  • /tmp
  • CartHome

41
Bean Attributes
  • Must be Serializable to permit activation and
    passivation
  • Bean itself is already serializable
    (EnterpriseBean)
  • Object Serialization semantics are used
  • Exception transient fields are not guaranteed to
    be initialized

42
(No Transcript)
43
(No Transcript)
44
Lifecycle Behavior
  • Client create() on home interface results in
    creation of bean
  • contrast with stateless session bean
  • Client remove() of EJBObject results in removal
    of bean
  • contrast with stateless session bean
  • Creation/Deletion controlled by client
  • Activation/Passivation controlled by container

45
Actions Required
  • ejbPassivate()
  • Release resources held
  • Prepare fields for Serialization
  • ejbActivate()
  • Acquire client-specific resources
  • Prepare object for access
  • may set transient, calculated fields

46
Consequences of Statefulness
  • Session Beans do not (typically) survive failures
  • Loss of a long conversation could have a large
    impact
  • short conversations
  • checkpoint state to database
  • advanced container capability

47
Stateful Session Bean Summary
  • Models a complete conversation with a client
    where state needs to be preserved between method
    invocations
  • Be prepared to lose state !
  • Session beans do not survive failure
  • code clients to re-establish state upon failure
    (if possible)
  • some products checkpoint session state (BEA)

48
Bean Characteristics
49
(No Transcript)
50
(No Transcript)
51
Misc. Topics
  • Obtaining Container Services
  • Concurrency
  • Exceptions
  • The Business Interface
  • Client APIs

52
Obtaining Container Services
  • Context object passed to bean implementation
  • setSessionContext()
  • setEntityContext()
  • Allows bean to participate in
  • security
  • transactions
  • Obtain this reference

53
(No Transcript)
54
Context Operations (Cont)
  • Save off reference to context for later use
  • public class TellerBean implements SessionBean
  • ..
  • public void setSessionContext( SessionContext
    ctx )
  • ctx_ ctx
  • SessionContext ctx

55
Threading
  • Concurrency
  • Access to a bean instance is serialized
  • Bean methods may not be synchronized
  • Reentrance
  • bean instances are non-reentrant by default
  • session beans can never be reentrant
  • entity beans can be configured to be reentrant
    (discouraged)
  • allows multithreaded access to bean !

56
Exceptions
  • Client sees exceptions as specified in Home and
    Remote Interfaces
  • All methods can throw Remote Exception
  • Server Exception categories
  • system
  • application
  • unchecked

57
Exception Types
  • System (Remote)
  • EJB container/server can potentially handle
    system exceptions
  • Ex. database failure
  • Application
  • Always passed to client
  • Unchecked Bean Exceptions
  • Must be caught by container

58
Business Interface
  • Our bean must have the same methods that we
    specify in the Remote/EJBObject interface.
  • Dont want to implement the Remote interface
  • its Remote !
  • Has additional EJBObject methods
  • could accidentally pass this when we wanted to
    pass a reference to an EJBObject
  • Develop an intermediate Business Interface

59
The Business Interface
60
Client Programming
  • Typically invokes home create() method and then
    invokes business methods. Invokes remove() when
    done.
  • isIdentical()
  • getHandle()
  • portable reference to an EJBObject
  • similar to stringified CORBA object reference
  • HandlegetEJBObject() to re-create object

61
Resources
  • Main EJB Page
  • http//java.sun.com/products/ejb/

62
References
  • Developing Java Enterprise Applications. Asbury,
    Weiner. Wiley. 1999
  • Java Enterprise in a Nutshell. Flanagan et al.
    OReilly 1999
  • Mastering Enterprise JavaBeans and the Java 2
    Platform, Enterprise Edition. Roman. Wiley. 1999
  • Java Report. Whats new in EJB 1.1?,
    Monson-Haefel. August 1999.
Write a Comment
User Comments (0)
About PowerShow.com