Title: Enterprise Java Beans EJB
1Enterprise Java Beans (EJB)
2Agenda
- What is EJB
- How does EJB work?
- What are the benefits of using EJB
3What is EJB?
- A component architecture
- A component model
- For developing object-oriented distributed
enterprise-level applications.
4Why 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
5Architecture Overview
6Architecture
7Application 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.
8EJB Container
9EJB Specification
- A contract between bean and container
- Many EJB container implementations
- BEA, IBM, GemStone, Sun, Sybase,
- A bean is portable across different EJB containers
10Enterprise Beans
- Two interfaces plus actual bean class
- Only the interfaces are visible to clients
11Interface heirarchy
12Interfaces 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
13Classes 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
14Deploying 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,
15Deploying 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
16Life Cycle of EJB Instance
17Types of EJBs
- Entity Beans
- Container-managed persistence (CMP)
- Bean-managed persistence (BMP)
- Session Beans
- Stateful session Beans
- Stateless session Beans
18Entity 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
19Mapping Schema to Entity Beans
20Container 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.
21Bean-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
22Comparing 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.
23Example 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)
24Example 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
25Example 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
26Example Deployment Descriptor
This bean represents a
customer
CustomerBean
CustomerHome Customeremote CustomerBean
Container Integer
False
27Example Deployment Descriptor
everyone
everyonerole-name
CustomerBean
CustomerBeanname
Requiredttribute
28EJB 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
29Session 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.
30Stateless 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.
31Example 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
32Stateful 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.
33Example 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()
34Benefits 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)