Top Banner
Enterprise Java Beans  EJBs
54

Enterprise Java Beans by Kamalakar Dandu

Apr 05, 2018

Download

Documents

Kamalakar Dandu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 1/54

Enterprise Java Beans EJBs

Page 2: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 2/54

EJB Architecture & design

• EJB components are designed to encapsulate business

logic, and to protect the application developer from having

to worry about many system-level issues, including

transaction, security, scalability, concurrency,

communication, resource management, persistence, error-

handling and operating-environment independence.

• An EJB is just a collection of java classes and an XML file

bundled into a single unit. The java classes follow certainrules and provide specific callback methods, as defined by

the J2EE container environment and EJB specifications.

Page 3: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 3/54

• An EJB runs in an EJB container. The EJB

container runs within an application serverand takes responsibility for system-level

issues.

• This division of labor between the EJBdeveloper and the container, which allows

the developer to concentrate on the business

logic rather than system-level programming,is an important part of the thinking behind

the Enterprise Javabean Technology.

Page 4: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 4/54

• EJBs are a Java Technologies so it should come as

no surprise that they are not tied to any particularoperating system. For example, we can write an

EJB on Windows 2000 and deploy it on Linux,

Solaris or even the AS/400.

• EJBs are reusable components. Component- baseddevelopment has proved its worth in developing

client applications. An EJB is intended to be a

reusable bundle of business logic, even if EJBs are

not sold for reuse outside a single company, it will

be possible to reuse the business logic in different

contexts with one company.

Page 5: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 5/54

• EJBs can work with any thpe of client, EJBs can

be used in conjunction with servlets and jsp to

provide access for web client. EJBs can be access

directly from java client using RMI. CORBA can

be used to access EJBs on a server that supports

RMI/IIOP.• EJBs have many benefits. They can boost

developer productivity. They can help structure

the model behind your company’s interactive web

site, and they can even be made to scale to thetarget e-commerce application.

Page 6: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 6/54

EJBs VS. JavaBeans

• The JavaBean architecture is designed to

provide a format for general-purpose

components, whereas the EnterpriseJavabeans architecture provides a format for

highly specialized business-logic

components deployed in a J2EEenvironment.

Page 7: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 7/54

EJBs Vs. JavaBean

JavaBeans

General purpose componet architecture

Any

Java libraries. Other servicesvery greatly with thecontainer(for example,microsoft word document, javaapplication etc.)

• Component

• Purpose

• Tier of execution

• Services

provided by

runtime

environment

EnterpriseJavaBeans

encapsulation and management

of bisiness logic.

Server

Persistence,transaction,

security,connection pooling, life

cycle services.support for

message service.

Page 8: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 8/54

J2EE Components and

Containers

Page 9: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 9/54

J2EE Components and

Containers

Page 10: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 10/54

The EJB container and its services

• A container is an execution environment for acomponent. The component lives in the containerand the container provides services for thecomponent. Similarly, container often lives in anapplication server, which provides an executionenvironment for it and for other containers.

• Technically a component interacts only with itscontainer and the resources the container provides.However as the interface between the containerand an application server isn’t well defined. 

Page 11: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 11/54

• An EJB component is usually written is to takeadvantage of the services that the containerprovides. Understanding what these services arewill help us to understand when it is appropriate touse EJB components in application design andwhat role they should play. Services provided byEJB container are:

 –  Persistence

 –  Transaction

 –  Security

 –  Scalability

 –  Portability

 –  Manageability

 –  Error-handling

 –  Component framework for business logic.

Page 12: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 12/54

Defining EJB Technology

• Is a server component specification (for

vendors)

• Separates and defines integration of 

development stages:

 – Component creation

 – Application assembly

 – Application deployment

Page 13: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 13/54

EJB Developer Roles

Page 14: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 14/54

 

• Container developer

 –  The one responsible for the tools that create the files and classesthat an enterprise bean needs at deployment time.

• Server developer

 –  The one responsible for the middleware management, whichincludes performance related issues that include load balancing,concurrency management and other low level, system relatedservices.

• System administrator –  The one responsible for the configuration and runtime well being

of the applications and of the entire system.

Page 15: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 15/54

Deployment Services

• Allows components and applications to be

customized at the time they are packaged and

deployed

• J2EE applications: deployed as a set of nested units – Each unit contains a deployment descriptor: an XML-

based text file

• A J2EE Application (.ear file)

 – EJB modules (.jar file)

 – Web modules (.war file)

 – Application client modules (.jar file)

Page 16: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 16/54

EJB Programming Paradigm• Declarative programming and

customizing

Page 17: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 17/54

EJB Programming Paradigm

• Deploying

Page 18: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 18/54

EJB Architecture Overview

• Uniform client access whether local or remote• Home object is a factory for EJBs

• EJB object is the remote object for accessing EJBs

Page 19: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 19/54

EJB Architecture Overview

Page 20: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 20/54

steps for accessing an enterprise bean.

• The client needs to locate the business component and obtain a

reference to it.• JNDI provides access to a lookup service to obtain a reference to thebean's home object.

• A reference is returned to the client.

• The client asks the home object to create or find an enterprise bean.

• The home object creates or finds the bean. It is also responsible for

removing the bean when the client requests it to be removed.• A reference to the remote object (EJB object) is returned to the client.

• The client calls a business method to the EJB object. The EJB objectintercedes the call on behalf of the bean and works with the containerto authenticate the client and to add transaction context to the callingthread. This intercession is a known design pattern called a proxy.

• The EJB object forwards the method call to the bean component whosereturn values are passed to the EJB object.

• The bean might need to access its properties, which is done throughJNDI.These properties were stored in the DD.

• The EJB object returns, to the bean, the values obtained by thebusiness call.

Page 21: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 21/54

EJB Server

• Services:

 – Transaction support

 – Data access

 – System resource

 – Namespace

• Industry support: – Application/ middleware servers

 – Database servers

Page 22: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 22/54

EJB Container

• Can contain multiple Bean classes or homes

 – Often created using code generation by

tools – Is normally provided by EJB server vendor

• Is not visible to the client• Is implemented in different ways by

different vendors

Page 23: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 23/54

EJB Container

•  Provides some standard services:

 – Persistence

 – Transaction control – Security

 – EJB instance lifecycle management

 – EJB instance identification

Page 24: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 24/54

Home Interface

• Factory interface for the bean:

 – Interface provided by EJB developer

 – Class generated by vendor tool

• One factory class per bean class

• Factory instance is installed into the server’snaming service

Page 25: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 25/54

Remote Interface

• Interface is provided by bean developer.

• Implementation is provided by container

tools, which include:

 – Stubs and skeletons

 – The EJB Object

Page 26: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 26/54

EJB Object

• The EJB Object is a wrapper object that acts as aproxy to the EJB instance.

 –  Interface provided by Bean developer

 –  Implementation class generated by vendor tool

• Each business method has a corresponding

wrapper method in the EJB Object – Wrapper method implements container- provided

services

Page 27: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 27/54

Stubs and Skeletons

• Stub is downloaded from server

• Communication between stub and skeleton can be

proprietary, yet EJB code is still portable

Page 28: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 28/54

Types of EJBs

Page 29: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 29/54

Session and Entity beans

Page 30: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 30/54

Session and Entity Beans

• Typical Architecture

Page 31: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 31/54

Message Driven Bean

• A message-driven bean is a message listener

that can reliably consume messages from a

queue or a durable subscription

Page 32: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 32/54

Message Driven Bean

• It has no home or remote interfaces

• It resembles a stateless session bean

• Through the MDB, the EJB 2.0 Container sets

itself up as a listener for asynchronous invocationand directly invokes the bean (no interfaces),

which then behaves like an enterprise bean.

• Container can pool instances to enhances

scalability

• Client interaction remains same

Page 33: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 33/54

Varieties of Beans

• EJB 2.0 specification provides three models forcallback methods and run-time lifecycle. EJB thatimpelemnt these model are entity beans, sessionbeans and message-driven beans.

• Session Bean

 –  Stateful session bean

 –  Stateless session bean

• Entity Bean –  Container managed persistence bean (CMP)

 –  Bean managed persistence bean (BMP)

• Message Driven Bean

Page 34: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 34/54

Session beans and Business logic

• There are two types of session beans: stateful andstateless. These two types have much in common:

 –  Both impelement the javax.ejb.SessionBean interface,and therefore have the same container callbacks.

 –  Both represent a private resource for the client thatcreated them.

 –  Both are intended to model a process or a task.

 –  Both can update shared data, but do not represent that

shared data in the way that an entity bean does.• In fact, the only way that an EJB container can

distinguish a stateless session bean from a statefulsession bean is to look in the XML discriptor file

to find the session-type.

Page 35: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 35/54

The primary difference betweenthe two types of bean is

• A stateful session bean can keep databetween client accesses. A stateless sessionbean cannot.

Page 36: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 36/54

• A session bean is intended for use by a single

client at a time. It may provide business logic such

as calculating the rate of return for an investment,

or it may save state such as shopping cart for a

web client, when the client leave the website or

the application is shut down, the session bean isfree to disappear. It is no longer available for

access by other clients.

• As its name suggest a session bean is similar to an

interactive session. A session bean is not shared, itcan have only one client.

Page 37: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 37/54

Life cycle diagram of a stateless session bean

Does not Exist

Method ready pool

Class.newInstance()

setSessionContext()

ejbCreate()

ejbRemove()

unsetSessionContext()

Business methods

Life cycle diagram of a stateful session bean

Page 38: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 38/54

Life cycle diagram of a stateful session bean

Does not Exist

Method ready pool

Class.newInstance()

setSessionContext()

ejbCreate()

ejbRemove()

unsetSessionContext()

Business methods

Passivate State

Page 39: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 39/54

Steps to create Session bean

• Create a Remote interface

• Create a home interface

• Create a bean class• Write deployment descriptor

• Create .jar module

• Upload in any application server• Create a client

• Run client

Page 40: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 40/54

Creating a Remote Interface

package myejbs;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

public interface Hello extends EJBObject

{

public String welcome(String nm) throwsRemoteException;

}

Page 41: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 41/54

Creating a Home Interface

package myejbs;

import javax.ejb.EJBHome;

import javax.ejb.CreateException;

import java.rmi.RemoteException;

public interface HelloHome extends EJBHome

{public Hello create() throws RemoteException,

CreateException;

}

Page 42: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 42/54

Creating a Bean Class

package myejbs;

import javax.ejb.*;

import java.rmi.*;

public class HelloBean implements SessionBean

{

public void ejbCreate() {}

public void ejbRemove() {}

public void ejbActivate() {}public void ejbPassivate() {}

public void setSessionContext(SessionContext ctx) {}

public void unsetSessionContext(SessionContext ctx) {}

public String welcome(String nm)

{String x = "welcome "+nm+" to EJB Technologies";

return x;

}

}

Page 43: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 43/54

Creating a Client Classimport javax. naming.Context;

import javax.naming.InitialContext;

import java.util.*;

import javax.rmi.PortableRemoteObject;

import java.rmi.*;

public class Helloclient

{

public static void main(String args[])throws Exception

{

Properties p = new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");

p.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");

Context ctx = new InitialContext(p);

Object obj = ctx.lookup("HelloBean");

HelloHome h = (HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);

Hello r = h.create();

String str = r.welcome("ritu");

System.out.println(str);

}

}

Page 44: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 44/54

Example of Stateful Session Bean

• Create InvalidBookName exception class

• Create Book interface(remote interface)

• Create BookHome interface(home

interface)

• Create BookBean class(bean class)

• Create BookClient class(client program)

Page 45: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 45/54

Creating a Remote Interface

package myejbs;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

import java.util.*;

public interface Book extends EJBObject

{

public String getBookName() throws RemoteException;

public void addBook(String nm) throws RemoteException;

public void removeBook(String bname) throws RemoteException,InvalidBookName;

public Collection getCartContents() throws RemoteException;

}

Page 46: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 46/54

Creating a Home Interface

package myejbs;

import javax.ejb.*;

import java.rmi.*;

public interface BookHome extends EJBHome

{

public Book create() throws RemoteException,CreateException;

}

C ti B Cl

Page 47: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 47/54

Creating a Bean Class

package myejbs;

import javax.ejb.*;import java.util.*;

import java.rmi.*;

public class BookBean implements SessionBean

{

String bnm = null; Vector cart;public void ejbCreate()

{

cart = new Vector();

try

{addBook(bnm);

}

catch(RemoteException e){}

}

Page 48: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 48/54

public void ejbRemove(){}

public void ejbActivate(){}

public void ejbPassivate(){}

public void setSessionContext(SessionContext ctx){}

public void unsetSessionContext(SessionContext ctx){}

public String getBookName()throws RemoteException

{ return bnm; }

public void setBookName(String bnm)

{ this.bnm = bnm; }

public void addBook(String bnm)throws RemoteException{ cart.addElement(bnm); }

public void removeBook(String bnm)throws InvalidBookName,RemoteException

{

boolean r = cart.removeElement(bnm);

if(r == false)

throw new InvalidBookName();

}

public Collection getCartContents()throws RemoteException

{ return cart; }

}

Page 49: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 49/54

Creating a Client Classpackage myejbs;

import javax. naming.Context;

import javax.naming.InitialContext;

import java.util.*;

import javax.rmi.PortableRemoteObject;

import java.rmi.*;

public class Bookclient

{

public static void main(String args[])throws Exception

{

Properties p = new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitil

ContextFactory");

p.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");

Page 50: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 50/54

Context ctx = new InitialContext(p);

Object obj = ctx.lookup("BookBean");

BookHome h =(BookHome)PortableRemoteObject.narrow(obj,BookHome.class);

Book r = h.create();

r.addBook("java");

r.addBook("ejb");

r.addBook("xml");

r.addBook("jsp");Collection c = r.getCartContents();

System.out.println(c);

r.removeBook("java");

c = r.getCartContents();

System.out.println(c);r.removeBook("j2ee");

c = r.getCartContents();

System.out.println(c);

}

}

Page 51: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 51/54

Entity Bean and Business Logic

• Entity Enterprise JavaBeans represent the entity objects in

our analysis model.

• They can correspond to real-world concepts, such as

customer or product or they can correspond to abstractions,

such as manufacturing process, company policies orcustomer purchases.

• Session bean can access any data that an entity bean can.

• Although a session bean can access data, it can’t provide

an object-oriented representation of that data.

• How does an entity bean differ? Why can’t you have a

‘customer’ session bean or a ‘product’ session bean? 

Page 52: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 52/54

• The state maintained by stateful session bean is private, in

the sense that only the client that is currently using the

session bean manipulate this state.

• Entity beans are different because their state is stored inthe database; several clients can therefore access their state

simultaneously.

• So fundamental problem in representing an object with a

session bean is in how that state is made available toclients of that bean.

• An entity bean is a single point of access for that data. Any

client that accesses the data will go through that entity

bean.• A session bean, on other hand, is only accessible to a

single client. If there are multiple clients, there will be

multiple session beans.

Life cycle diagram of an Entity bean

Page 53: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 53/54

unsetSessionContext()

ejbRemove()

Life cycle diagram of an Entity bean

Does not Exist

Pooled state

Class.newInstance()

setEntityContext()

Business methods

Ready

ejbStore()ejbLoad()

Page 54: Enterprise Java Beans by Kamalakar Dandu

8/2/2019 Enterprise Java Beans by Kamalakar Dandu

http://slidepdf.com/reader/full/enterprise-java-beans-by-kamalakar-dandu 54/54

CMP Vs. BMP

The EJB container can manage the processof saving and restoring the state of yourentity beans. This is known as container

managed persistence (CMP).

Alternatively, bean developers can takecontrol of the bean’s persistencethemselves, this is known as bean-managedpersistence (BMP).