You are on page 1of 39

Overview of

Enterprise Java Beans

NITESH AMBASTHA

( nitesh@psyon.org )
ACM Seminar Enterprise JavaBeans ™

COVERAGE

 Introduction and Technology Overview

 EJB Architecture and Specification

 Developing a Simple Application

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB : Th e Jargon
Defined
 What is a Bean ?

 What is a Java Bean ?

 What is an Enterprise Java Bean ?

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

JavaBeans Vs EJBs :
 JavaBeans are developed to act and run a certain way.

 They can be on the client or on the server.

 The JavaBeans may not cover all the actions needed


by the enterprise.

 It will take a programmer lots of development time to write


new code for the extra functions a bean would require such as
accessibility, lifecycle management, transaction management,
and security.

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Sun’s Definition :
 EJB architecture is a component architecture for
the development and deployment of component-
based distributed business applications.

 Applications written using EJB architecture are


scalable, transactional and multi-user secure.

 These applications may be written once and then


deployed on any server platform that supports the
EJB Specifications.
8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Evolution of EJB :

T P Monitors
ORBs
•Transaction mgmt
•Mission-criticality • Object – based
•High volume of data • Plug-and-play nature
•Implicit support for • High level abstraction
services rid
b
hy

Component Transaction Monitors

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Advantages of EJB

 Powerful industry-wide standard

 Low development time

 System level services are delegated to vendor

 Integrates seamlessly with CORBA

 Provides for portable, scalable and flexible


applications for the Enterprise

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Advantages of EJB (…contd.)

 You can take any Java class and with little effort
make it a distributed, secure, transactional class

 You can take any data source and make the data
source appear to be a collection of Java objects

 Eliminates distinction between data from a database


and any other source

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

How much do we know ?


 Enterprise JavaBeans will run on almost any platform, and
you can add client systems at any time.
A. True
B. False

 Which of the following is/are true ?

A. JavaBeans and EJBs are interchangeable.


B. EJBs are suitable specific to enterprise applications.
C. EJBs are vendor-independent specifications.

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB™ Archit ecture :

The EJB architecture specifies the responsibilities


and interactions among EJB entities :
 EJB Servers
 EJB Containers
 Enterprise Beans
EJB Clients

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB™ Serv er :

 The EJB Server provides system services and


manages resources :
Process and thread management
System resources management
Database connection pooling and caching
Management API

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB™ Co ntainer
:
Hosts the Enterprise JavaBeans
Provides services to Enterprise JavaBeans
Naming
Life cycle management
Persistence (state management)
Transaction Management
Security
 Interface between Enterprise Bean and outside world
8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Enterpr ise Jav aBeans™


Clas s:
 A specialized Java class where the real business logic
lives
May be developer-written or tool-generated
Distributed over a network
 Transactional
 Secure
Server vendors provide tools that automatically
generate distribution, transaction and security behavior

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB™ Cli ents :

Client access is controlled by the container in which


the enterprise Bean is deployed
 Clients locates an Enterprise JavaBean through
Java Naming and Directory Interface (JNDI)
RMI is the standard method for accessing a bean
over a network

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

JNDI Architecture :

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB Architecture (Conceptual View)

Elements :
• Remote Interface
Session
• Home Interface Bean

• Bean Class

• Deployment Entity
Descriptor Bean

• EJB Container

• EJB Server
8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

. . . The Complete Picture

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Entity Beans :
 Persistent data components, survive failures

 Several Entity Bean instances may represent the same


underlying data

 Entity bean instances can be pooled

 There are two ways to persist Entity Beans :


: container – managed persistence
: bean – managed persistence

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJB CONTAINER

ENTITY BEAN

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Session Beans :

 Represent business processes, work performed for a


client

 Relatively short-lived components, non-persistent

 Two types of session beans :


i. Stateless session beans
i. Stateful session beans

 Session bean invocations are performed in a lock-step

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Sess io n Vs Entit y B ean


Session Bean Entity Bean
 Mandatory for EJB 1.0  Optional for EJB 1.0
 Represents a specific  Represents underlying
client. data object.
( 1 instance per client ) ( clients share instance )
 Short-lived  Long-lived
 Transient  Persistent
 Can be any Java class  Can be a class that maps
to persistent data
 May be transactional  Always transactional
 Business logic bean  Persistent data bean

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

How much do we know ?


Q. Client calls are always intercepted by containers to
provide system-level services.

A. True
B. False

Q. Which of the following is/are true ?


A. Entity beans as well as Stateful Session beans survive
server-crashes.
B. Persistence management is always taken care by the
Container and cannot be controlled by the developer.
C. Stateless Session beans execute fastest.

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

A Simple Application :

 Things we need :

i. A Remote Interface

ii. A Home Interface


iii. A Bean Implementation Class

iv. A Primary Key Class (only for Entity Beans)

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

EJBObject Inter face :

 Remote objects must implement this interface


 Intercepts calls to the EJB Class to add support for:
transactions security threading
EJBObject class has the same methods as the bean
and delegates to the bean for actual behavior
 Clients can never get a reference to a bean’s
EJB Class, only the EJBObject interface.
 EJBObject class checks security and sets up
transaction before delegating method call to the bean

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Remote Interface :

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Converter extends EJBObject {
public double dollarToYen(double dollars) throws
RemoteException;
public double yenToEuro(double yen) throws
RemoteException;
}

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Interface
jav ax.e jb.EJBH ome :
 Home objects must implement this interface

 Returns a reference to a bean by creating or finding it


 Every bean has a EJBHome interface that provides
methods for getting references to one or more beans
 Provides a bean removal interface also
 The EJBHome Class implementation is
provided by the
EJB Server Provider
8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Interface
jav ax.e jb.EJBH ome :
 Home objects must implement this interface

 Returns a reference to a bean by creating or finding it


 Every bean has a EJBHome interface that provides
methods for getting references to one or more beans
 Provides a bean removal interface also
 The EJBHome Class implementation is
provided by the
EJB Server Provider
8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Home Interface :

import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ConverterHome extends EJBHome {
Converter create() throws RemoteException,
CreateException;
}

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Bean Implementation Class :


import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class ConverterEJB implements SessionBean {
public double dollarToYen(double dollars) {
return dollars * 121.6000;
}
public double yenToEuro(double yen) {
return yen * 0.0077;
}
// . . . continued on the next slide
8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Bean Implementation Class :

// continued from the previous slide . . .


public ConverterEJB() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Client Class :
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.*;
import javax.servlet.http.*;
import Converter;
import ConverterHome;
public class ConverterServlet extends HttpServlet {
Converter converter;
public void init() throws ServletException {

// . . . continued on the next slide


8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Client Class : (…contd)


// continued from the previous slide . . .

System.out.println("in init of ConverterServlet");


try {
InitialContext ic = new InitialContext();
Object objref = ic.lookup("java:comp/env/ejb/Converter");
System.out.println("lookup ok");
ConverterHome home =
(ConverterHome)PortableRemoteObject.narrow(objref,ConverterHome.cl
ass);
System.out.println("narrow ok");
converter = home.create();

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Client Class : (…contd)


// continued from the previous slide . . .

System.out.println("create ok");
} catch(Exception e) {
e.printStackTrace();
}
}
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String inputString = req.getParameter("inputString");
Integer inputNumber = new Integer(inputString);

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Client Class : (…contd)


// continued from the previous slide . . .

double amount = converter.dollarToYen(inputNumber.intValue());


double amount2 = converter.yenToEuro(inputNumber.intValue());
res.setContentType("text/html");
PrintWriter out = res.getWriter();
generatePage(out,amount,amount2,inputNumber);
}
private void generatePage(PrintWriter out, double amount,double
amount2,Integer inputNumber) {
out.println("<html><head><title>Input for ConverterServlet</title>");

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

Client Class : (…contd)


// continued from the previous slide . . .

out.println("</head>");
out.println("<body>");
out.println("$"+inputNumber.intValue()+" = "+String.valueOf(amount)+"
yens.");

out.println(inputNumber.intValue()+" Yens =
"+String.valueOf(amount2)+" euro.");
out.println("<p>");
out.println("<form method = get action=\"ConverterAlias\">");
out.println("Please enter an integral amount :");
out.println("<input type=text name=\"inputString\">");
out.println("<p>");
out.println("<input type=submit></form></body></html>");
}

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

What’s new in EJB 2.0 ?

 Introduction of Message – driven Bean

 Radical changes in CMP : Abstract persistence schema

 New query language EJB QL for find methods

 Tightening of specifications to eliminate ambiguities

8th December 2000 ©2000 National Centre for Software Technology, Mumbai
ACM Seminar Enterprise JavaBeans ™

References :

 Enterprise Java Beans - Richard Monson-Haefel (O’Reilly)

 Mastering Java Beans - Ed Roman (Addison Wesley)

 Java Enterprise in a Nutshell - (O’Reilly)


 EJB Specifications ( 1.1 and 2.0 ) - (Sun Microsystems)

 www.java.sun.com/products/j2ee

 www.ejbportal.com/

THANK S
8th December 2000 ! ©2000 National Centre for Software Technology, Mumbai

You might also like