Enterprise Java Beans (EJB)

Download Report

Transcript Enterprise Java Beans (EJB)

Presented By
Pradeep K Sahu
What will be the Contents of the Seminar ?
What is EJB ?
Differences between Java Bean and Enterprise Java Bean
Why EJB ?
EJB Architecture
Types of EJB
Session
Entity
Writing a Hello World Session EJB
Enterprise Java Beans (EJB) ….
Defines a standard to building distributed server
side systems
Frees the EJB developer to only concentrate on
programming the business logic
Handles the other “plumbing” to handle
transactions, security connection /thread pooling
etc., by delegating this to the vendor
It defines a framework for creating distributed
enterprise middleware
Differences between
Java Beans
Can be either visible or nonvisible
Intended to be local to a
single process on the client
side
Uses the Bean Info classes.
Property Editors and
Customizers to describe it self
Can also be developed as an
ActiveX control
Enterprise Java Beans
Are decidedly Non –Visible
remote objects
Remotely executable
components deployed on the
server
Uses the Deployment
Descriptor to describe itself
Cannot be deployed as an
ActiveX control since OCX
run on the desktop
No explicit support exists for EJB may be transactional and
transactions in Java beans.
the EJB server provide
transactional support.
Why Enterprise Java Beans?
Architectural Independence from middleware
Write Once Run Anywhere (WORA) for server
side components
Establishes role for application development
Takes care of Transaction Management
Provides Distributed Transaction support
Helps create Portable and scalable solutions
Integrates seamlessly with CORBA
Provides vendor specific enhancements
The EJB Architecture
Components of the EJB Architecture are
EJB server
EJB Container
The Home Interface and Home Object
The Remote Interface and EJBObject
EJBeans
Other services like JNDI,JTS & Security
EJB Client
Architecture
The EJB Server
Provides an organized framework for EJB
Containers to execute in
Provides system-services like multiprocessing,
load balancing, device access, JNDI accessible
naming and transaction services available to the
container
Makes EJB Container visible
EJB Containers
Interface between EJBeans and outside world
EJB client never access an EJBeans directly –any
access is done through container- generated
methods which in turn invoke bean methods
Session containers contain session EJBs and
Entity containers contain entity EJBs
The Home Interface and The Home Object
Contains Factory methods for locating creating
and removing instances of EJBs
The EJB developer defines the Home Interface
for his bean
The Home Object is generated by tools
provided by the Container Vender
The Home Object is the implementation of the
Home Interface
The Remote Interface and the EJB Object
The Remote Interface lists the business methods
present in an EJB class
The Remote Interface is defined by the EJB
developer
The EJBObject which is the concern class for the
Remote Interface is generated by the tools
provided by the container vendor
EJB Clients use the methods present in the
Remote Interface to invoke business methods of
the EJBean
The EJB Client
Finds EJB containers using JNDI
Uses EJB containers to invoke EJB methods
Uses the Home Object to locate , create,or destroy
an EJB class.
Uses the EJBObject instance to invoke business
methods of the EJB instance
The Enterprise Java Bean
Contained within the EJB container and is only
accesses through the container
There are two types of EJBeans
Session
Entity
(3rd type Message Driven Bean is added in EJB 2.0)
Session Beans are again two types
Stateless and
Stateful
Difference between Session and Entity Bean
Session Bean
Entity Bean
Are associated with a
particular client
Are shared by multiple
clients
Handle database access
for a particular client
Handle database access
for a multiple clients
Life is limited to the life Persist across multiple
of a particular client
invocations
Do not survive server
crashes
Survive server crashes
Steps involved in Developing an EJB
Define Home Interface
Define Remote Interface
Develop Entity or Session Bean
In the case of Entity Beans, define the Primary
Key Class
Write the Deployment Descriptor / XML File.
Compile all classes and create the Jar file
Generate the container code by using the tools
provided by the container provider
Deploy the EJB in the server
Develop the Client code
Start the server and execute the client
Lets Summarize…..
We have covered
Why we need EJB and its advantages
Components of EJB Architecture
Types of EJB that is Session and Entity Bean
Difference between Session and Entity Bean
Next
Guess ?
Yes, you are right…
Next
Session Bean…..
Introduction
Session Beans

Represents a business process and business
process related logics.
 Are two types
1. Stateless Session Bean : are beans that holds
conversations that span a single method call.
2. Stateful session bean : are beans that hold
conversations with clients that may span many method
calls.
Now let’s write a “Hello World” Stateless Session
Bean(We will deploy in Weblogic)
Writing a
“Hello World !”
Stateless Session Bean
Writing the Home Interface
Requirements for any Home Interface
Extend javax.ejb.EJBHome
Expose at least one create() method.
The Complete java Code for HelloHome.java
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* This is the home interface for HelloBean. This
interface
* is implemented by the EJB Server's tools - the
* implemented object is called the Home Object and
serves
* as a factory for EJB Objects.
*/
public interface HelloHome extends EJBHome {
/*
* This method creates the EJB Object.
*/
Hello create() throws RemoteException,
CreateException;
}
Writing the Remote Interface
Requirements for any Remote Interface
Extend javax.ejb.EJBObject
Declare the business methods that are to be
exposed.
The Complete java Code for Hello.java
import javax.ejb.*;
import java.rmi.RemoteException;
import java.rmi.Remote;
/**
* This is the HelloBean remote interface.
*
* This interface is what clients operate on when
* they interact with EJB objects. The container
* vendor will implement this interface; the
* implemented object is the EJB object, which
* delegates invocations to the actual bean.
*/
public interface Hello extends EJBObject {
/**
* The one method - hello - returns a greeting to
the client.
*/
public String hello() throws
java.rmi.RemoteException;
}
Writing the Bean Class
Requirements for SessionBean
Extend javax.ejb.SessionBean
public interface javax.ejb.SessionBean extends
javax.ejb.EnterpriseBean
{
public abstract void setSessionContext(SessionContext ctx) throws
java.rmi.RemoteException;
public abstract void ejbPassivate() throws java.rmi.RemoteException;
public abstract void ejbActivate() throws java.rmi.RemoteException;
public abstract void ejbRemove() throws java.rmi.RemoteException;
}
Implements the Business methods defined in the
Remote interface.
In our example hello().
The Complete java Code for HelloBean.java
import javax.ejb.*;
public class HelloBean implements SessionBean {
// EJB-required methods
public void ejbCreate() {
System.out.println("ejbCreate()");
}
public void ejbRemove() {
System.out.println("ejbRemove()");
}
public void ejbActivate() {
System.out.println("ejbActivate()");
}
public void ejbPassivate() {
System.out.println("ejbPassivate()");
}
public void setSessionContext(SessionContext ctx) {
System.out.println("setSessionContext()");
}
//Business methods
public String hello{
System,out.println(“hello()”);
return “Hello,World”;
}
}
Deployment Descriptor
To Deploy any Session Bean on Weblogic server 5.1
we need 2 descriptor (XML) file.
1. ejb-jar.xml :This XML file must conform to the
DTD provided by JavaSoft in the EJB 1.1
specification. This XML file is not vendor
specific.
2. weblogic-ejb-jar.xml : specifies deployment
properties required for deploying EJBs in
WebLogic Server (For example defines timeout,
pooling, and clustering behavior for EJBs)
Writing ejb-jar.xml Deployment Descriptor
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN'
'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<small-icon>images/green-cube.gif</small-icon>
<enterprise-beans>
<session>
<small-icon>images/orange-cube.gif</small-icon>
<ejb-name>HelloWorld</ejb-name>
<home>HelloHome</home>
<remote>Hello</remote>
<ejb-class>HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HelloWorld</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Writing weblogic-ejb-jar.xml Deployment Descriptor
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN'
'http://www.bea.com/servers/wls510/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>HelloWorld</ejb-name> The name which will be shown in the server
<caching-descriptor>
<max-beans-in-free-pool>10</max-beans-in-free-pool> Max no of bean in pool
</caching-descriptor>
<jndi-name>HelloHome</jndi-name> The JNDI Name for the client to lookup
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
Creating EJB-jar
After getting all the java files compiled and the 2 xml file
written we have to create the jar file containing all the files
with the required order.For our Hello World application the
files should be in the following order
<Root>
Hello.class
If package is defined then the files
HelloHome.class
should be in the respective package order
HelloBean.class
Like MyPackage/Hello.class in the root
<META-INF>
ejb-jar.xml
weblogic-ejb-jar.xml
<images>
green-cube.gif
orange-cube.gif
Icon image files
Create the jar file with the following command
Jar cf <Jar File Name to be created> *.*
For Example jar cf HelloWorld.jar *.*
Generating EJB Container classes and Deployment
Once the HelloWorld.jar file is created next is to


Create the container classes
Generate the container classes with the help of ejbc
Compiler. The Command is
ejbc <source jarFile> <The target jar file>
Source jar file : is the jar file created by us.(Contains 2 XML
files and Home,Remote and Bean classes.
The target jar file :The jar file which will be created.This will
contain all the required container classes.
For Example :
ejbc HelloWorld.jar HelloWorldEJB.jar
Continue….
Once we get the jar file containing the Container classes
we are ready to deploy the our EJB in Weblogic 5.1 server.
We can deploy in a different ways.We will deploy by
adding the jar file details to the weblogic.properties file so
that the EJB will get automatically loaded when the
application server starts.
The format is
weblogic.ejb.deploy=<The Complete jar file path>
Note : The folder path separator is “/” not ‘\’
For example:
weblogic.ejb.deploy=D:/Java/Programs/Examples/EJB/HeloWorldEJB.jar
After adding the above line restart the WebLogic.So that
the EJB will get loaded.
Lets Summarize…..
We have covered

EJB Architecture and its Components

Home Interface,Remote Interface

Session Bean Details

How to write a Stateless Session EJB

How to deploy an EJB in Weblogic server
Next
How to write the Client application.
Lets take a
How to write the Client program
The Client code performs the following tasks :
•
Looks up a home object
•
Uses the home object to create an EJB Object.
•
Calls the business methods (hello() in our example) on the EJB
object
•
Removes the EJB Object.
The Complete java Code for HelloClient.java
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
public class HelloClient {
public static void main(String[] args) {
try {
// Get System properties for JNDI initialization
Properties props = System.getProperties();
// Form an initial context
Context ctx = new InitialContext(props);
// Get a reference to the home object (the factory for EJB objects)
HelloHome home = (HelloHome) ctx.lookup("HelloHome");
// Use the factory to create the EJB Object
Hello hello = home.create();
//Call the hello() method, and print it
System.out.println(hello.hello());
//Remove the EJB Object
hello.remove();
} catch (Exception e) {
e.printStackTrace();
}
}
Running the Client
The Client needs the following components or parameters

The J2EE class files in the classpath

The Home and Remote Interface in it’s classpath

The JNDI environment information
Running the Client
1. Run the setEnv.bat file present in the Weblogic root folder
to set the class path and other environment settings
2. Run the Client with the JNDI parameters.
In case of weblogic we can run the command by the
following command
java -Djava.naming.factory.initial=weblogic.jndi.TengahInitialContextFactory
-Djava.naming.provider.url=t3://localhost:7001 <ClientClassFile2Run >
For our HelloWorld application it is
java -Djava.naming.factory.initial=weblogic.jndi.TengahInitialContextFactory
-Djava.naming.provider.url=t3://localhost:7001 HelloClient
Output
The Server-Side Output
When we run the client, our container shows the folloeing
output
setSessionContext()
ejbCreate()
hello()
ejbREmove()
The Client-Side Output
After running the client, we can see the following out put :
Hello,World!
Stateless Session Bean Life Cycle
Before Closing the Stateless Session Bean lets have a look on the Life
Cycle and sequence diagram of Stateless Session Bean
Each method call shown is an invocation from the container to the bean
instance.
Sequence diagram for stateless session beans.
For Your Patience……
Presented By
Pradeep K Sahu