Tải bản đầy đủ (.ppt) (30 trang)

Bài giảng lập trình mạng stateless session bean GV nguyễn xuân vinh

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.22 MB, 30 trang )

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



Logical three-tier architecture of EJB:

The Client

The EJB Server

The Database

The EJB Container resides inside the EJB server. The container acts as a buffer
between the bean and the outside world.

The responsibility of the EJB Server and Container
01/06/154 /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/4 of 31
Review of Session 2-(2 of 2)

The server and the container provide following services to the
developer
* Transaction support
* Security support
* Persistence support
* Support for management of multiple instances .

An Enterprise Java Bean can be classified into:
* Session Beans
* Entity Beans
* Message-Driven Beans
01/06/155 /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/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

container with the
help of these interfaces
public interface javax. ejb. EnterpriseBean extends
java.io.Serializable {
}
Once the above interface is
implemented, the bean class is
confirmed

01/06/157 /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/7 of 31
The EJB Object






Client
Code
EJB Container/Server
1 .Calls a method
4.Returns method
to client
Home
Object
EJB
Object
3. Returns the Method

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

getEJBHome() throws
java.rmi.RemoteException;
}
01/06/159 /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/9 of 31
Relationship between Java RMI and EJB
Objects
Bean
Java Virtual Machine
Bean
Java Virtual Machine
Location Transparency
Remote Method Invocation
Portability of Client Code
EJB Object
java.rmi.Remote Remote
Object
JVM
JVM
01/06/1510 /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/10 of 31
The Home Interface
Home
Interface
Find EJB
Objects Create EJB
Objects
Destroy EJB

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

Object
EJB
Object
Enterprise
Beans
EJB Container/Server
1. Requests new EJB
Object
3. Returns the objects
2. Creates new object
01/06/1513 /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/13 of 31
Deployment Descriptors
Deployment
Descriptors
Life cycle requirements
and Bean management
Persistence Requirements
Transaction Requirements
Security Management
Classes that form the bean
Home Interface
Remote Interface
EJB Server
01/06/1514 /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/14 of 31
Life Cycle of a Session Bean


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)

ejbCreate()
ejbPassivate()
Business methods
ejbRemove()
ejbActivate()
01/06/1517 /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/17 of 31
The setSessionContext (SessionContext ctx)
Container
setSessionContext()
Bean
public class theBean implements SessionBean
{
private SessionContext ctx;
public void setSessionContext(SessionContext ctx)
{
this.ctx=ctx;
}
. . . . . . .
}
Session Context
(Gateway)
Associates
01/06/1518 /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/18 of 31
Business Methods
import javax.ejb.*;
public class sess implements Sessionbean{

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

Object
3. Creates an EJB Object
5. Return Object reference
6. Invokes business methods
EJB Container/Server
4. Create EJB Object
2. Returns reference
to the home object
7. Delegates request to bean
01/06/1520 /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/20 of 31
Steps in accessing Home Objects
The steps followed by the client code
to get a reference
The setting up of the Environment
Destroying the EJB Object
Calling a Method
Creating an Object
Retrieving the home objects
The Initial Context
01/06/1521 /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/21 of 31
Pooling of Stateless Session Bean
Client
EJB
Object
Invokes
EJB Server/Container

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

file.
<jboss>
<enterprise-beans>
<session>
<ejb-name>Welcome</ejb-name>
<jndi-name>Welcome</jndi-name>
</session>
</enterprise-beans>
</jboss>
01/06/1524 /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/24 of 31
Creating the jar file
Welcome.class
Deployment
Descriptors
Welcome.jar
Welcomebean.classWelcomehome.class
A jar file is created to package the three java files, namely the
bean class, the home interface and the remote interface. The
XML files Namely ejb-jar.xml

and

jboss.xml are also present in the
jar file.
01/06/1525 /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/25 of 31
Deploying the bean


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

×