enterprise java beans n.v.rajasekhar reddy. definition of ejb ejbs are the components that are the...

30
Enterprise Java Beans N.V.RAJASEKHAR REDDY

Upload: bruno-peters

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

What is the need of EJB? EJB are needed because the following objectives were to be fulfilled, in order to meet the requirements of the enterprise level Applications, to get much better and improved performance. 1.Object distribution 2.Persistence 3.Transaction 4.Security N.V.RAJASEKHAR REDDY

TRANSCRIPT

Page 1: 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

Enterprise Java Beans

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 2: 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

Definition of EJB• EJBs are the components that are the set of classes and interfaces

deployed within a container.• EJB container provides system-level services such as transactions and

security to its enterprise beans. • The business logic of the application can be implemented with the

help of the EJB.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 3: 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

What is the need of EJB?EJB are needed because the following objectives were to be fulfilled, in

order to meet the requirements of the enterprise level Applications, to get much better and improved performance.

1.Object distribution2.Persistence3.Transaction4.Security

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 4: 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

Object distribution In order for an enterprise application to address the issues of scalability, performance and reliability, it is important to distribute the application over

different machines, systems and operating system processes.

Persistence The industry standard is to store information in a Relational Database, a technology that has been used very successfully for a number of years and very well

understood.

Transaction Handling transactions within one database is fairly straightforward but for the programs that have to deal with multiple databases, even seasoned programmers start experiencing difficulties.

security Authentication and authorization are recurring problems that come up all the time in Enterprise Application Development. RMI alone does not worry about security.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 5: 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

Advantages of EJB

1. Provides a mechanism to store and retrieve data in a persistent way.

2. Problems of transaction mechanism is automatically handled.

3. Data transfer between beans is secure.4. Runs in multithreaded environment.5. Portable 6. Reusability.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 6: 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

J2EE application model

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 7: 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

Components of EJB

EJB has 4 components.

• 1. Remote interface2. Home Interface3. Bean Class4. Deployment Descriptor

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 8: 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

Ejb Architecture

Implements

Invokes

Creates / uses

Client

Server

Home Interface(Factory)

EJB Object(Wrapper) Enterprise

Java Bean(Biz Logic)

RemoteInterface

Container

RMI

RMI

Naming Service

You write this

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 9: 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

Remote interface1. A remote interface defines the business methods that a client can call. 2. The business methods are implemented in the enterprise bean code.3. It extends javax.ejb.EJBObject

Home interface1. This interface extends javax.ejb.EJBHome interface.2. Provides remote access to create, find and remove the beans.

Bean class1. The enterprise bean class implements the business methods that a remote

interface defines.

Deployment descriptor1. It gives the XML description of beans

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 10: 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

Example

1. Remote interface

import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.math.*; public interface Converter extends EJBObject

{ public BigDecimal dollarToYen(BigDecimal

dollars) throws RemoteException; public BigDecimal yenToEuro(BigDecimal yen)

throws RemoteException; }

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 11: 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

Example 2.Home interface

import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface ConverterHome extends EJBHome {

   public Converter create() throws

RemoteException, CreateException;

}

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 12: 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

Example3. Bean Classimport java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import java.math.*; public class ConverterBean implements SessionBean {  

 BigDecimal yenRate = new BigDecimal("121.6000");   

BigDecimal euroRate = new BigDecimal("0.0077");   public BigDecimal dollarToYen(BigDecimal dollars) {    

 BigDecimal result = dollars.multiply(yenRate);     

return result.setScale(2,BigDecimal.ROUND_UP);   }

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 13: 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

Example Cont..public BigDecimal yenToEuro(BigDecimal yen) { BigDecimal result = yen.multiply(euroRate);

return result.setScale(2,BigDecimal.ROUND_UP); } public ConverterBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {}public void setSessionContext(SessionContext sc) { }

}

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 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

Example4. Deployment Descriptor

<?xml version =“1.0” ?><!DOCTYPE ejb-jar public - // Sun Microsystems, INC. >

<ejb-jar><description> A Simple Session Ban </description><enterprise-beans><session> <description> … </description> <ejb-name>ConverterBean</ejb-name><home>ConverterHome</home><ejb-class>ConverterBean</ejb-class>…………….</session>

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 15: 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

Types of EJB

There are three types of EJB.1. Session beans2. Entity beans3. Message beans

Session Bean1. Performs task for the client2. Shields the client from complexity.3. A session bean is not shared, it can have only one client.4. To access an application that is deployed on the server, the

client invokes the session bean's methods.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 16: 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

Types of Session beans

Stateless session beans1. Does not maintain a conversational

state.2. All instances of a stateless bean are

equivalent, allowing the EJB container to assign an instance to any client.

3. Support multiple clients4. Provide better scalability for large

applications.5. An application require fewer number

of stateless beans than stateful beans.

6. Offer better performance.

Stateful session beans1. State is retained during the entire

session.2. The instance variables represent

the state of a unique client-bean session.

Stateful session beans are appropriate if1. The bean's state represents the

interaction between the bean and a specific client.

2. The bean needs to hold information about the client across method invocations

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 17: 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

Life cycle of stateless session bean

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 18: 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

Explanation of Life cycle

1. The client initiates the life cycle by invoking the create method.

2. The EJB container instantiates the bean and then invokes the setSessionContext and ejbCreate methods in the session bean.

3. The bean is now ready to have its business methods invoked.4. The EJB container invokes the bean's ejbPassivate method

immediately before passivating it.5. EJB container activates the bean when client invokes the

business method.6. At the end of the life cycle, the client invokes the remove

method and the EJB container calls the bean's ejbRemove method.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 19: 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

Life cycle of stateful session bean

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 20: 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

Entity beans

1. An entity bean represents a business object in a persistent storage mechanism.

2. customers, orders, and products are some examples of the business objects.

3. In the Application Server, the persistent storage mechanism is a relational database.

4. As the entity bean used, its state changes, thus it should be synchronized with the persistence information stored in the database.

5. Each entity bean has an underlying table in a relational database,

6. Each instance of the bean corresponds to a row in that table.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 21: 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

Types of Entity beansContainer Managed persistence 1. The bean's code contains no database access (SQL) calls2. The bean's code is not tied to a specific persistent storage mechanism

(database).3. No need to modify or recompile the bean's code on different J2EE

servers.4. Entity beans are more portable if we use container-managed

persistence

Bean Managed persistence

1. you must write the code for the database access calls.2. More control on the access.3. They are mostly used where underlying database schema is very

complex.4. Less portable because they are tied to the particular database schema

and system.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 22: 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

Life cycle of Entity bean

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 23: 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

Life cycle explanation1. After the EJB container creates the instance, it calls the

setEntityContext method of the entity bean class. 2. After instantiation, the entity bean moves to a pool of available

instances.

When to use Entity Beans?1. The bean represents a business entity and not a procedure. 2. The bean's state must be persistent. If the bean instance

terminates or if the Application Server is shut down, the bean's state still exists in persistent storage

Example : 1. Creditcard: entity bean 2. Creditcard Verification: session bean

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 24: 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

Architecture of the business process

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 25: 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

Message driven beans1. It is a special type of stateless bean that responds to the requests

placed by clients using JMS.2. Allows J2EE applications to process messages asynchronously.3. Unlike a session or entity bean, a message-driven bean has only a

bean class.4. They can process messages from different clients simultaneously.5. clients do not access message-driven beans through interfaces unlike

in session beans or entity beans.6. A message-driven bean's instances retain no data or conversational

state for a specific client.7. All instances of a message-driven bean are equivalent.8. The onMessage method may call helper methods, or it may invoke a

session or entity bean to process the information in the message or to store it in a database.

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 26: 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

The process overview

1. When a message arrives, the container calls the message-driven bean's onMessage method to process the message.

2. The onMessage method normally casts the message to one of the JMS message types.

• BytesMessage • TextMessage• StreamMessage• MapMessage• ObjectMessage N

.V.R

AJA

SE

KH

AR

RE

DD

Y

Page 27: 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

The onMessage method Examplepublic void onMessage(Message inMessage) { TextMessage msg = null; try { if (inMessage instanceof TextMessage) { msg = (TextMessage) inMessage; System.out.println ("MESSAGE BEAN: Message received: " + msg.getText());

} else { System.out.println ("Message of wrong type: " + inMessage.getClass().getName());

} } catch (JMSException e) { e.printStackTrace(); mdc.setRollbackOnly(); } catch (Throwable te) { te.printStackTrace(); }}

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 28: 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

EJB Application Servers1. BEA WebLogic Server

   2. iPlanet

  3. Oracle

   4. Orion Server

   5. WebSphere

     6. NetDynamics

    7. JRun Server

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 29: 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

References

1. http://www.roseindia.net/ejb/ 2. http://www.java.sun.com/developer/onlineTraining/Beans/

EJBTutorial 3. http://docs.jboss.org/ejb3/app-server/tutorial/index.html4. http://openejb.apache.org/hello-world.html

N.V

.RA

JAS

EK

HA

R R

ED

DY

Page 30: 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

Thank you

N.V

.RA

JAS

EK

HA

R R

ED

DY