1 Session Beans and Business Logic Presented By: Sethu Ram Kettimuthu.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Advertisements

12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009.
6/1/20151 Luca Simone Software Engineering 2 a.a. 2001/2002.
1 Softsmith Open Learning - EJB 21-Nov Enterprise Java Beans Introduction –Application ServerApplication Server –Java 2 Enterprise EditionJava.
Overview of The Java Platform Solution for E-Business Applications : JSP, Servlet and EJB.
1 Copyright 2002 © Paulo Merson J2EE – Building Component-based Enterprise Web Applications 05/09/2002 Paulo Merson.
Enterprise Java Beans Welcome to the world of “Distributed System” Presented By: Sameer Nanda Date: 12/17/03.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
J2EE Kenneth M. Anderson CSCI Web Technologies October 3, 2001.
Object Oriented Analysis and Design 1 Chapter 8 - Web Application System Design  Modeling Web Application using UML  Application Server  Web Application.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
EJB Fundamentals Celsina Bignoli
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container.
1 J2EE Components. 2 Application Servers relieve the programming burden for business distributed components. They provide support for system level services.
1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành.
1 J2EE. 2 Agenda 1. Application servers 2. What is J2EE?  Main component types  Application Scenarios  J2EE APIs and Services 3. EJB – a closer look.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Enterprise Java Beans - (EJB)
EJB Enterprise Java Bean.
Seminar: Enterprise JavaBeans. Agenda Agenda Java™ 2 Platform Java™ 2 Platform Java™ 2 Platform,Enterprise Edition(J2EE) Java™ 2 Platform,Enterprise Edition(J2EE)
Entity Beans BMP Celsina Bignoli
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Enterprise JavaBeans Umer Farooq CS6704: Design Patterns & Component FrameworksFebruary 25, 2002.
1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences.
Distributed Systems 1 Master of Information System Management Distributed Systems Persistence.
Enterprise Java Bean Matt. 2 J2EE 3 J2EE Overview.
The Triad of Beans I Oleh: Dini Addiati ( ) Fahrurrozi Rahman ( Y) Irfan Hilmy ( ) Salman Azis A ( ) Aziiz Surahman.
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.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
© jGuru.com Enterprise JavaBeans Fundamentals.
Introduction to Enterprise JavaBeans Topics In Systems Architecture Barry Herbold
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
Enterprise Java Bean Technology Briefing Markus Hebach.
Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Enterprise Java Beans N.V.RAJASEKHAR REDDY. Definition of EJB EJBs are the components that are the set of classes and interfaces deployed within a container.
Copyright © 2002 ProsoftTraining. All rights reserved. Enterprise JavaBeans.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
©NIIT Introducing Enterprise JavaBeans (EJB) Lesson 1A / Slide 1 of 43J2EE Server Components Objectives In this lesson, you will learn about: The features.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Middleware Technology (J2EE/EJB) EJB Fundamentals.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
EJB. Introduction Enterprise Java Beans is a specification for creating server- side scalable, transactional, multi-user secure enterprise-level applications.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
1 EJB 3.0: There’s Something Different About You Martin Jäkle, TSBU Middleware.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Luca Simone Software Engineering 2 a.a. 2001/2002
Objectives In this lesson, you will learn to:
Understanding and Designing with EJB
Enterprise Java Beans Bina Ramamurthy 1/13/2019 B.Ramamurthy.
Understanding and Designing with EJB
Enterprise Java Beans Bina Ramamurthy 4/5/2019 B.Ramamurthy.
Enterprise Java Beans.
Introduction to Session beans
Knowledge Byte In this section, you will learn about:
Presentation transcript:

1 Session Beans and Business Logic Presented By: Sethu Ram Kettimuthu

2 For Starters..

3 A look at the ‘Forest’ What is a middle ware? Middleware is a layer of software between the network and the applications that provides services such as identification, authentication, authorization, directories, and security. By promoting standardization and interoperability, middleware will make advanced network applications much easier to use.

4 Trees of the Forest CORBA

5 Corba (Contd) The CORBA specification only defines a set of conventions and protocols that must be followed by CORBA implementations CORBA does not make any restrictions on language or underlying operating system Because of CORBA's heterogeneous nature, a neutral language was needed to perform the task of defining the interfaces to ORB-based objects You can implement IDL interfaces using any programming language for which an IDL mapping is available

6 E-Speak Fundamentally e-speak is a distributed development environment, just like Java/RMI, just like CORBA, just like the Web itself To access a service, a client: Creates a connection to the e-Speak engine Identifies a contract and vocabulary to use when searching for services Locates the service using ESServiceFinder Invokes the methods specified by the service interface

7

8

9.Net & Com+ With.NET—Microsoft's new Web services platform—your applications, services, and devices work together to provide access to the information you need at anytime and any place.net is MS equivalent of J2EE Com+ is MS equivalent of EJB C# is MS equivalent of Java

10 Now to EJB..at last Why EJB? EJB enables rapid development of mission-critical application that are versatile, reusable and portable across middleware while protecting IT investment and preventing vendor lock-in What is EJB? Enterprise beans are server components written in the Java programming language. Enterprise beans contain the business logic for your application. For example, a checkbook client might invoke the debit and credit methods of an account enterprise bean

11 FeatureEJBCOM+ Component LanguageJava only All (Java: unclear future) PlatformsAllWindows 2000 Middleware Vendors30+Microsoft Legacy Integration RMI/JNI, CORBA, Connectors (future) COM TI, MSMQ, OLE DB ProtocolAny (future: IIOP)DCOM Stateless componentsYes Stateful componentsYesNo Persistent componentsYesNo Queued componentsNoYes Middleware comes with OS NoYes Cluster-wide processors Theoretically unlimited Development toolsChoice of manyMicrosoft Dev Studio

12 key features of the EJB technology EJB components are server-side components written entirely in the Java programming language EJB components contain business logic only System-level services are automatically managed for the EJB component by the EJB server EJB components are fully portable across any EJB server and any OS

13 Types of enterprise beans Session Beans (More on Session Beans rest of the seminar) Entity Beans - An entity bean represents a business object in a persistent storage mechanism such as a database - May be shared by multiple clients - Persistent - even when the EJB container terminates, the entity state remains in a database.

14 Session Beans Purpose : Performs a task for a client Shared Access : May have one client Persistence : Not persistent. When the client terminates its session bean is no longer available.

15 Session Beans – Contd.. The client accesses remote services by invoking the session bean's methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server It is similar to an interactive session The Session Beans Terminates with the client Session beans are powerful because they extend the reach of your clients into remote servers-- yet they're easy to build

16 How to Write Session Beans To write a session enterprise bean class, the class must implement the javax.ejb.SessionBean interface The javax.ejb.SessionBean interface extends the more generic javax.ejb.Enterprise Bean interface So does the javax.ejb.EntityBean

17 Methods in SessionBean Interface Javax.ejb.SessionBean Interface 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; }

18 A look at each method setSessionContext(SessionContext ctx)  Container calls this method to associate the bean with a session context  A session context is the bean’s gateway to interact with the container  The bean can use the session contexts to query the container about the current transactional state,security state etc..

19 A look at each method ejbCreate(..)  Initializes the session bean  Can define one or more ejbCreate() methods and each can take different arguments  Can perform any initialization the bean needs,ie., public class MyBean implements SessionBean { private int memberVar; public void ejbCreate(int init val) { this.memberVar = initval; } …. }

20 A look at each method ejbPassivate()  If too many beans are instantiated, the EJB container can passivate some of them(by writing the beans to a temp storage like a DB or File)  The container can then release the Resources the beans had claimed  Immediately before the Beans are passivated the container calls the ejbPassivate() method  NOTE: Remember that passivation doesn’t apply to stateless bean as it cannot hold the state.

21 A look at each method ejbActivate()  When a client needs to use the passivated bean then the container ‘kicks’ the bean back into memory. This is activation.  Once the bean is back in the memory again, the beans implementation acquires any resource the bean needs.  NOTE: Activation doesn’t apply to stateless session beans as it cannot hold state and can simply be created/destroyed rather than passivated/activated

22 Guess who spoke this "You've heard Al Gore say he invented the internet. Well, if he was so smart, why do all the addresses begin with "W"?“ Any body …?

23 A look at each method ejbRemove()  When the container is about to remove your session bean instance it calls the bean’s ejbRemove() method  ejbRemove is a clean-up method, alerting the bean that it is about to destroyed.  ejbRemove() is a required method of all beans and takes no parametrs.  There is only one ejbRemove() method per bean.

24 A look at each method Business Methods:  There can be zero or more Business methods in your bean  These methods actually solve business problems For eg: public class MyBean implements SessionBean { public int add(int I,int j) { return(I + j) ;} …}  For clients to call your business methods, the business methods must be listed in the bean’s remote interface

25 Major parts of an EJB Application Session Bean Class Home Interface Remote Interface Deployment Descriptor

26 Two types of State Transactional state  Roughly speaking this is the data stored in the persistent store  Multiple client can read and modify the data without conflict  If the App Server crashes or re-started the data is still available in the data store  Example: An order that a customer has placed

27 Two types of State Conversational State :  Cached data either on the Client or Server  Private data that isn’t accessible to other clients  Example : The web shopping cart before the order has been confirmed  REMEMBER: A stateless session bean holds conversations that span a single method call. So it doesn’t hold Conversational state, BUT stateful session bean holds Conversational state

28 Types of Session Beans Stateless Session Beans Stateful Session Beans (We’ll see about this later)

29 Stateless Session Beans(SLSB)  A stateless session bean does not maintain a conversational state for a particular client  When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation  The state is not customized for each client  The SLSB cannot retain state between method calls,but they also cannot retain state after a client passes data to an ejbCreate() call  So all SLSB may expose only a single ejbCreate() method,which takes no parameters

30 Stateless Session Bean Pooling Client Remote Interface EJB Obj bean Stateless session bean pool Invoke()

31 ‘Hello world’ SLSB Function:  The SLSB, a component running in a Distributed Environment will do the mighty task of returning the string ‘Hello World’ to the client.

32 ‘Hello world’ Remote Interface The remote interface is what the 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  import javax.ejb.*;  import java.rmi.RemoteException;  Import java.rmi.remote; Public interface Hello extends EJBObject { public String hello() throws java.rmi.RemoteException; }

33 Implementing the ‘Hello world’ bean /* Stateless Session bean */ import javax.ejb.*; public class HelloBean implements SessionBean { /* EJB required methods */ public void ejbCreate( ) { System.out.println(“Create Method”);} public void ejbremove( ) { System.out.println(“Remove Method”);} public void setSessionContext(SessionContext ctx) {System.out.println(“Set Session Context”);} // Business Methods public String hello() { System.out.println(“Hello()”); return “Hello World”;} }

34 ‘Hello World’ Home Interface The Home Interface for HelloBean is implemented by the EJB Server’s glue-code tools- the implemented object is called the Home Object and serves as the factory for EJB Objects import javax.ejb.*; import java.rmi.RemoteException; /* One create() method is in this Home INTERFACE */ public interface HelloHome extends EJBHome { /* This method creates the EJB Object & return the newly created EJB object*/ Hello create() throws RemoteException, CreateException; }

35 Deployment Descriptor Deployment Descriptor SettingValue Bean Home NameHelloHome Enterprise bean class name.helloworld.HelloBean Home Interface Class name.helloworld.HelloHome Remote Interface class name.helloworld.Hello Environment Variables Re-entrantfalse Stateful or statelessSTATELESS_SESSION Session timeout7 secs

36 EJB-JAR File Last step before deploying Package all the files above in an EJB-Jar file Jar files are compact modules in which to wrap our beans The files to be included are :  Enterprise Bean  Remote Interface  Home interface  Deployment Descriptor

37 Client pseudo-code for Stateless Bean The HelloClient class invokes methods on a simple stateless beans Get system properties for JNDI initialization Form an initial context Get a reference for the home object(factory for EJB Objects) Use the factory to create EJB Object Call the Hello() method and print the string Since the EJB object is done with, remove it /* hello.remove; */ Check for any Exception

38 Lifecycle of a SLSB Because a stateless session bean is never passivated, its life cycle has just two stages: non-existent and ready for the invocation of business methods.

39 Stateful Session Beans Stateful Session Beans are conversational beans Because they hold conversation with clients that span multiple method invocations Stateful Session Bean store conversatinal state within the bean The conversational state is specific to a particular client

40 Passivation of Stateful bean Remote Interface EJB Obj bean Client Passivated Bean stored

41 Stateful Session bean Lifecycle Does not exist ReadyPassive ejbPassivate ejbActivate 2. ejbRemobe 1.remove 1.create 2. setSessionContext 3 ejbCreate

42 CartEJB.java import java.util.*; import javax.ejb.*; public class CartEJB implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new Vector(); }

43 public void ejbCreate(String person, String id) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new CreateException("Invalid id: " + id); } contents = new Vector(); }

44 public void addBook(String title) { contents.addElement(title); } public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + " not in cart."); } } public Vector getContents() { return contents; } public CartEJB() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }

45 The SessionBean Interface The SessionBean interface extends the EnterpriseBean interface, which in turn extends the Serializable interface. The SessionBean interface declares the ejbRemove, ejbActivate, ejbPassivate, and setSessionContext methodsSessionBean

46 The ejbCreate Methods Because an enterprise bean runs inside an EJB container, a client cannot directly instantiate the bean. Only the EJB container can instantiate an enterprise bean. During instantiation, the example program performs these steps: 1. The client invokes a create method on the home object: Cart shoppingCart = home.create(“Sethu","123"); 2. The EJB container instantiates the enterprise bean. 3. The EJB container invokes the appropriate ejbCreate method in CartEJB

47 public void ejbCreate(String person, String id) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new CreateException("Invalid id: " + id); } contents = new Vector(); }

48 Business Methods The primary purpose of a session bean is to run business tasks for the client. The client invokes business methods on the remote object reference that is returned by the create method. From the client's perspective, the business methods appear to run locally, but they actually run remotely in the session bean. The following code snippet shows how the CartClient program invokes the business methods: Cart shoppingCart = home.create("Duke DeEarl", "123");... shoppingCart.addBook("The Martian Chronicles"); shoppingCart.removeBook("Alice In Wonderland"); bookList = shoppingCart.getContents();

49 The CartEJB class implements the business methods in the following code: public void addBook(String title) { contents.addElement(new String(title)); } public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + " not in cart."); } } public Vector getContents() { return contents;}

50 Home Interface A home interface extends the EJBHome interface. The purpose of the home interface is to define the create methods that a client may invoke. The CartClient program, for example, invokes this create method: Cart shoppingCart = home.create("Duke DeEarl", "123"); Every create method in the home interface corresponds to an ejbCreate method in the bean class. The signatures of the ejbCreate methods in the CartEJB class follow: public void ejbCreate(String person) throws CreateException... public void ejbCreate(String person, String id) throws CreateException

51 create methods in the CartHome interface: import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; Cart create(String person, String id) throws RemoteException, CreateException; }

52 Remote Interface The remote interface, which extends javax.ejb.EJBObject, defines the business methods that a client may invoke. Here is the source code for the Cart remote interface : import java.util.*; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; public Vector getContents() throws RemoteException; }

53 You should consider using a stateful session bean if any of the following conditions are true: The bean's state must be initialized when it is created. The bean needs to hold information about the client across method invocations. The client is an interactive application. Since the primary goal of a session bean is to represent a client in the J2EE server, most of your session beans will be stateful. However, sometimes you may want to use stateless session beans: The bean performs a task that is not tailored to the needs of a particular client. For example, you might use a stateless session bean to fetch from a database a commonly used set of data. The bean doesn't need to hold information about the client across method invocation

54 FIRE ……….

55 Am Still Alive !!!!

56 References On Starters : More on ‘HIS’ Quotes: More on EJB: ???????????? Ask Google or Dr. juggy Actually > 1) Text: Mastering EJB by Ed roman 2) 3) 4)