Introduction To EJB..

Download Report

Transcript Introduction To EJB..

N.V.RAJASEKHAR REDDY
Enterprise Java Beans
• 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.RAJASEKHAR REDDY
Definition of EJB
What is the need of EJB?
1.
2.
3.
4.
Object distribution
Persistence
Transaction
Security
N.V.RAJASEKHAR REDDY
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.
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.
N.V.RAJASEKHAR REDDY
security
Authentication and authorization are recurring problems that come up all the time in
Enterprise Application Development. RMI alone does not worry about security.
1.
2.
3.
4.
5.
6.
Provides a mechanism to store and retrieve data in a
persistent way.
Problems of transaction mechanism is automatically
handled.
Data transfer between beans is secure.
Runs in multithreaded environment.
Portable
Reusability.
N.V.RAJASEKHAR REDDY
Advantages of EJB
N.V.RAJASEKHAR REDDY
J2EE application model
Components of EJB
•
1.
2.
3.
4.
Remote interface
Home Interface
Bean Class
Deployment Descriptor
N.V.RAJASEKHAR REDDY
EJB has 4 components.
Ejb Architecture
Naming Service
RMI
Home Interface
(Factory)
Container
Client
RMI
You write this
Implements
Invokes
Creates / uses
Remote
Interface
EJB Object
(Wrapper)
Enterprise
Java Bean
(Biz Logic)
N.V.RAJASEKHAR REDDY
Server
Remote interface
1. 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
Bean class
1. The enterprise bean class implements the business methods that a remote
interface defines.
Deployment descriptor
1. It gives the XML description of beans
N.V.RAJASEKHAR REDDY
Home interface
1. This interface extends javax.ejb.EJBHome interface.
2. Provides remote access to create, find and remove the beans.
Example
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.RAJASEKHAR REDDY
1. Remote interface
Example
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.RAJASEKHAR REDDY
2.Home interface
3. Bean Class
import 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.RAJASEKHAR REDDY
Example
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.RAJASEKHAR REDDY
Example Cont..
Example
<?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.RAJASEKHAR REDDY
4. Deployment Descriptor
There are three types of EJB.
1. Session beans
2. Entity beans
3. Message beans
Session Bean
1. Performs task for the client
2. 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.RAJASEKHAR REDDY
Types of EJB
Types of Session beans
1. 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 clients
4. Provide better scalability for large
applications.
5. An application require fewer number
of stateless beans than stateful
beans.
6. Offer better performance.
Stateful session beans
1.
2.
State is retained during the entire
session.
The instance variables represent
the state of a unique client-bean
session.
Stateful session beans are appropriate
if
1.
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.RAJASEKHAR REDDY
Stateless session beans
N.V.RAJASEKHAR REDDY
Life cycle of stateless session
bean
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.RAJASEKHAR REDDY
Explanation of Life cycle
N.V.RAJASEKHAR REDDY
Life cycle of stateful session
bean
1.
2.
3.
4.
5.
6.
An entity bean represents a business object in a persistent
storage mechanism.
customers, orders, and products are some examples of the
business objects.
In the Application Server, the persistent storage mechanism
is a relational database.
As the entity bean used, its state changes, thus it should be
synchronized with the persistence information stored in the
database.
Each entity bean has an underlying table in a relational
database,
Each instance of the bean corresponds to a row in that table.
N.V.RAJASEKHAR REDDY
Entity beans
Container Managed persistence
1. The bean's code contains no database access (SQL) calls
2. 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.RAJASEKHAR REDDY
Types of Entity beans
N.V.RAJASEKHAR REDDY
Life cycle of Entity bean
Life cycle explanation
2.
After the EJB container creates the instance, it calls the
setEntityContext method of the entity bean class.
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.RAJASEKHAR REDDY
1.
N.V.RAJASEKHAR REDDY
Architecture of the business
process
1. 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.RAJASEKHAR REDDY
Message driven beans
1. When a message arrives, the container calls the messagedriven 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.RAJASEKHAR REDDY
The process overview
public 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.RAJASEKHAR REDDY
The onMessage method
Example
EJB Application Servers
1. BEA WebLogic Server
3. Oracle
4. Orion Server
5. WebSphere
6. NetDynamics
7. JRun Server
N.V.RAJASEKHAR REDDY
2. iPlanet
1. http://www.roseindia.net/ejb/
2. http://www.java.sun.com/developer/onlineTraining/Beans/EJ
BTutorial
3. http://docs.jboss.org/ejb3/app-server/tutorial/index.html
4. http://openejb.apache.org/hello-world.html
N.V.RAJASEKHAR REDDY
References
N.V.RAJASEKHAR REDDY
Thank you