Title: Enterprise Java Beans (EJBs)
1Enterprise Java Beans (EJBs)
- Dave Elliman
- But with thanks to Martin Hansson at IconMedia
Lab, and Phillip Bride of GemStone Systems for
some of this lecture.
2The Concept
- Entity bean
- corresponds to a record in a database
- Session bean
- handles business flow (one per client unless
stateless, when may be shared)
3Enterprise JavaBeans, Defined
- From Sun
- The Enterprise JavaBeans architecture is a
component architecture for the development and
deployment of component-based distributed
business applications. Applications written using
the Enterprise JavaBeans architecture are
scalable, transactional, and multi-user secure.
These applications may be written once, and then
deployed on any server platform that supports the
Enterprise JavaBeans specification
4Enterprise JavaBeans(Shorter Definition)
- From OReillys Enterprise JavaBeans book
- An Enterprise Java Bean is a standard server-side
component model - Aha! Its a standard for building MIDDLEWARE In
multi-tiered solutions
5The Context in Which EJBs Are Used
6It All Works by RMI
7A Taxonomy of EJBs
8Differences Between Beans
- Entity Beans
- Represent persistent data
- Long-lived
- Session Beans
- Interact with clients
- Model business Logic
- Short-lived
9Session Bean (Stateful)
- Assigned to 1 client only
- Keeps info about a client
- ie has attributes for clients state
- Assigned to a client for lifetime
- Non-persistent
- Short-lived (client, timeout, server crash)
10Session Bean (Stateless)
- Many clients can access it
- Implements business logic
- One bean can service multiple clients (fast and
efficient) - Lifetime controlled by container
- No state across methods
- Each instance identical upon creation
- Short-lived
11Insight Into Session Beans
12Entity EJBs
- Bean managed persistence (BMP)
- developer writes JDBC code and SQL statements
- Container managed persistence (CMP)
- Container generates methods to read and write to
the database - Takes necessary information from the Deployment
Descriptor
13Entity EJBs
- are used as the object model
- contains all JDBC code
- should guarantee the integrity of the database
- live as long as the database (for ever)
- survives server crashes
- should be reused
14Resource Handling
- Passivation
- the process that stores the beans state to a
secondary memory, and swaps it out of primary
memory - Activation
- the opposite process of recreating the bean into
primary memory - This holds for every type of bean except the
- stateless session bean
15Automatic Database Handling
- Create an entity bean with CMP
- Tell server what database you use
- Specify what attributes should go into what
database fields - This can all be done in the Deployment
Descriptor - This completely cuts the code from the database
model and vendor, and frees the developer from
JDBC coding!
16EJB Architecture Diagram
17Additional Info
- Clients never interact directly with a bean
class, they use stubs (which connect to
skeletons, which connect to containers which
call the bean classwhew!) - Why? This allows the application server to
replicate bean instances (for performance
reasons), manage transactions, etc - A bean also interacts with its server via a
container interface the container calls the
beans life cycle methods, manages the beans
persistence, etc
18To Implement an Enterprise Bean
- Any enterprise bean must define two interfaces
and one or two classes - Remote interface
- defines a beans external interface
- must extend javax.ejb.EJBObject (which in turn
extends java.rmi.Remote) - Home interface
- The home interface defines a beans life cycle
methods, eg. create bean, remove bean, find bean,
etc - must extend javax.ejb.EJBHome which also extends
java.rmi.Remote
19To Implement, Continued
- Bean Class
- The java class that actually implements the
beans external interface, e.g. the bean class
provides implementations for the beans business
methods - An entity bean must implement the
javax.ejb.EntityBean interface, while a session
bean must implement the (you guessed it)
javax.ejb.SessionBean. Both of these interfaces
extend javax.ejb.EnterpriseBean
20To Implement, Continued
- Primary Key
- The primary key is a very simple class that
provides a pointer into a database Only entity
beans need a primary key. This class must
implement java.io.Serializable (so the entity
bean can automatically be sent to persistent
storage)
21Creating an EJB1
- Write your Home interfacecreate(...) and
findByXXX(...) - Write your Remote interfaceimplement your
business methods - Develop your entity or session bean
- If it is a entity bean define a PrimaryKey class
- Write your Deployment Descriptor
22Creating an EJB 2
- Compile your files and generate container code
with the server tools - Set up a data source to your database
- Install the bean in the server
- Develop the client application
- Start your server and run the client!
23Home Interface - EmployeeHome.Java
import javax.ejb. import java.rmi.RemoteExceptio
n import java.util. public interface
EmployeeHome extends EJBHome public Employee
create(Hashtable personInfo) throws
CreateException, RemoteException public
Employee findByPrimaryKey(EmployeePK primaryKey)
throws FinderException, RemoteException
public Enumeration findEmployeesByLastName(String
lastName) throws FinderException,
RemoteException
24Remote Interface - Employee.Java
import java.rmi.RemoteException import
javax.ejb. import java.util.Hashtable public
interface Employee extends EJBObject public
int update(java.util.Hashtable personInfo)
throws RemoteException public String name()
throws RemoteException
25Entity Bean - EmployeeBean.Java
public class EmployeeBean implements EntityBean
public int
EmployeeId // also the primary Key public
String firstName public
String lastName public
String title public String
SSN public void ejbCreate(Hashtable
personInfo) long now System.currentTimeMil
lis() this.EmployeeId (int) now //unique
id this.fornamn (String) personuppgifter.get
(firstName") this.efternamn (String)
personuppgifter.get(lastName") this.title
(String) personuppgifter.get("title")
public String name() return firstName " "
lastName ...
26We Have to Implement the EntityBean Interface
public void ejbCreate() public void
setSessionContext(SessionContext ctx) public
void ejbRemove() public void ejbActivate()
public void ejbPassivate() public
void ejbLoad() public void ejbStore()
27Primary Key Interface-EmployeePK.Java
- Used to uniquely identify an entity bean
public class EmployeePK implements
java.io.Serializable public int EmployeeId
28Ill Try to Explain This Again!
From http//www.ncworldmag.com/ncw-03-1998/ncw-03
-ejbprog.html
29This All Seems Very Complicated!
- Yes there is a lot to remember
- It is always nearly the same though
- Make a template? Cut and paste
- Use a naming convention
- If you have an EJB called XXXBean
- Home interface XXXHome
- Remote interface XXX
- For an Entity bean Primary key XXXPK
30Deploying Your Entity Java Beans
- You need to know how to deploy the EJB to a
server - You need to know how to find and use it from a
client
31To Deploy an EJB You Need
- A Deployment descriptor - An XML file that
specifies information about the bean such as its
persistence type and transaction attributes. - deploytool creates the deployment descriptor when
you step through the New Enterprise Bean Wizard.
32deploytool
33Client Code to Access an EJB
import javax.naming.Context import
javax.naming.InitialContext import
javax.rmi.PortableRemoteObject import Employee
import EmployeeHome public class
EmployeeClient public static void
main(String args) try
Context initial new InitialContext()
Object objref initial.lookup ("javacomp/env/ejb
/Employee") EmployeeHome home
(EmployeeHome)PortableRemoteObject.narrow(objref,
EmployeeHome.class) Employee
currencyEmployee home.create()
catch (Exception ex)
System.err.println("Caught an unexpected
exception!") ex.printStackTrace()
34Compile, Build Ear, Deploy, Run
- Compile files
- Create a new client application in the
application ear - Deploy the entire application using the
deploytool - Run the application client using
- runclient -client EmployeeApp.ear -name
EmployeeClient
35Finding Out More..
Goto http//java.sun.com/j2ee/tutorial Instant
Enterprise Java Beans by Paul Tremblett, McGraw
Hill See it all in action onhttp//java.sun.com/j
2ee/tutorial/1_3-fcs/doc/Ebank.html