Enterprise Java Beans EJB By Bharath Reddy Allam
Enterprise Java Beans - (EJB) By Bharath Reddy Allam
EJB Overview Enterprise Java Beans Technology is part of larger Framework The J 2 EE (Java 2 Enterprise Edition) Component Architecture for Distributed Systems Framework for creating middleware
Why EJB? z. EJB Architecture simplifies the development of distributed enterprise applications. z. EJB Specification is a solution that is operating-system independent. z. Middleware Independence z. EJB defines a solution that eases the development and deployment of distributed applications
Why EJB? z. EJB enables the development of reusable and portable components. z. EJB has Broad Industry Support: Server Vendors and End Users.
EJB Vs Java. Beans z EJB component model z Java. Beans component logically extends the model defines a standard Java. Beans component model mechanism to develop to support server portable, Java components, such as z EJB components cannot be widgets or controls. manipulated by a visual Java z Java. Beans technology IDE in the same way that can be used in any visual Java. Beans can, They are Java technology prepackaged pieces of integrated development application functionality that environment (IDE) are designed to run in an application server.
EJB Architecture • EJB Server • EJB Container • EJB Client • Enterprise Java Beans • Auxiliary Systems - JNDI, JTA, Java. Mail
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 Vendor - Provides an Application Framework in which to run EJB Containers Application Developer - Who writes client applications that uses EJB.
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
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
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
Enterprise Java Beans Session Bean 1. Stateless 2. Stateful Entity 1. Bean Managed 2. Container Managed
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
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
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
Session. Bean Interface public abstract interface Session. Bean extends Enterprise. Bean{ public void set. Session. Context(Session. Context ctx) throws EJBException, java. rmi. Remote. Exception public void ejb. Remove() throws EJBException, java. rmi. Remote. Exception public void ejb. Activate() throws EJBException, java. rmi. Remote. Exception public void ejb. Passivate() throws EJBException, java. rmi. Remote. Exception }
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. Remote. Exception
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. Remote. Exception and javax. ejb. Create. Exception
Writing a Session Bean-Step 3 Writing the Bean Class (Session. Bean) - should implement Session. Bean interface - Should be a public class, and cannot be abstract or final - contains public constructor with no Arguments - should implement ejb. Create methods whose arguments should correspond to create methods of Home Interface - Return type of ejb. Create methods should be void - throws clause of ejb. Create methods should implement javax. ejb. Create. Exception - implements Business Methods of Remote Interface
Writing an EJB Client Locate the Home Interface Create an Enterprise Bean Instance Invoke a Business Method upon the Enterprise Bean Remove the Bean
Life Cycle of Stateful Session. Bean
Life Cycle of Stateless Session. Bean
Container Vs Bean Managed Entity. Bean-Managed Entity. Bean Container-Managed Entity. Bean 1. Bean is responsible for saving its own state 1. Container is responsible for Saving the state of Bean 2. Entity Bean consists Code for DB calls 2. In DD, Specify the Container managed fields 3. Some times the code Contains DB specific calls which makes non portable 3. Is Independent of Data Store, Such as a Relational Database
Entity. Bean Interface public abstract interface Entity. Bean extends Enterprise. Bean{ public void set. Entity. Context(Entity. Context ctx) throws EJBException, java. rmi. Remote. Exception public void unset. Entity. Context() throws EJBException, java. rmi. Remote. Exception public void ejb. Remove() throws Remove. Exception, EJBException, java. rmi. Remote. Exception public void ejb. Activate() throws EJBException, java. rmi. Remote. Exception public void ejb. Passivate() throws EJBException, java. rmi. Remote. Exception public void ejb. Load() throws EJBException, java. rmi. Remote. Exception public void ejb. Store() throws EJBException, java. rmi. Remote. Exception }
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. Remote. Exception
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. Remote. Exception and javax. ejb. Create. Exception
Writing a Entity Bean - Step 3 Writing the Bean Class (Entity. Bean) - should implement Entity. Bean interface - Should be a public class, and cannot be abstract or final - contains public constructor with no Arguments - should implement ejb. Create and ejb. Post. Create method(s) whose arguments should correspond to create method(s) of Home Interface - Return type of ejb. Create method(s) is Primarykey - Return type of ejb. Post. Create method(s) is void - throws clause of ejb. Create methods should implement javax. ejb. Create. Exception - implements Business Methods of Remote Interface
Life Cycle of Entity Bean
Deployment of EJB’s in J 2 EE Server
Transactions z. Transactions are units of work that can maintain a reliable data source that is accessed by several clients.
Transactions z. 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
Java Transaction Service (JTS) z. JTS is an implementation of an underlying transaction service zjavax. transaction. Transaction. Service allows the application server to manage transaction boundaries.
EJB Transactions EJB Architecture: y. Supports flat transactions y. Supports the two-phase commit protocol - Prepare-ready - Commit y. Supports access to transaction service using the JTS interface.
User. Transaction Interface javax. transaction. User. Transaction ybegin ycommit yrollback yset. Rollback. Only yset. Transaction. Time. Out yget. Status
Container-Managed Isolation Enterprise Bean can still participate via the EJBContext y. The set. Rollback. Only method is used by a Bean to force a rollback (usually due to an application exception). y. The get. Rollback. Only method is used by a Bean to test if the transaction is marked for a rollback.
Bean-Managed Isolation Session Bean Can: z Gain Access to the transaction service y. Define transaction type as Bean in the deployment descriptor y. Ensure methods do not declare attributes that conflict with Bean-Managed demarcation. y. Use the javax. transaction. User. Transaction interface. y. Start a new transaction only after others complete
Bean-Managed Isolation import javax. transaction. User. Transaction; public class Cart. Bean implements Session. Bean{ EJBContext ic = null; // initialize some where else void some. Method(){ User. Transaction ex = ic. get. User. Transaction(); tx. begin(); // do work tx. commit(); } }
Bean-Managed Isolation z. Use the following methods: y. User. Transaction. get. Status method obtains status of a global transaction y. User. Transaction. rollback method rolls back a global transaction z. Do not use the following methods: yset. Rollback. Only() yget. Rollback. Only()
Bean-Managed Demarcation z. Stateful Session Beans y. Methods can return in the middle of a transaction without closing the transaction. y. Subsequent calls can go to any Bean method z. Stateless and Entity Beans y. Methods must complete a transaction before returning.
Vendors Supporting EJB • SUN • Novell • IBM • Netscape • Web. Logic • IONA • Oracle • Gemstone • Inprise • Informix • BEA • Allaire
- Slides: 39