Enterprise Java Beans EJB - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Enterprise Java Beans EJB

Description:

Entity beans manage their ... Unlike entity beans, states are not stored in a permanent data ... Stateless session beans are pooled by their container to ... – PowerPoint PPT presentation

Number of Views:1579
Avg rating:3.0/5.0
Slides: 35
Provided by: eecgTo
Category:
Tags: ejb | beans | enterprise | java

less

Transcript and Presenter's Notes

Title: Enterprise Java Beans EJB


1
Enterprise Java Beans (EJB)
  • MIE456 Tutorial

2
Agenda
  • What is EJB
  • How does EJB work?
  • What are the benefits of using EJB

3
What is EJB?
  • A component architecture
  • A component model
  • For developing object-oriented distributed
    enterprise-level applications.

4
Why EJB?
  • Common tasks of Enterprise Applications
  • Concurrency
  • Persistence
  • Transactions
  • Synchronization
  • Security
  • Resource management
  • Connection pools, thread pools, replication, load
    balance
  • The EJB/application server technologies can
  • Take care of these common issues
  • Let developers focus on implementing the business
    logic

5
Architecture Overview
6
Architecture
7
Application Server and EJB Container
  • Application Server
  • Enterprise Java Server
  • EJB Container
  • Insulates the EJBs from direct access from client
    applications.
  • Every time a bean is requested, created, or
    deleted, the container manages the whole process.

8
EJB Container
9
EJB Specification
  • A contract between bean and container
  • Many EJB container implementations
  • BEA, IBM, GemStone, Sun, Sybase,
  • A bean is portable across different EJB containers

10
Enterprise Beans
  • Two interfaces plus actual bean class
  • Only the interfaces are visible to clients

11
Interface heirarchy
  • Note use of RMI

12
Interfaces of EJBs
  • Home interface
  • Defines the beans life cycle methods
  • E.g. create, remove, and finding
  • Use to get reference to Remote interface
  • Remote interface
  • Defines the API of the methods of the EJB
  • The business logic method API

13
Classes of EJBs
  • Bean class
  • Implements the beans business methods
  • Does NOT implement home or remote interface
  • However, it must have methods that match the
    signatures of the remote interface
  • Primary key class
  • For entity beans only,
  • more later

14
Deploying EJBs
  • After the home interface, remote interface, bean
    implementation, and primary key class (if needed)
    are prepared (generated by tools or coded
    manually), the EJBs must be added to the EJB
    container.
  • This process is called deployment.
  • During deployment, many files are generated
  • Home stub, object stub, EJB home, EJB object,

15
Deploying EJBs
EJB Server
EJB home stub
Client
EJB Container
Home interface
Home interface
EJB home
EJB object stub
EJB object
Remote interface
Remote interface
Bean class
16
Life Cycle of EJB Instance
17
Types of EJBs
  • Entity Beans
  • Container-managed persistence (CMP)
  • Bean-managed persistence (BMP)
  • Session Beans
  • Stateful session Beans
  • Stateless session Beans

18
Entity Beans
  • Represent permanent data
  • Provide methods to manipulate data
  • Usually, permanent data is stored in a data
    source, such as a relational or object database
  • Each bean is identified by a primary key

19
Mapping Schema to Entity Beans
20
Container Managed Persistence (CMP)
  • Delegate their persistence to their EJB container
  • Do not have to know which source is used to
    provide the persistent state of the bean.
  • You just have to specify which fields are
    persistent.
  • All the required JDBC code for accessing the
    database is generated for you.
  • Therefore, there is absolute portability and the
    EJB developer can focus on the business logic.

21
Bean-managed persistence (BMP)
  • Entity beans manage their own persistence
  • The EJB developer manages the persistent state of
    the bean by coding database calls
  • Usually, the developer uses JDBC for coding the
    persistence logic

22
Comparing CMP and BMP
  • A BMP entity bean is inappropriate for large
    applications.
  • CMP is more scalable
  • BMPs may provide better portability than CMPs,
    because less container-generated code is used.

23
Example Bean Class
public class CustomerBean implements EntityBean
int customerID Address
myAddress Name myName // CREATION
METHODS public Customer ejbCreate(Integer id)
public Customer ejbCreate(Integer id,
Name name) // BUSINESS
METHODS public Name getName() ...
public void setName(Name name) public
Address getAddress() public void
setAddress(Address address)
  • Business methods can be more than accessors and
    mutators
  • Container will call set/get or create/destroy
    object to ensure bean is synchronized with
    database records (for CMP)

24
Example Home Interface
public interface CustomerHome
extends javax.ejb.EJBHome public Customer
create(Integer customerNumber)
throws RemoteException,CreateException
public Customer create(Integer customerNumber,
Name name) throws RemoteException,Cre
ateException public Customer
findByPrimaryKey(Integer customerNumber)
throws RemoteException, FinderException
public Enumeration findByZipCode(int zipCode)
throws RemoteException, FinderException
  • Bean developer writes interface
  • Container will generate code
  • To mediate all calls

25
Example Remote Interface
public interface Customer extends
EJBObject public Name getName()
throws RemoteException public void
setName(Name name) throws
RemoteException public Address
getAddress() throws
RemoteException public void
setAddress(Address address)
throws RemoteException
  • Defines the externally visible business methods
  • Must match bean methods
  • No other bean methods visible
  • Container generates code
  • To mediate all calls

26
Example Deployment Descriptor

This bean represents a
customer
CustomerBean
CustomerHome Customeremote CustomerBean
Container Integer
False

27
Example Deployment Descriptor

everyone
everyonerole-name
CustomerBean


CustomerBeanname
Requiredttribute

28
EJB Transaction Attributes
  • Bean managed
  • Required
  • Use callers transaction if exists, else start
    new transaction
  • Supports
  • Use callers transaction if exists, else no
    transactions
  • Requires new
  • Create new transaction for duration of method
    call
  • Mandatory
  • Fail if caller hasnt started a transaction
  • Never
  • Fail if caller has started a transaction

29
Session Beans
  • Encapsulates typical business processes
  • Perform some tasks on behalf of the client
  • May contain a conversational state associated
    with a particular client
  • Unlike entity beans, states are not stored in a
    permanent data source and will not survive a
    server failure
  • Session beans implement business logic, business
    rules, and workflow.

30
Stateless Session Beans
  • Does not maintain any conversational state.
  • Easier for container to manage
  • Use less resources, process requests faster
  • Stateless session beans are pooled by their
    container to handle multiple requests from
    multiple clients.

31
Example Stateless Bean
// remote interface public interface
CreditService extends javax.ejb.EJBObject
public void verify(CreditCard
card, double amount) throws RemoteException,
CreditServiceException public
void charge(CreditCard card,
double amount) throws RemoteException,
CreditServiceException
  • All data to perform verify() are in the arguments
  • After one client is finished with a call, another
    client can use the same bean
  • Resource pooling
  • Can have (non-conversational) state
  • E.g. database connection

32
Stateful Session Beans
  • Maintains client-specific session information
    (called conversational state) across multiple
    method calls and transactions
  • Clients cannot share bean
  • Aware of client history
  • Each stateful session bean has a timeout value
  • The bean instance is destroyed and the remote
    reference is invalidated after the timeout period
    is elapsed.

33
Example Stateful Bean
  • Maintain state between calls
  • Note, state is not persistent (like entity beans)
  • Lost if server crashes
  • Container may passivate, activate bean for
    performance reasons

public class HotelClerkBean implements
SessionBean //conversational state
Customer cust Vector resVector new
Vector() public void ejbCreate(Customer
customer) public void
addReservation(Name name,
RoomInfo ri, Date from, Date to) public
void reserveRooms()
34
Benefits of using EJBs
  • Independence from database schema
  • Transaction management
  • Platform independence (portable)
  • Scalable environment
  • Secure access
  • Multi-tier architecture
  • Code generation ? easier development process
  • Code reuse (container)
Write a Comment
User Comments (0)
About PowerShow.com