View
15
Download
0
Category
Tags:
Preview:
DESCRIPTION
Citation preview
ACCP2005/EJB 2.0/Session 3/1 of 31
Stateless Session Beans
Session 3
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
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
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
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
DeploymentDescriptors
The Home Interface
The EJB-jar file
The Home object
The Local InterfaceThe LocalHome
Interface
ACCP2005/EJB 2.0/Session 3/6 of 31
The Bean class
Well-defined interfaceThe 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
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
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.RemoteInterfaceextends javax.ejb.EJBObject{
public abstract javax.ejb.EJBHome getEJBHome() throws java.rmi.RemoteException;}
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
ACCP2005/EJB 2.0/Session 3/10 of 31
Methods under javax.ejb.EJBObject
javax.ejb.EJBObject
getEJBHome()
getPrimaryKey()
remove()
getHandle()
isIdentical()
ACCP2005/EJB 2.0/Session 3/11 of 31
The Home InterfaceHome
Interface
Find EJB Objects Create EJB
Objects
Destroy EJBObjects
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;
}
ACCP2005/EJB 2.0/Session 3/12 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
ACCP2005/EJB 2.0/Session 3/13 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
ACCP2005/EJB 2.0/Session 3/14 of 31
Deployment Descriptors
Deployment Descriptors
Life cycle requirements and Bean management
Persistence RequirementsTransaction Requirements
Security Management
Classes that form the bean
Home Interface
Remote Interface
EJB Server
ACCP2005/EJB 2.0/Session 3/15 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.
ACCP2005/EJB 2.0/Session 3/16 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
ACCP2005/EJB 2.0/Session 3/17 of 31
Writing a Session BeanThe six methods to be followed while
writing a session bean
setSessionContext(SessionContext ctx)
ejbCreate()
ejbPassivate()
Business methods
ejbRemove()
ejbActivate()
ACCP2005/EJB 2.0/Session 3/18 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
ACCP2005/EJB 2.0/Session 3/19 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.
ACCP2005/EJB 2.0/Session 3/20 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
ACCP2005/EJB 2.0/Session 3/21 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
ACCP2005/EJB 2.0/Session 3/22 of 31
Pooling of Stateless Session Bean
Client
EJB Object
Invokes
EJB Server/Container
Bean
Bean
Bean
Bean Invokes
ACCP2005/EJB 2.0/Session 3/23 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.
ACCP2005/EJB 2.0/Session 3/24 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>
ACCP2005/EJB 2.0/Session 3/25 of 31
Creating the jar file
Welcome.classDeployment 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.
ACCP2005/EJB 2.0/Session 3/26 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
ACCP2005/EJB 2.0/Session 3/27 of 31
Accessing from Client sidePossible Clients
Ordinary JavaBean
Enterprise JavaBean
JSP Page
Servlet
Applet
Enterprise JavaBean
HomeObject
EJB Object
JNDI lookup
create()
Business Methods
ACCP2005/EJB 2.0/Session 3/28 of 31
Summary - 1 The bean class, the EJB object, the remote interface, the home
interface, the home object, the deployment descriptors, and the jar files constitute the enterprise bean.
The bean class contains the implementation of the business logic methods.
The EJB container performs certain important management functions when it intercepts client requests. These management functions are:
* Transaction logic* Security logic* Bean instance logic
The Remote interface duplicates the methods exposed by the bean class.
ACCP2005/EJB 2.0/Session 3/29 of 31
Summary - 2 Responsibilities of the EJB home object:
* Creating EJB objects* Searching for existing EJB Objects* Removing EJB Objects
The deployment descriptor: A file that tells the EJB server about the classes, the home interface, and the remote interface that form the bean.
The lifetime of a session bean may last till such time as that of a client session. It could be as long as a window is open or as long as an application is open. Session beans do not, therefore, survive application server crashes or machine
crashes.
ACCP2005/EJB 2.0/Session 3/30 of 31
Summary - 3 Three classes are essential for deployment:
* Home Interface* Remote Interface* Bean class
The ejb-jar.xml file is a compressed file that contains the declarations of the enterprise bean class, the remote interface and the home interface.
It is important to have a client because EJB will not function without the client. This client can be:
* An ordinary JavaBean* Another EJB* A JSP Page* A servlet* An applet*A stand-alone application
Recommended