1 / 39

Enterprise Java Beans - (EJB)

Enterprise Java Beans - (EJB). By Bharath Reddy Allam. 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. Why EJB?.

yakov
Télécharger la présentation

Enterprise Java Beans - (EJB)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enterprise Java Beans - (EJB) By Bharath Reddy Allam By Bharath Reddy Allam

  2. 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

  3. Why EJB? • EJB Architecture simplifies the development of distributed enterprise applications. • EJB Specification is a solution that is operating-system independent. • Middleware Independence • EJB defines a solution that eases the development and deployment of distributed applications

  4. Why EJB? • EJB enables the development of reusable and portable components. • EJB has Broad Industry Support: Server Vendors and End Users.

  5. 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. EJB Vs JavaBeans

  6. EJB Architecture • EJB Server • EJB Container • EJB Client • Enterprise Java Beans • Auxiliary Systems • - JNDI, JTA, • JavaMail

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

  8. 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

  9. 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

  10. 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

  11. Enterprise Java Beans Entity 1. Bean Managed 2. Container Managed Session Bean 1. Stateless 2. Stateful

  12. 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

  13. Passivation and Activation Passivation - Saving the state of a Bean to a persistent Storage Device and swapping it out Activation - Restoring the state of Bean from a persistent Storage Device and swapping it in Depends upon Container Provider Applies to both Session and Entity Beans

  14. Stateless Vs Stateful Session Beans Stateful 1. Posses State during Client-Bean Session 2. Is passivated 3. One Per Client Stateless 1. No State during Client-Bean Session 2. Is not passivated 3. Can be shared by multiple clients

  15. 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 }

  16. 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

  17. 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

  18. 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

  19. Writing an EJB Client Locate the Home Interface Create an Enterprise Bean Instance Invoke a Business Method upon the Enterprise Bean Remove the Bean

  20. Life Cycle of Stateful SessionBean

  21. Life Cycle of Stateless SessionBean

  22. 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

  23. 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 }

  24. 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

  25. 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

  26. 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

  27. Life Cycle of Entity Bean

  28. Deployment of EJB’s in J2EE Server

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

  30. 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

  31. Java Transaction Service (JTS) • JTS is an implementation of an underlying transaction service • javax.transaction.TransactionService allows the application server to manage transaction boundaries.

  32. 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.

  33. UserTransaction Interface javax.transaction.UserTransaction • begin • commit • rollback • setRollbackOnly • setTransactionTimeOut • getStatus

  34. 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.

  35. 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

  36. 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(); } }

  37. Bean-Managed Isolation • Use the following methods: • UserTransaction.getStatus method obtains status of a global transaction • UserTransaction.rollback method rolls back a global transaction • Do not use the following methods: • setRollbackOnly() • getRollbackOnly()

  38. 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.

  39. Vendors Supporting EJB • SUN • IBM • WebLogic • Oracle • Inprise • BEA • Novell • Netscape • IONA • Gemstone • Informix • Allaire

More Related