EJB – an Introduction
Agenda
Background
JavaBeans
What is EJB?
Architecture
Roles
Steps involved
Questions?
Monolithic systems
Background
No reuse
Fat clients,
data model reused
Client/server
Thin clients,
both data model and N-Tier
business logic reused
3-tier architecture
Presentation Clients, GUI
Business logic “middleware”
Data DBMSs
The J2EE
Consists of 12 APIs:
Naming service
provides a common way of naming objects and
directories
Java Naming and Directory Interface (JNDI)
Transaction handling
Java Transaction Services (JTS)
Presentation (JSP, Servlet)
Communication (RMI)
Distribution, component model (EJB)
What is Enterprise Java
Beans?
defines a standard to build distributed
systems on the server side
frees developers from “plumbing” (s.a.
transactions , security, pooling, etc.)
is a framework to create enterprise
middleware
Why EJB
Independence of middleware (both
regarding hard- and software)
Automatic transaction handling
Automatic distribution
Portability
Scalability
Tightly coupled to CORBA
JavaBeans vs. EJB
Can be visible or Are always invisible
invisible Are distributed executable
components on a server
Designed to run locally Are described with a
Are described with DeploymentDescriptor
Properties, BeanInfo Can not be used as a ActiveX
classes and control
Customizers
May be used as an
ActiveX control
EJB
client architecture
EJB server
EJB container
EJB Home
EJBean
EJB Object
security
JNDI
JTS
…..
Different types of EJBs, 1
Entity bean
corresponds to a record in a
database
Session bean
handles business flow
(one per client)
Different types of EJBs, 2
EJB
Entity Session
Bean managed Container managed stateless stateful
Entity 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
Resource 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
Session EJBs
are mainly used for business logic
should constitute the clients only interface to the
system
controls processes that spans several entity
beans
lives as long as the client is alive
will not survive a server crash
are difficult to reuse
Entity EJBs
are used as the object model
contains all JDBC code
should guarantee the integrity of the
database
lever as long as the database (“for ever”)
survives server crashes
should be reused
Automatic database
handling
Create an entity bean with CMP
Tell server what database you use
Specify what attributes should go into what database
fields
-> all being made in the Deployment Descriptor
This completely cuts the code from the database
model and vendor, and frees the developer from
JDBC coding!
Roles in EJB Often the same vendor
Constitutes with the
platform
EJB server provider
Developes the bean
EJB container provider
Installs the bean in the server
EJB developer Develops the client software that
calls and uses the bean
EJB deployer
Client software developer
Steps in creating an EJB, 1
Write your Home interface
[create(...) and findByXXX(...)]
Write your Remote interface
[implement your business methods]
Develop your entity or session bean
If it is a entity bean define a PrimaryKey
class
Write your Deployment Descriptor
Steps in creating 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!
Home interface - EmployeeHome.java
Example
package examples.ejb.personnel;
import javax.ejb.*;
import java.rmi.RemoteException;
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;
}
Example
Remote interface - Employee.java
package examples.ejb.personnel;
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;
}
Example
Entity 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.currentTimeMillis();
this.EmployeeId = (int) now; //unique id
this.fornamn = (String) personuppgifter.get(”firstName");
this.efternamn = (String) personuppgifter.get(”lastName");
this.titel = (String) personuppgifter.get("title");
}
public String name() {
return firstName + " " + lastName;
}
...
Example
Primary key interface - EmployeePK.java
• used to uniquely identify an entity bean
package examples.ejb.personnel;
public class EmployeePK implements java.io.Serializable
{
public int EmployeeId;
}
Example – Tools, 1
Example – Tools,2