Post on 12-Jan-2016
transcript
Presented By
Pradeep K Sahu
What will be the Contents of the Seminar ?
What is EJB ?
EJB Architecture
Types of EJB
Session
Entity
Why EJB ?
Writing a Hello World Session EJB
Differences between Java Bean and Enterprise Java Bean
It defines a framework for creating distributed enterprise middleware
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
Differences between
No explicit support exists for transactions in Java beans.
EJB may be transactional and the EJB server provide transactional support.
Java Beans Enterprise Java BeansCan be either visible or non-visible
Are decidedly Non –Visible remote objects
Intended to be local to a single process on the client side
Remotely executable components deployed on the server
Uses the Bean Info classes. Property Editors and Customizers to describe it self
Uses the Deployment Descriptor to describe itself
Can also be developed as an ActiveX control
Cannot be deployed as an ActiveX control since OCX run on the desktop
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
Components of the EJB Architecture are
The EJB Architecture
EJB Client
EJB server
EJB Container
The Home Interface and Home Object
The Remote Interface and EJBObject
EJBeans
Other services like JNDI,JTS & Security
Architecture
Makes EJB Container visible
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
Session containers contain session EJBs and Entity containers contain entity EJBs
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
The Home Object is the implementation of the Home Interface
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 Remote Interface and the EJB Object
EJB Clients use the methods present in the Remote Interface to invoke business methods of the EJBean
The Remote Interface lists the business methods present in an EJB classThe Remote Interface is defined by the EJB developerThe EJBObject which is the concern class for the Remote Interface is generated by the tools provided by the container vendor
The EJB Client
Finds EJB containers using JNDIUses EJB containers to invoke EJB methodsUses 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 typesStateless and
Stateful
Difference between Session and Entity Bean
Session Bean Entity BeanAre 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 of a particular client
Persist across multiple invocations
Do not survive server crashes
Survive server crashes
Start the server and execute the client
Steps involved in Developing an EJB
Define Home InterfaceDefine Remote InterfaceDevelop 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 fileGenerate the container code by using the tools provided by the container provider
Develop the Client code
Deploy the EJB in the server
We have covered
Lets Summarize…..
Next
Why we need EJB and its advantagesComponents of EJB ArchitectureTypes of EJB that is Session and Entity BeanDifference between Session and Entity Bean
Guess ?
Yes, you are right…
Next Session Bean…..
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)
Introduction
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;}
Requirements for SessionBean
Extend javax.ejb.SessionBeanpublic 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().
Writing the Bean Class
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> <caching-descriptor>
<max-beans-in-free-pool>10</max-beans-in-free-pool> </caching-descriptor> <jndi-name>HelloHome</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar>
The name which will be shown in the server
The JNDI Name for the client to lookup
Max no of bean in pool
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.classHelloHome.classHelloBean.class<META-INF>
ejb-jar.xml weblogic-ejb-jar.xml
<images> green-cube.gif
orange-cube.gif
Create the jar file with the following command
Jar cf <Jar File Name to be created> *.*
For Example jar cf HelloWorld.jar *.*
If package is defined then the files should be in the respective package orderLike MyPackage/Hello.class in the root
Icon image files
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 isweblogic.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.
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 initializationProperties props = System.getProperties();
// Form an initial contextContext 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 ObjectHello hello = home.create();
//Call the hello() method, and print itSystem.out.println(hello.hello());
//Remove the EJB Objecthello.remove();
} catch (Exception e) {e.printStackTrace();
} }}
The Complete java Code for HelloClient.java
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 informationRunning 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 commandjava -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
Running the Client
The Server-Side OutputWhen we run the client, our container shows the folloeing output
setSessionContext()ejbCreate()hello()ejbREmove()
The Client-Side OutputAfter running the client, we can see the following out put :
Hello,World!
Output
Stateless Session Bean Life CycleBefore 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.
Presented By
Pradeep K Sahu
For Your Patience……