Enterprise Java Beans: an introduction

Slides:



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

11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
Overview of The Java Platform Solution for E-Business Applications : JSP, Servlet and EJB.
Enterprise Applications & Java/J2EE Technologies Dr. Douglas C. Schmidt Professor of EECS.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
J2EE Kenneth M. Anderson CSCI Web Technologies October 3, 2001.
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.
15 - RMI/EJBCSC4071 Distributed Objects Java Remote Method Invocation Enterprise Java Beans.
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.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Enterprise Java Beans - (EJB)
Entity Beans BMP Celsina Bignoli
Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.
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 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.
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.
Enterprise Java Bean Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Introduction to Enterprise JavaBeans Topics In Systems Architecture Barry Herbold
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
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.
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.
Enterprise JavaBeans™ Trademark of Sun Microsystems, Inc.
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.
©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
EJB (Enterprise Java Beans)
Introduction to J2EE Architecture
Structure of Enterprise Java Beans
Introduction to Enterprise JavaBean
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.
Component-based Applications
Component Technology Bina Ramamurthy 2/25/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:
Objectives In this lesson, you will learn about:
Presentation transcript:

Enterprise Java Beans: an introduction

Java E-Commerce © Martin Cooke, 2003 Today’s lecture Why is enterprise computing so complex? Component models and containers Session beans Entity beans 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Why is enterprise computing so complex? session management authorisation persistence transactions Core code 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 A solution Benefits Programming becomes easier Certain aspects are hard eg transactions … … or tedious eg persistence No need to duplicate code or reinvent wheel Can take advantage of 3rd party solutions Business logic can be ported between container providers Scaleability issues taken out of webapp container business logic enterprise bean persistence transactions authorisation sessions object pooling load-balancing network services Drawback Loss of control can impact upon efficiency “middleware” services 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Distributed object technologies CORBA (Object Management Group) Revolutionised distributed computing, but overcomplex programming model, and less portable than expected Java RMI (Sun) DCOM/MTS (Microsoft) EJB (Sun) 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Containers and components

Java E-Commerce © Martin Cooke, 2003 Definitions Component reusable software building block often customisable Container shell in which the component executes provides services all communication with component goes via container 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Definitions The relationship between a container and its components is analogous to that between a webserver and servlets a browser and applets 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Component model Defines structure of interfaces and mechanisms by which it interacts, both with container and with client applications Components are called Enterprise Java Beans 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Containers Manages many beans simultaneously Intercepts all method invocations on beans Pools resources Can ‘swap’ beans in and out: transparent to client applications Provides everything needed by bean eg JDBC access 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Comparison with java beans Enterprise java bean Typically used for widgets, controls Manipulated using visual tools Configured at compile time Run on one machine, in one address space Typically used for larger-grained business processes and objects Can be configured at deploy time 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Using the bean Client Anything: webapp, app or another EJB Container Home interface Used to control the lifecycle methods of the bean: create, remove, find; uses JNDI Home interface create find remove Client app EJB Remote interface Exposes methods that make the bean tick Remote interface Business methods Deployment descriptor Deployment descriptor Defines security, transactional, … behaviour 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Two types of EJB Entity bean Represents persistent data Can be viewed as an object representation of a row in a database (but is more general) Think of it as a noun Session bean Represents transient data and processes Think of it as a verb 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Two types of EJB entity session Object representation of persistent data Identified by primary key Transactional Recoverable after system crash Persistence can be managed by container or by bean Egs: a booking, a customer Represents work being performed by client, and may span several method calls Created by client Exists for a single session Can be stateful or stateless May be transactional usually not recoverable Eg: a reservation handler 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Session beans

Java E-Commerce © Martin Cooke, 2003 Typical uses Price quoting Order entry Compression Complex calculations Database operations Credit card verification ie business logic 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Session beans: 2 flavours Stateless don’t maintain state across method calls Any instance can be used at any time by any client Eg audio compression Stateful Maintain state within and between transactions Each is associated with a specific client Containers manage instance pool Eg shopping cart 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Session bean lifecycle Pooling Number of instances < number of current clients due to thinking time Eg threads, socket connections, database connections, … Concurrency All methods are thread-safe (one-at-a-time) Extra beans created to handle bottlenecks Source: middleware-company.com book 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 A first example Simplest possible bean: stateless session “hello world” bean Recall that we need Home interface Remote interface Bean implementation Deployment descriptor Container create find remove Home interface Client app EJB Remote interface Business methods Deployment descriptor 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Remote method invocation (RMI) RMI enables invocation of methods on remote objects, passing in and receiving real Java objects as arguments and return values Remote objects implement a remote interface which specifies which methods can be invoked remotely Any object that implements java.rmi.remote is callable across the network EJB objects implement java.rmi.remote, hence all EJBs are networkable objects 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 The home interface public interface HelloHome extends javax.ejb.EJBHome {   Hello create() throws java.rmi.RemoteException, javax.ejb.CreateException; } All home interfaces extend javax.ejb.EJBHome All home interfaces must supply a create method which is used for initialisation and can take params, but not here (stateless) Note exceptions: all remote methods must throw a RemoteException Container provides implementation 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 The remote interface public interface Hello extends javax.ejb.EJBObject { public String hello() throws java.rmi.RemoteException; } All remote interfaces extend javax.ejb.EJBObject hello is our single business method 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 The bean class public class HelloBean implements javax.ejb.SessionBean { public void ejbCreate() {}   public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext (javax.ejb.SessionContext ctx) {} public String hello() { return "Hello, World!"; } Business methods Container methods allow container to interact with bean ejbCreate must match create method of home interface ejbRemove called when bean is destroyed ejbActivate and ejbPassivate concepts don’t apply to stateless session beans setSessionContent allows beans to access certain resources eg info about caller identity 11/04/2019 Java E-Commerce © Martin Cooke, 2003

The deployment descriptor <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <session> <ejb-name>Hello</ejb-name> <home>examples.HelloHome</home> <remote>examples.Hello</remote> <ejb-class>examples.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 What’s missing Simplest possible bean; many features not covered or used Vendor-specific deployment information Packaging Deployment Client-access code (next) 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Client application import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties;   public class HelloClient { public static void main(String[] args) throws Exception { Context ctx = new InitialContext(System.getProperties()); HelloHome home = (HelloHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("HelloHome"), HelloHome.class); Hello hello = home.create(); System.out.println(hello.hello()); hello.remove(); } Steps Use of JNDI to find bean RMI-style cast Use home object to create EJB Call business method on bean Remove bean 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Stateful session beans Issue Not so easy to pool beans because of need to maintain conversational state: could easily run out of resources Solution cf operating systems and applications: Passivate: Swap out state and save Activate: Swap in when needed to restore state Bean instance receiving state might not be same as that previously used, but doesn’t matter Has effect of pooling beans 11/04/2019 Java E-Commerce © Martin Cooke, 2003

More on passivation & activation Which? Container dependent, but least recently used is typical When passivated? Any time bean is not in a method call or in a transaction When activated? Container dependent, but often ‘just in time’ What is saved? All non transient member variables How used? ejbPassivate called just before saving, ejbActivate just after restoration. Gives bean chance to relinquish resources or reconstruct transient objects 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Example: counter public class CountBean implements SessionBean { public int val;   public int count() { System.out.println("count()"); return ++val; } public void ejbCreate(int _val) throws CreateException { val = _val; etc 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Deployment descriptor <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <session> <ejb-name>Count</ejb-name> <home>examples.CountHome</home> <remote>examples.Count</remote> <ejb-class>examples.CountBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Client application main Context ctx = new InitialContext(System.getProperties()); CountHome home = (CountHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("CountHome"), CountHome.class); Count count = home.create(0); System.out.println(count.count()); count.remove(); 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Entity beans

Java E-Commerce © Martin Cooke, 2003 Reminder Entity beans provide an in-memory representation of persistent data Can read themselves from storage and persistent themselves back to storage Survive system failures Used to model the nouns of an application Customer Order 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Persistence Container-managed Simple to use Possibly very inefficient given current state-of-the-art in containers Bean-managed Customisable 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Transactional issues Like session beans, entity beans are single-threaded Multiple threads makes transactions very difficult Difficult to produce reliable thread-safe code Like session beans, containers can create multiple instances (pools) For entity beans, this raises the issue of multiple instances of the same data becoming out of synch. Dealt with using transactional isolation 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Pooling Like session beans, entity beans may be pooled ie implement ejbActivate() and ejbPassivate() In addition, state is saved just prior to passivation and loaded just prior to activation 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 BMP vs CMP Source: the J2EE tutorial (2002), Sun 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Example entity bean From WebTomorrow tutorial (see resources) Models a CD Uses container-managed persistence 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 The remote interface import javax.ejb.*; import java.rmi.RemoteException; public interface CD extends EJBObject {   public abstract String getTitle() throws RemoteException;   public abstract void setTitle(String title) throws RemoteException;   public abstract String getId() throws RemoteException;   public abstract void setId(String id) // … more get/set methods for other fields } 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 The home interface import javax.ejb.*; import java.rmi.RemoteException; public interface CDHome extends EJBHome {   public CD create(String id) throws RemoteException, CreateException; public CD findByPrimaryKey (String id) throws RemoteException, FinderException; } NB: this and other finder methods implemented by container 11/04/2019 Java E-Commerce © Martin Cooke, 2003

The bean implementation import javax.ejb.*; import java.rmi.RemoteException; public class CDBean implements EntityBean { public String id; public String title; // other fields   public String getTitle() { return title; } public void setTitle(String _title) { title = _title; 11/04/2019 Java E-Commerce © Martin Cooke, 2003

The bean implementation contd public String ejbCreate(String _id) { id = _id; return null; } // mandatory methods public void ejbPostCreate(String id) {} public void setEntityContent(EntityContext ctx) {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbLoad() {} public void ejbStore() {} public void ejbRemove() {} 11/04/2019 Java E-Commerce © Martin Cooke, 2003

The deployment descriptor <enterprise-beans> <entity> <ejb-name>CD</ejb-name> <home>examples.CDHome</home> <remote>examples.CD</remote> <ejb-class>examples.CDBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <cmp-field><field-name>id</field-name></cmp-field> <cmp-field><field-name>title</field-name></cmp-field> <primkey-field>id</primkey-field> </entity> </enterprise-beans> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

The deployment descriptor, contd <assembly-descriptor> <container-transaction> <method> <ejb-name>CD</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </assembly-descriptor> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 What else is in EJB? Message-driven beans EJB-QL Security Network services Tools for deployment 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 jBoss jBoss.org Free widely-used 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Summary Distributed component models focus developers’ attention on business logic Other middleware services provided by container 2 principal bean types: session & entity, representing process and persistent data Some efficiency concerns at present, but better object-relational mapping tools will help 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Course summary eCommerce wasn’t a blip Now in a more mature phase, but new techniques, notations, tools, etc being developed at a rapid pace Beware investment of time in recent developments -- tendency to disappear just as quickly Don’t reinvent wheel: use 3rd party services for critical aspects (eg online monetary transactions) Distributed, multi-client computing is here to stay with its associated issues: concurrency, load-balancing, security, … Architectural design is key 11/04/2019 Java E-Commerce © Martin Cooke, 2003

Java E-Commerce © Martin Cooke, 2003 Resources Some of the examples in this lecture are derived from a book on EJBs which is in preparation, and available for public comments, at www.middleware-company.com An excellent tutorial on the use of jBoss is given in Creating a distributed Web application in Java using jBoss and Tomcat by Kevin Boone (available at www.web-tomorrow.com) Another useful tutorial is Enterprise Java Beans Fundamentals from the IBM DeveloperWorks site. 11/04/2019 Java E-Commerce © Martin Cooke, 2003