Top Banner
Enterprise Java Beans - (EJB) By Bharath Reddy Allam By Bharath Reddy Allam
39
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 - (EJB) By Bharath Reddy Allam.

Enterprise Java Beans - (EJB)

By Bharath Reddy Allam

By Bharath Reddy Allam

Page 2: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Overview

Enterprise Java Beans Technology is part of larger Framework -

The J2EE (Java 2 Enterprise Edition)

Component Architecture for Distributed Systems

Framework for creating middleware

Page 3: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Why EJB?

EJB Architecture simplifies the development of distributed enterprise applications.

EJB Specification is a solution that is operating-system independent.

Middleware IndependenceEJB defines a solution that eases the

development and deployment of distributed applications

Page 4: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Why EJB?

EJB enables the development of reusable and portable components.

EJB has Broad Industry Support: Server Vendors and End Users.

Page 5: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Vs JavaBeans

JavaBeans component model defines a standard mechanism to develop portable, Java components, such as widgets or controls.

JavaBeans technology can be used in any visual Java technology integrated development environment (IDE)

EJB component model logically extends the JavaBeans component model to support server components.

EJB components cannot be manipulated by a visual Java IDE in the same way that JavaBeans can, They are prepackaged pieces of application functionality that are designed to run in an application server.

Page 6: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Architecture

•EJB Server

•EJB Container

•EJB Client

•Enterprise Java Beans

•Auxiliary Systems - JNDI, JTA, JavaMail

Page 7: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Roles in EJB Development

EJB Developer - Person who provides implementations of EJB classesEJB Deployer - Responsible for Deploying EJB’s in EJB ServerEJB Container Vendor - Provides software to install an EJB into an EJB ServerEJB Server Vendor - Provides an Application Framework in which to run EJB ContainersApplication Developer - Who writes client applications that uses EJB.

Page 8: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Server

Is a High Level Process or Application that

Manages and makes EJB Containers Visible

Provides Access to System Services

Provides Naming and Transaction Services

Page 9: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Container

Interface between Enterprise Java Bean and outside world

Accessing of EJB is done through Container generated

methods, which in turn call the EJB methods

Page 10: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Clients

Is an Application, Servlet, JSP or Applet that

•Find EJB Containers via JNDI

•Uses EJBHome to Create or Remove EJB from Container

•Uses EJBObject to Invoke Business Methods of EJB

Page 11: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Enterprise Java Beans

Session Bean 1. Stateless 2. Stateful

Entity 1. Bean Managed 2. Container Managed

Page 12: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Session Vs Entity Beans

Session 1. Performs a task for a Client 2. Associated with a particular client 3. Non Persistent and do not survive System Shutdown

Entity 1. Represents a business entity Object that exists in a persistent Storage 2. Shared by multiple clients 3. Persistent across multiple Invocations and survives System Shutdown

Page 13: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Passivation and Activation

Passivation - Saving the state of a Bean to a persistent Storage Device and swapping it outActivation - Restoring the state of Bean from a persistent Storage Device and swapping it inDepends upon Container ProviderApplies to both Session and Entity Beans

Page 14: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Stateless Vs Stateful Session Beans

Stateless 1. No State during Client-Bean Session 2. Is not passivated 3. Can be shared by multiple clients

Stateful 1. Posses State during Client-Bean Session 2. Is passivated 3. One Per Client

Page 15: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

SessionBean Interface

public abstract interface SessionBean extends EnterpriseBean{

public void setSessionContext(SessionContext ctx) throws

EJBException, java.rmi.RemoteException

public void ejbRemove() throws EJBException, java.rmi.RemoteException

public void ejbActivate() throws EJBException, java.rmi.RemoteException

public void ejbPassivate() throws EJBException, java.rmi.RemoteException

}

Page 16: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing a Session Bean-Step 1

Create a Remote Interface - Should extend EJBObject - Provide the Business methods - Arguments and Return types must be valid RMI types - The throws Clause of the Business methods must include java.rmi.RemoteException

Page 17: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing a Session Bean-Step 2

Create Home Interface - Should extend EJBHome - The argument and return types of create method(s) must be valid RMI types - create method(s) should return the Remote Interface - Throws clause of create method(s) must include java.rmi.RemoteException and javax.ejb.CreateException

Page 18: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing a Session Bean-Step 3

Writing the Bean Class (SessionBean)

- should implement SessionBean interface

- Should be a public class, and cannot be abstract or final

- contains public constructor with no Arguments

- should implement ejbCreate methods whose arguments should correspond

to create methods of Home Interface

- Return type of ejbCreate methods should be void

- throws clause of ejbCreate methods should implement

javax.ejb.CreateException

- implements Business Methods of Remote Interface

Page 19: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing an EJB Client

Locate the Home Interface

Create an Enterprise Bean Instance

Invoke a Business Method upon the Enterprise Bean

Remove the Bean

Page 20: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Life Cycle of Stateful SessionBean

Page 21: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Life Cycle of Stateless SessionBean

Page 22: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Container Vs Bean Managed EntityBean

Bean-Managed EntityBean 1. Bean is responsible for saving its own state

2. Entity Bean consists Code for DB calls

3. Some times the code Contains DB specific calls which makes non portable

Container-Managed EntityBean 1. Container is responsible for Saving the state of Bean

2. In DD, Specify the Container managed fields

3. Is Independent of Data Store, Such as a Relational Database

Page 23: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EntityBean Interface

public abstract interface EntityBean extends EnterpriseBean{ public void setEntityContext(EntityContext ctx) throws EJBException, java.rmi.RemoteException public void unsetEntityContext() throws EJBException, java.rmi.RemoteException public void ejbRemove() throws RemoveException, EJBException, java.rmi.RemoteException public void ejbActivate() throws EJBException, java.rmi.RemoteException public void ejbPassivate() throws EJBException, java.rmi.RemoteException public void ejbLoad() throws EJBException, java.rmi.RemoteException public void ejbStore() throws EJBException, java.rmi.RemoteException}

Page 24: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing a Entity Bean - Step 1

Create a Remote Interface - Should extend EJBObject - Provide the Business methods - Arguments and Return types must be valid RMI types - The throws Clause of the Business methods must include java.rmi.RemoteException

Page 25: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing a Entity Bean - Step 2

Create Home Interface - Should extend EJBHome - The argument and return types of create method(s) must be valid RMI types - create method(s) should return the Remote Interface - Throws clause of create method(s) must include java.rmi.RemoteException and javax.ejb.CreateException

Page 26: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Writing a Entity Bean - Step 3

Writing the Bean Class (EntityBean) - should implement EntityBean interface - Should be a public class, and cannot be abstract or final - contains public constructor with no Arguments - should implement ejbCreate and ejbPostCreate method(s) whose arguments should correspond to create method(s) of Home Interface - Return type of ejbCreate method(s) is Primarykey - Return type of ejbPostCreate method(s) is void - throws clause of ejbCreate methods should implement javax.ejb.CreateException - implements Business Methods of Remote Interface

Page 27: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Life Cycle of Entity Bean

Page 28: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Deployment of EJB’s in J2EE Server

Page 29: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Transactions

Transactions are units of work that can maintain a reliable data source that is accessed by several clients.

Page 30: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Transactions

Transactions have ACID Properties- Atomicity: A transaction either commits

or aborts- Consistency: A transaction correctly

manages the changing state of a system- Isolation: A transaction’s effects are

isolated from other transactions’ effects- Durability: The result of a transaction

persists

Page 31: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Java Transaction Service (JTS)

JTS is an implementation of an underlying transaction service

javax.transaction.TransactionService allows the application server to manage transaction boundaries.

Page 32: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

EJB Transactions

EJB Architecture: Supports flat transactions Supports the two-phase commit

protocol- Prepare-ready- Commit

Supports access to transaction service using the JTS interface.

Page 33: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

UserTransaction Interface

javax.transaction.UserTransaction begin commit rollback setRollbackOnly setTransactionTimeOut getStatus

Page 34: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Container-Managed Isolation

Enterprise Bean can still participate via the EJBContext The setRollbackOnly method is used by

a Bean to force a rollback (usually due to an application exception).

The getRollbackOnly method is used by a Bean to test if the transaction is marked for a rollback.

Page 35: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Bean-Managed Isolation

Session Bean Can:Gain Access to the transaction service

Define transaction type as Bean in the deployment descriptor

Ensure methods do not declare attributes that conflict with Bean-Managed demarcation.

Use the javax.transaction.UserTransaction interface.

Start a new transaction only after others complete

Page 36: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Bean-Managed Isolation

import javax.transaction.UserTransaction;public class CartBean implements SessionBean{

EJBContext ic = null; // initialize some where else void someMethod(){ UserTransaction ex = ic.getUserTransaction(); tx.begin(); // do work tx.commit(); }}

Page 37: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Bean-Managed Isolation

Use the following methods: UserTransaction.getStatus method

obtains status of a global transaction UserTransaction.rollback method rolls

back a global transactionDo not use the following methods:

setRollbackOnly() getRollbackOnly()

Page 38: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Bean-Managed Demarcation

Stateful Session Beans Methods can return in the middle of a

transaction without closing the transaction.

Subsequent calls can go to any Bean method

Stateless and Entity Beans Methods must complete a transaction

before returning.

Page 39: Enterprise Java Beans - (EJB) By Bharath Reddy Allam.

Vendors Supporting EJB

•SUN

•IBM

•WebLogic

•Oracle

•Inprise

•BEA

•Novell

•Netscape

•IONA

•Gemstone

•Informix

•Allaire