tutorial ejb
TRANSCRIPT
-
8/2/2019 Tutorial EJB
1/34
Tutorial on Enterprise JavaBeans(EJB)
Yi Liu
Department of Computer and Information Science
University of Mississippi
-
8/2/2019 Tutorial EJB
2/34
Contents
Part I Introduction to EJB component model
Part II Three Enterprise beans typesEntity Beans
Session Beans
Message-driven Beans
Part III J2EE deploytool demonstration
-
8/2/2019 Tutorial EJB
3/34
Part I
Introduction to EJB ComponentModel
-
8/2/2019 Tutorial EJB
4/34
What is Enterprise JavaBeans ?
Component model for building server-
side, enterprise-class applications.
-
8/2/2019 Tutorial EJB
5/34
EJB Model
EJB Client
EJB Client
EJB Client
. . .Enterprise Bean
Enterprise Bean
EJB Container
EJB Server
-
8/2/2019 Tutorial EJB
6/34
EJB Container
Exists on the server-side
Forms core of EJB component model
implementation
Provides EJB objects a runtime environment
remote access to the bean
security, persistence
transactions, concurrency
access to and pooling of resources
-
8/2/2019 Tutorial EJB
7/34
Enterprise Beans
Encapsulate business logic in reusable,
serverside components
Are deployed and executed within EJB
container , never directly accessed from
outside container.
Are accessed and executed by clients viaEJB container
-
8/2/2019 Tutorial EJB
8/34
Part II
Three Enterprise Beans Types
-
8/2/2019 Tutorial EJB
9/34
Three Types of Enterprise Beans
Session Beans
Entity Beans
Message-driven Beans
-
8/2/2019 Tutorial EJB
10/34
Session Beans
In-memory objects that are non-persistent
Designed to perform the processes of a
business
Not survive container crashes
Two types:
Stateless
Stateful
-
8/2/2019 Tutorial EJB
11/34
Stateful Session Bean
Holds conversational state
Stores information for a relatively short
amount of time
Capable of multiple interactions with the
client
Example: on-line shopping cart
-
8/2/2019 Tutorial EJB
12/34
Stateless Session Bean
Doesnt maintain conversational state
Immediately processes the information
received from the client
Example: check out
-
8/2/2019 Tutorial EJB
13/34
Transaction Management
Container-managed transaction demarcation
Beanmanaged transaction demarcation
-
8/2/2019 Tutorial EJB
14/34
Session Bean Elements
Remote Interface
Declares publicly available methods of EJB
Home Interface
Declare create method
Bean implementation class
Implement EJB business methods declared in remoteinterface
implementations for methods declared in homeinterface.
Deployment descriptor
Helper classes (optional)
-
8/2/2019 Tutorial EJB
15/34
Remote Interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface CourseRegistration extends EJBObject {//Students add or drop courses
public boolean studentAddSection(int pinNo, int sectionID,boolean isAudit)
throws RemoteException;
public boolean studentDropSection(int pinNo, intsectionID)throws RemoteException;
}
-
8/2/2019 Tutorial EJB
16/34
Home Interface
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;import java.lang.*;
public interface CourseRegistrationHome extends EJBHome {
CourseRegistration create() throws RemoteException,CreateException;
}
-
8/2/2019 Tutorial EJB
17/34
Bean implementation class(I)import java.rmi.RemoteException;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class CourseRegistrationBean implements SessionBean{
private Connection con;
private String dbName = "java:comp/env/jdbc/AcxiomProDB";
private SessionContext context;
private Person person;
private TermHome tHome;
private Course course;
-
8/2/2019 Tutorial EJB
18/34
Bean implementation class(II)
public boolean studentAddSection(int pinNo, intsectionID, boolean isAudit)throws RemoteException{
return person.addCourse(pinNo, sectionID, isAudit);
}
public boolean studentDropSection(int pinNo, int
sectionID)throws RemoteException{return person.deleteCourseRegistered(pinNo,sectionID);
}
-
8/2/2019 Tutorial EJB
19/34
Bean implementation class(III)public void ejbCreate()throws RemoteException{
try{
Context initial = new InitialContext();
Object objref = initial.lookup("java:comp/env/ejb/SimplePerson");
PersonHome pHome = (PersonHome)PortableRemoteObject.narrow(objref,
PersonHome.class );
person = pHome.create();objref = initial.lookup( "java:comp/env/ejb/SimpleCourse");
CourseHome cHome = (CourseHome)PortableRemoteObject.narrow(objref,
CourseHome.class );
course = cHome.create();
objref = initial.lookup( "java:comp/env/ejb/SimpleTerm");
tHome = (TermHome)PortableRemoteObject.narrow(objref, TermHome.class );
}
catch(Exception ex){
throw new EJBException("ejbCreate: " + ex.getMessage());
}
}
-
8/2/2019 Tutorial EJB
20/34
Bean implementation class(IV)
public CourseRegistrationBean() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
-
8/2/2019 Tutorial EJB
21/34
Entity Beans
Represent business objects and an in-memory view
of persistent data
Are used to represent data stored in a database Allow shared access from multiple EJB clients
Survive server crashes
Two types of persistence Container-managed
Bean-managed
-
8/2/2019 Tutorial EJB
22/34
Entity Bean Elements Remote Interface
Declares publicly available methods of EJB
Home Interface
Declare
create method
finder method Bean implementation class
Implement EJB business methods declared in remoteinterface
implementations for create and finder methods declaredin home interface.
Deployment descriptor
Helper classes (optional)
Primary key classes(optional)
-
8/2/2019 Tutorial EJB
23/34
Remote Interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Term extends EJBObject {
public Integer getTermID()throws RemoteException;
public String getTermName()throws RemoteException;
public Integer getTermYear()throws RemoteException;
}
-
8/2/2019 Tutorial EJB
24/34
Home Interface
import java.rmi.RemoteException;
import javax.ejb.*;
public interface TermHome extends EJBHome {public Term create(String termName, Integer termYear) throws
RemoteException, CreateException;
public Term findByPrimaryKey(Integer termID) throws
FinderException, RemoteException;public Term findByNameYear(String termName, Integer
termYear) throws FinderException, RemoteException;
}
-
8/2/2019 Tutorial EJB
25/34
Bean Implementation Class (I)import java.sql.*;
import javax.sql.*;
import javax.ejb.*;
import javax.naming.*;
public class TermBean implements EntityBean
{
private Integer termID;
private String termName;
private Integer termYear;private EntityContext context;
private Connection con;
private String dbName = "java:comp/env/jdbc/TermDB";
-
8/2/2019 Tutorial EJB
26/34
Bean Implementation Class (II)
public Integer getTermYear()
{
return termYear;
}
public String getTermName()
{
return termName;
}
public Integer getTermID()
{
return termID;
}
-
8/2/2019 Tutorial EJB
27/34
Bean Implementation Class (III)
public Integer ejbCreate(String tName, Integer tYear)throws
CreateException {
Integer id = null;
tName = tName.toUpperCase();
try {
}
catch (Exception ex) {
throw new EJBException("ejbCreate: " + ex.getMessage());
}
}
-
8/2/2019 Tutorial EJB
28/34
Bean Implementation Class (IV)
public void ejbPostCreate(String tName, Integer
tYear)
{ }
-
8/2/2019 Tutorial EJB
29/34
Bean Implementation Class (V)
public void ejbRemove() {
try {
deleteRow(termID);
} catch (Exception ex) {
throw new EJBException("ejbRemove: " +
ex.getMessage());
}
}
-
8/2/2019 Tutorial EJB
30/34
Bean Implementation Class (VI)public void ejbLoad() {
try {loadRow();
} catch (Exception ex) {
throw new EJBException("ejbLoad: " + ex.getMessage());
}
}
public void ejbStore() {
try {
storeRow();
}
catch (Exception ex) {
throw new EJBException("ejbStore: " + ex.getMessage());
}
}
-
8/2/2019 Tutorial EJB
31/34
Bean Implementation Class (VII)
public Integer ejbFindByPrimaryKey(Integer primaryKey) throws Exception
{
boolean result;
try
{
result = selectByPrimaryKey(primaryKey);
}
catch (Exception ex){
throw new Exception("ejbFindByPrimaryKey: " + ex.getMessage());
}
}
-
8/2/2019 Tutorial EJB
32/34
Message-driven Beans
Act as asynchronous message consumers
Use Java Message Service (JMS)
Execute business logic of JMS message
routed by container
Handle only one client at time
-
8/2/2019 Tutorial EJB
33/34
Types of Enterprise JavaBeans
Enterprise JavaBeans(EJB)
Session Bean(SB) Entity Bean(EB) Message Driven Bean(MDB)
Stateless(SLSB)
Stateful(SFSB)
Bean ManagedPersistence
Container ManagedPersistence
Synchronous Communication Asynchronous Communication
-
8/2/2019 Tutorial EJB
34/34
Part III
J2EE Deploytool Demonstration