12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.

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.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Communicating in J2EE.
19 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Developing Web Services.
21 Copyright © 2005, Oracle. All rights reserved. Oracle Application Server 10g Transaction Support.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Addition Facts
Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009.
Addition 1’s to 20.
Week 1.
Development and Deployment Roles Venugopal Pakanati.
June 1999 EJB1 EJB and CHAIMS Dorothea Beringer Enterprise Java Beans and how to build a CHAIMS infrastructure using the EJB paradigm.
Enterprise Java Beans Adam, Engels, Josh, Marc, Tim.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
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)
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.
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.
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.
Enterprise Java Bean Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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.
© jGuru.com Enterprise JavaBeans Fundamentals.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
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.
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.
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
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.
Entity Bean Chuyên đề Lập trình Java & J2EE Chương 15
Structure of Enterprise Java Beans
Objectives In this lesson, you will learn to:
EJB Types Alessio Bechini June 2002.
Understanding and Designing with EJB
Knowledge Byte In this section, you will learn about:
Enterprise Java Beans.
Presentation transcript:

12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs

12-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe session beans Differentiate stateless session beans from stateful session beans Develop a home interface, a remote interface, and a bean class for session beans Develop a client application to invoke the business methods

12-3 Copyright © 2005, Oracle. All rights reserved. Session Beans A session bean: Implements business processes Is short-lived and has the lifetime of a clients session Does not survive server, machine, or network crashes Is not saved in permanent storage Implements the javax.ejb.SessionBean interface

12-4 Copyright © 2005, Oracle. All rights reserved.

12-5 Copyright © 2005, Oracle. All rights reserved. javax.ejb.SessionBean Interface The SessionBean interface contains the following callback methods: setSessionContext(SessionContext ctx) ejbActivate() ejbPassivate() ejbRemove()

12-6 Copyright © 2005, Oracle. All rights reserved.

12-7 Copyright © 2005, Oracle. All rights reserved. Types of Session Beans There are two types of session beans: Stateless session bean: A stateless session bean does not maintain the state for a client. Stateful session bean: A stateful session bean maintains the state for a client, and the instance variable represents the state of a unique client.

12-8 Copyright © 2005, Oracle. All rights reserved. Types of Session Beans There are two types of session beans: Stateless session bean: A stateless session bean does not maintain the state for a client. Stateful session bean: A stateful session bean maintains the state for a client, and the instance variable represents the state of a unique client.

12-9 Copyright © 2005, Oracle. All rights reserved. When to Use Session Beans The state of the bean need not be persistent. Use stateless session beans when: –The state need not be maintained for a client –A general task must be performed –Data is fetched only from a database, and data manipulation is not necessary Use stateful session beans when: –Interaction between bean and client must be maintained across method calls and transactions –A bean works on logic based on entity beans that represent persistent data

12-10 Copyright © 2005, Oracle. All rights reserved.

12-11 Copyright © 2005, Oracle. All rights reserved. Life Cycle of a Stateless Session Bean Container invokes class.newInstance, setSessionContext(sessCtx), and ejbCreate(). Ready Container invokes ejbRemove(). Does not exist

12-12 Copyright © 2005, Oracle. All rights reserved. Home Interface for Stateless Session Beans import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface StatelessejbHome extends EJBHome { Statelessejb create() throws RemoteException, CreateException; }

12-13 Copyright © 2005, Oracle. All rights reserved. Home Interface for Stateless Session Beans import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface StatelessejbHome extends EJBHome { Statelessejb create() throws RemoteException, CreateException; }

12-14 Copyright © 2005, Oracle. All rights reserved. Remote Interface for Stateless Session Beans import javax.ejb.EJBObject; import java.rmi.*; public interface Statelessejb extends EJBObject { public String incrementValue() throws RemoteException; public int getValue()throws RemoteException; }

12-15 Copyright © 2005, Oracle. All rights reserved. The Session Bean Class The class must be defined as public, must not be final, and must not be abstract. The class must implement ejbCreate() methods: –There must be an ejbCreate() method for each create() method of the home interface. –The signatures of the two methods mentioned above should match. –The return type of the ejbCreate() method should be void. –Remote or create exceptions need not be thrown. The class can optionally implement the SessionSynchronization interface.

12-16 Copyright © 2005, Oracle. All rights reserved.

12-17 Copyright © 2005, Oracle. All rights reserved. The Session Bean Class: Business Methods The bean class may define zero or more methods to process the business logic. The business methods that are to be accessed by the client applications must be public. The business methods must not be declared as final or static. The business methods that are to be accessed by clients must be exposed through the component interface. The method arguments and return types must be legal types for RMI. Application-specific exceptions can be thrown.

12-18 Copyright © 2005, Oracle. All rights reserved. Bean Class for the Stateless Session Bean... public class StatelessejbBean implements SessionBean { int value =0; public void ejbCreate() { } public void ejbActivate() { } public void ejbPassivate(){ } public void ejbRemove() { } public void setSessionContext(SessionContext ctx) { } public String incrementValue() { value++; return " value incremented by 1"; } public int getValue() { return value; }

12-19 Copyright © 2005, Oracle. All rights reserved. Deployment Descriptor Session Bean ( Stateless ) statelessejb Statelessejb StatelessejbHome Statelessejb StatelessejbBean Stateless Container

12-20 Copyright © 2005, Oracle. All rights reserved. Client Application To access methods on the bean instance, an EJB client must perform the following operations: Obtain access to the naming service (Java Naming and Directory Interface [JNDI]) where the beans home interface is published Authenticate itself with the naming service interface Obtain a reference to the beans home interface from the naming service by using the beans URL Invoke the create() method on the home interface Invoke business methods

12-21 Copyright © 2005, Oracle. All rights reserved. Client Application for Stateless Session Beans... public class StatelessejbClient { public static void main(String [] args) { StatelessejbClient statelessejbClient = new StatelessejbClient(); try {Context context = getInitialContext(); StatelessejbHome statelessejbHome = (StatelessejbHome)PortableRemoteObject.narrow (context.lookup("Statelessejb"), StatelessejbHome.class); //create 3 instances Statelessejb obj[] = new Statelessejb[3]; for (int i=0;i<3;i++) { obj[i]= statelessejbHome.create(); }...

12-22 Copyright © 2005, Oracle. All rights reserved.

12-23 Copyright © 2005, Oracle. All rights reserved. Client Application for Stateless Session Beans... // Invoke the business methods with each of the // instances created to observe the state of each // instance for (int i=0;i<3;i++) { System.out.println("Value before increment for object" + i + " "+ obj[i].getValue()); System.out.println( "Calling incrementValue with object" + i+" " +obj[i].incrementValue()); System.out.println("Calling getValue with object" + i+" " +obj[i].getValue()+"\n"); } for (int i=0;i<3;i++) { obj[i].remove(); }...

12-24 Copyright © 2005, Oracle. All rights reserved.

12-25 Copyright © 2005, Oracle. All rights reserved. Life Cycle of a Stateful Session Bean Ready setSessionContext(sessCtx), and ejbCreate() Container invokes ejbRemove() Passivated instances ejbPassivate() ejbActivate() Does not exist

12-26 Copyright © 2005, Oracle. All rights reserved. Home Interface for Stateful Session Bean import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface StatefulejbHome extends EJBHome { Statefulejb create(int x) throws RemoteException, CreateException; }

12-27 Copyright © 2005, Oracle. All rights reserved. Client Application for Stateful Session Bean... Statefulejb obj[]= new Statefulejb[3]; for (int i=0;i<3;i++) { obj[i]= StatefulejbHome.create(0); } for (int i=0;i<3;i++) { System.out.println("Value before increment for object" + i + " "+ obj[i].getValue()); System.out.println( "Calling incrementValue with object" + i+" " +obj[i].incrementValue()); System.out.println("Calling getValue with object" + i+" " +obj[i].getValue()+"\n"); } for (int i=0;i<3;i++) { obj[i].remove(); }...

12-28 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Describe session beans Differentiate stateless session beans from stateful session beans Develop a stateless session bean Develop a client application to invoke a stateless session bean

12-29 Copyright © 2005, Oracle. All rights reserved. Practices 12-1 and 12-2: Overview These practices cover the following topics: Creating a session bean to validate a card Creating a session bean to display the first_name, last_name, , and department_name of an employee whose employee_id is provided

12-30 Copyright © 2005, Oracle. All rights reserved.

12-31 Copyright © 2005, Oracle. All rights reserved.

12-32 Copyright © 2005, Oracle. All rights reserved.

12-33 Copyright © 2005, Oracle. All rights reserved.

12-34 Copyright © 2005, Oracle. All rights reserved.