Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Slides:



Advertisements
Similar presentations
A plataforma J2EE.
Advertisements

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
Development and Deployment Roles Venugopal Pakanati.
1 Softsmith Open Learning - EJB 21-Nov Enterprise Java Beans Introduction –Application ServerApplication Server –Java 2 Enterprise EditionJava.
Enterprise Java Beans Adam, Engels, Josh, Marc, Tim.
Enterprise Java Beans Welcome to the world of “Distributed System” Presented By: Sameer Nanda Date: 12/17/03.
Application Server Lecture Paulo Barroso Kris Carver Todd Kitterman Eric Silva.
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)
Organizational Communication Technologies 1 Enterprise Java Beans Detail.
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 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.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Enterprise Java Beans - (EJB)
Seminar: Enterprise JavaBeans. Agenda Agenda Java™ 2 Platform Java™ 2 Platform Java™ 2 Platform,Enterprise Edition(J2EE) Java™ 2 Platform,Enterprise Edition(J2EE)
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
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.
Java Web Development with NetBeans IDE -- Kai Qian Chapter 6 Session Beans.
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 Beans Part II Kyungmin Cho 2001/04/13.
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.
Middleware Technology (J2EE/EJB) Stateful Session Bean.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
1 Introduction to J2EE CS : Software Design Winter /T13 Based on: Advanced Web Applications Development, cs © Eliezer Dekel, Sara.
J2EE Structure & Definitions Catie Welsh CSE 432
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.
1 Session Beans and Business Logic Presented By: Sethu Ram Kettimuthu.
© jGuru.com Enterprise JavaBeans Fundamentals.
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
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.
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.
Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
D Copyright © 2004, Oracle. All rights reserved. BMP Entity EJBs J2EE Connector Architecture.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Enterprise JavaBeans™ Trademark of Sun Microsystems, Inc.
EJB Enterprise Java Beans JAVA Enterprise Edition
Session Beans & Business Logic CS 486 Sagar Deshpande.
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.
JAVA & J2EE UNIT – 8 ENTERPRISE JAVA BEAN - EJB 28-Jun-16 Prepared by: SANTHIYA.M & GANGA V. C. Department: Computer Science & Engineering Course code:
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.
Understanding and Designing with EJB
Luca Simone Software Engineering 2 a.a. 2001/2002
Objectives In this lesson, you will learn to:
EJB Types Alessio Bechini June 2002.
Understanding and Designing with EJB
Understanding and Designing with EJB
Enterprise Java Beans.
Introduction to Session beans
Enterprise Java Beans.
Presentation transcript:

Session Beans -) stateless -) stateful

Session Beans A session bean represents a single client inside the J2EE server. To access an application that is deployed on the server, the client invokes 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. At any given time, only one client has access to the bean instance. The state (i.e. the values of the instance variables) of the bean is not persistent, existing only for a short period of time. A session bean can be stateful or stateless.

Stateful session Beans In a stateful session bean, the instance variables represent the state of a unique client-bean session. This state is often called the conversational state. The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears.

Stateless session Beans 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. When the method is finished, the state is no longer retained.

Stateless vs. stateful session Beans All instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. => Stateless session beans can support multiple clients, and offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.

Stateless vs. stateful session Beans The EJB container can write a stateful session bean to secondary storage. However, stateless session beans are never written to secondary storage. Therefore, stateless beans may offer better performance than stateful beans.

Stateful session Beans – examples of use The bean needs to hold information about the client across method invocations. The bean mediates between the client and the other components of the application, presenting a simplified view to the client. Behind the scenes, the bean manages the work flow of several enterprise beans.

Stateless session Beans – examples of use In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an that confirms an online order. The bean fetches from a database a set of read-only data that is often used by clients.

Stateless Example Stateless Example: Euro Converter

Stateless example – The Component (Remote) Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface Converter extends EJBObject { public double convert(int lire); }

Stateless example – The Home Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface ConverterHome extends EJBHome { public Converter create() throws RemoteException, CreateException; }

Stateless example – The bean package statelessDemo; import java.rmi.*; import javax.ejb.*; public class ConverterBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } private static double ratio= ; public double convert(int lire) { return lire/ratio; }

Stateless example – The descriptor A simple demo of a stateless Bean An Euro Converter Bean A converter from Lire to Euro EuroConverter statelessDemo.ConverterHome statelessDemo.Converter statelessDemo.ConverterBean Stateless Container

Stateless example – The descriptor EuroConverter * Required

Stateless example – The client import javax.ejb.*; import javax.naming.InitialContext; public class ConverterClient { public ConverterClient() { } public static void main(String[] args) { try { InitialContext ctx=new InitialContext(); Object objref=ctx.lookup("L2EConverter"); statelessDemo.ConverterHome home = (statelessDemo.ConverterHome) javax.rmi.PortableRemoteObject.narrow( objref,statelessDemo.ConverterHome.class); statelessDemo.Converter bean=home.create(); Get naming context and object reference Cast to correct type Get a bean instance from container JNDI name

Stateless example – The client int lire=100000; System.out.println(lire+" Lire = "+ bean.convert(lire)+" Euro"); } catch (javax.naming.NamingException ex) { System.out.println("NamingException: "+ex); } catch (ClassCastException cc) { System.out.println(" ClassCastException : "+cc);} catch (javax.ejb.CreateException ce) { System.out.println("CreateException: "+ce); } catch (java.rmi.RemoteException re) { System.out.println("RemoteException: "+re); } Do your business

Stateless example – execution RUN Lire = Euro

Stateful Example Stateful Example: Dollar Converter

Stateful example – The Component (Remote) Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverter extends EJBObject { public double convertInEuro(double dollar) throws java.rmi.RemoteException; public double convertInDollar(double euro) throws java.rmi.RemoteException; public void setRate(double euro_dollar_ratio) throws java.rmi.RemoteException; }

Stateful example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverterHome extends EJBHome { public DollarConverter create() throws RemoteException, CreateException; }

Stateful example – The bean import java.rmi.*; import javax.ejb.*; public class DollarConverterBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; }

Stateful example – The bean private double euro_dollar_ratio=1; public double convertInEuro(double dollar) throws java.rmi.RemoteException{ return dollar/euro_dollar_ratio; } public double convertInDollar(double euro) throws java.rmi.RemoteException{ return euro*euro_dollar_ratio; } public void setRate(double euro_dollar_ratio) throws java.rmi.RemoteException{ this.euro_dollar_ratio=euro_dollar_ratio; }

Stateful example – The descriptor A simple demo of a stateful Bean An Euro to Dollar Converter Bean A converter from Euro to Dollar DollarConverter DollarConverterHome DollarConverter DollarConverterBean Stateful Container

Stateful example – The descriptor DollarConverter * Required

Stateful example – The client import javax.ejb.*; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class ConverterClient { public ConverterClient() {} public static void main(String[] args) { Object objref=null; DollarConverterHome home=null; DollarConverter bean=null; try { InitialContext ctx=new InitialContext(); objref=ctx.lookup("EDC"); home =(DollarConverterHome)PortableRemoteObject.narrow( objref,DollarConverterHome.class); bean=home.create(); Get naming context and object reference Cast to correct type Get a bean instance from container JNDI name

Stateful example – The client bean.setRate(0.978); double euro=1000; System.out.println(euro+" Euro = " +bean.convertInDollar(euro)+" Dollar"); double dollar=1000; System.out.println(dollar+" Dollar = " +bean.convertInEuro(dollar)+" Euro"); } catch (Exception e) { e.printStackTrace(); } Do your business

Stateful example – execution RUN Euro = Dollar Dollar = Euro

Session Beans Lifecycle: client’s view Does not exist Is not referenced Exists Is not referenced Exists Is referenced Does not exist Is referenced Start home.create() Client releases reference Crash, Timeout Client releases reference Client obtains handle object.remove(), home.remove(), Crash, timeout Client invokes method Client invokes method (NoSuchObject Exception)

Stateless session Beans Lifecycle Exists In the pool Does not exist ejbRemoved() Client invokes method (NoSuchObject Exception) 1)newInstance() 2)setSessionContext(sc) 3)ejbCreate() Client invokes create() Container executes: Client invokes remove() Container executes:

Stateful session Beans Lifecycle Passive ejbPassivate() ejbActivate() Exists, method ready in the pool Does not exist Client invokes remove(), or timeout is reached Container executes: ejbRemoved() Client invokes create() Container executes: 1)newInstance() 2)setSessionContext(sc) 3)ejbCreate() Client invokes non TX method Exists, method ready in TX Client invokes TX method AfterBegin() Client invokes commit beforeCompletion() afterCompletion(true) Client invokes rollback afterCompletion(false)