Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Presented By Pradeep K Sahu

2 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

3  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

4 Differences between  No explicit support exists for transactions in Java beans.  EJB may be transactional and the EJB server provide transactional support. Java BeansEnterprise Java Beans  Can 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

5 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

6 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

7 Architecture

8  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

9  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

10  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

11 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 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

12 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

13 The Enterprise Java Bean  Contained within the EJB container and is only accesses through the container  There are two types of EJBeans  Session  Entity (3 rd type Message Driven Bean is added in EJB 2.0)  Session Beans are again two types  Stateless and  Stateful

14 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 of a particular client  Persist across multiple invocations  Do not survive server crashes  Survive server crashes

15  Start the server and execute the client 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  Develop the Client code  Deploy the EJB in the server

16 We have covered Lets Summarize….. Next  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 Guess ?

17 Yes, you are right… Next Session Bean…..

18

19 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

20 Writing a “Hello World !” Stateless Session Bean

21 Writing the Home Interface Requirements for any Home Interface  Extend javax.ejb.EJBHome  Expose at least one create() method.

22 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; }

23 Writing the Remote Interface Requirements for any Remote Interface  Extend javax.ejb.EJBObject  Declare the business methods that are to be exposed.

24 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; }

25 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(). Writing the Bean Class

26 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”; }

27 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)

28 Writing ejb-jar.xml Deployment Descriptor images/green-cube.gif images/orange-cube.gif HelloWorld HelloHome Hello HelloBean Stateless Container HelloWorld Remote * Required

29 Writing weblogic-ejb-jar.xml Deployment Descriptor HelloWorld 10 HelloHome The name which will be shown in the server The JNDI Name for the client to lookup Max no of bean in pool

30 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 Hello.class HelloHome.class HelloBean.class ejb-jar.xml weblogic-ejb-jar.xml green-cube.gif orange-cube.gif Create the jar file with the following command Jar cf *.* For Example jar cf HelloWorld.jar *.* If package is defined then the files should be in the respective package order Like MyPackage/Hello.class in the root Icon image files

31 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 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

32 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= 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.

33 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.

34 Lets take a

35 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.

36 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(); } The Complete java Code for HelloClient.java

37 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 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

38 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! Output

39 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.

40 Sequence diagram for stateless session beans.

41 Presented By Pradeep K Sahu For Your Patience……


Download ppt "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."

Similar presentations


Ads by Google