Download presentation
Presentation is loading. Please wait.
Published byBruno Peters Modified over 9 years ago
1
Enterprise Java Beans N.V.RAJASEKHAR REDDY
2
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.RAJASEKHAR REDDY
3
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
4
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.RAJASEKHAR REDDY
5
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.RAJASEKHAR REDDY
6
J2EE application model N.V.RAJASEKHAR REDDY
7
Components of EJB EJB has 4 components. 1.Remote interface 2.Home Interface 3.Bean Class 4.Deployment Descriptor N.V.RAJASEKHAR REDDY
8
Ejb Architecture Implements Invokes Creates / uses Client Server Home Interface (Factory) EJB Object (Wrapper) Enterprise Java Bean (Biz Logic) Remote Interface Container RMI Naming Service You write this N.V.RAJASEKHAR REDDY
9
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 Home interface 1.This interface extends javax.ejb.EJBHome interface. 2.Provides remote access to create, find and remove the beans. 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
10
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.RAJASEKHAR REDDY
11
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.RAJASEKHAR REDDY
12
Example 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
13
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.RAJASEKHAR REDDY
14
Example 4. Deployment Descriptor A Simple Session Ban … ConverterBean ConverterHome ConverterBean ……… ……. N.V.RAJASEKHAR REDDY
15
Types of EJB 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
16
Types of Session beans Stateless 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.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 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
17
Life cycle of stateless session bean N.V.RAJASEKHAR REDDY
18
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.RAJASEKHAR REDDY
19
Life cycle of stateful session bean N.V.RAJASEKHAR REDDY
20
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.RAJASEKHAR REDDY
21
Types of 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
22
Life cycle of Entity bean N.V.RAJASEKHAR REDDY
23
Life cycle explanation 1.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.RAJASEKHAR REDDY
24
Architecture of the business process N.V.RAJASEKHAR REDDY
25
Message driven beans 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
26
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.RAJASEKHAR REDDY
27
The onMessage method Example 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
28
EJB Application Servers 1.BEA WebLogic Server BEA WebLogic Server 2.iPlanet iPlanet 3.Oracle Oracle 4.Orion Server Orion Server 5.WebSphere WebSphere 6.NetDynamics NetDynamics 7.JRun ServerJRun Server N.V.RAJASEKHAR REDDY
29
References 1.http://www.roseindia.net/ejb/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.htmlhttp://docs.jboss.org/ejb3/app-server/tutorial/index.html 4.http://openejb.apache.org/hello-world.html N.V.RAJASEKHAR REDDY
30
Thank you N.V.RAJASEKHAR REDDY
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.