01/06/151 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
EJB: Stateless Session Bean
Presenter: Nguyễn Xuân Vinh
Information Technology Faculty
Nong Lam University
01/06/152 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/2 of 31
Session Objectives
Identify the constituents of an Enterprise
JavaBean.
Define a bean class, EJB object, home interface,
home object and deployment descriptors.
Define a stateless session bean
Write programs related to stateless session beans
Compile and deploy stateless session beans
01/06/153 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/3 of 31
Review of Session 2-(1of 2)
In session 2 we discussed
Four stages are followed while developing business solutions
Six parties are involved while deploying Enterprise JavaBeans
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/5 of 31
Components of an Enterprise Bean
Components of an
enterprise bean
The bean class
The EJB object
The Remote
interface
Deployment
Descriptors
The Home
Interface
The EJB-jar file
The Home object
The Local Interface
The LocalHome
Interface
01/06/156 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/6 of 31
The Bean class
Well-defined interface
The Bean
Bound
Communicates to the
client through the
interface
Container
Works in any
Enterprise
Bean
Instance
2. Delegates
method
to bean
The container is the middleman between the
client and the bean. It manifests itself as a
single network-aware object.
This network-aware object is called the
EJB Object
01/06/158 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/8 of 31
The Remote Interface
Remote Interfaces
derived from javax.ejb.EJBObject
Business
Methods
Define
Perform
Functionality of
the bean
javax.ejb.EJBObject
public interface
java.rmi.RemoteInterface
extends javax.ejb.EJBObject
{
public abstract javax.ejb.EJBHome
Objects
EJB specifies certain
methods that the home
interface has to support.
These methods are defined
in the javax.ejb.EJBHome
public interface javax.ejb.EJBHome extends
java.rmi.Remote {
public abstract EJBMetaData getEJBMetaData()
throws java.rmi.RemoteException;
}
01/06/1511 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/11 of 31
The Methods in EJB Home
getEJBMetaData(): It is this method
that gets information about the
beans that are being worked on
remove(): This method destroys an
EJB object
The methods that fall under the
EJB Home are
01/06/1512 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/12 of 31
The Home Object
Client
Code
Home
A session bean may last as long as the client session.
Will not survive if the application server changes or crashes.
They are objects which are present in-memory which die along
with the surrounding environment and are not persisted in a
database.
01/06/1515 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/15 of 31
Conversational and Non Conversational Beans
A conversation stretches across a business process with
respect to the client.
A stateless session bean conducts a conversation that
spreads over a single method call.
Stateful session beans can retain their conversational
state.
Client
Conversation
Bean
01/06/1516 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/16 of 31
Writing a Session Bean
The six methods to be followed while
writing a session bean
setSessionContext(SessionContext ctx)
public int multiply(int a, int b){
return (a*b);
}
ejbPassivate()
ejbCreate()
ejbActivate()
ejbRemove()
. . . . . . .
}
Business methods are written to solve business logic problems.
Business methods are contained in the remote interface of
the bean for the client to access them.
01/06/1519 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/19 of 31
Using JNDI to lookup Home Objects
Home
Object
Enterprise
beans
EJB
Object
Client
JNDI
Naming
Service
1. Retrieve
Home
Bean
Bean
Bean
Bean
Invokes
01/06/1522 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/22 of 31
Deployment Descriptors
specifies how the container is supposed to create and manage the
enterprise bean object.
defines the name of the enterprise bean class, and the names of the
home and remote interfaces.
ejb-jar file provides naming information about enterprise bean,
remote interface and home interface.
ejb.jar has to be present in the directory called META-INF.
01/06/1523 /XX MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH
TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN
ACCP2005/EJB 2.0/Session 3/23 of 31
jboss.xml
Provides the container information about the
JNDI mapping, persistence information and
database mapping.
This file also has to be put into the META-INF
the newly created .jar file has to be copied into
the deploy directory on the server.
D:\bin\jboss\deploy
.jar file
.jar file
jar cvf welcome.jar Welcome.class Welcomehome.class
Welcomebean.class
META-INF