280 likes | 403 Views
Creating the Business Tier: Enterprise JavaBeans. Objectives. After completing this lesson, you should be able to do the following: Define an Enterprise JavaBean Describe the Enterprise JavaBeans (EJB) architecture Describe the types of EJBs and when they are used Explain EJB interfaces
E N D
Objectives • After completing this lesson, you should be able to do the following: • Define an Enterprise JavaBean • Describe the Enterprise JavaBeans (EJB) architecture • Describe the types of EJBs and when they are used • Explain EJB interfaces • Define the steps to deploy an EJB to Oracle Application Server 10g
Enterprise JavaBeans (EJB) • Enterprise JavaBeans are portable components, which: • Enable faster application development • Allow reuse of business components • Encapsulate business logic that can be invoked by clients • Execute in a container that provides services such as support for transactions, persistence, and access control for the beans
When to Use EJBs • When developing a J2EE application, decide whether to use EJBs based on the following requirements: • The applications are complex and would benefit from the system-level services that are provided by an EJB container. • The applications must be portable and scalable. • The applications must be accessed by different types of clients.
Types of EJBs EJB Type Purpose Session Beans Performs a task for a client Represents a business object that exists in a database Entity Beans Receives asynchronous Java Message Service (JMS) messages Message-Driven Beans
Session Beans • Session beans invoke methods for a single client. There are two types of session beans: • Stateless Session Beans (SLSBs) • Conversation that spans a single method call • Single request business processes that do not maintain client-specific state • Stateful Session Beans (SFSBs) • Conversation with one client that may invoke many methods • Business processes that span multiple method requests, thus maintaining state EJB container EJB container Pool of SLSBs SFSBs Client 1 Client 1 Client 2 Client 2
Entity Beans • Entity beans represent a business object in the database. They are: • Sharable across multiple clients • Uniquely identifiable through a primary key • Persistent—the state survives an EJB server crash • There are two types of persistence in entity EJBs: • Container-managed persistence (CMP) beans: • The state of the bean is maintained by the container. • The bean developer specifies the persistent fields. • Bean-managed persistence (BMP) beans: • The state of the bean is maintained by the bean itself. • The bean developer writes the logic to manage persistence by using Java Database Connectivity (JDBC).
Message-Driven Beans • Provide a facility for asynchronous communication • Exist within a pool, and receive and process incoming messages from a JMS queue or topic • Are invoked by the container to handle each incoming message from the queue or topic • Are similar to stateless session beans JMS queue EJB container Clients Pool of MDBs
EJB Architecture EJB server EJB container Home/local home interface Home/local home object EJB Class EJB client Remote/local object Remote/ local interface Database Enterprise Services Naming, Transaction, Security, Messaging Deployment descriptor
EJB Server • Manages the EJB container • Provides a deployment and execution platform for EJB components • Provides system services to containers that in turn provide services to beans: • Transaction services • JNDI naming services • Can provide vendor-specific features such as connection pooling
EJB container Home/local home interface home/local home object EJB class Container generated Client remote/local object Remote/ local interface EJB Container • Manages the life cycle of the enterprise beans • Isolates the enterprise beans from direct access by client applications • Makes required services available to the EJB classes through well-defined interfaces
Services Provided by the EJB Container • Life-cycle management • Bean instance pooling • Client state management • Database connection pooling • Declarative transaction management • Security • Persistence
EJB Client • An EJB client is a stand-alone application, servlet, JSP, or another EJB that accesses the bean. It can be a: • Local client: • Resides within the same Java virtual machine (JVM) as the bean • Passes arguments by reference to the bean • Interacts with the EJB through methods defined in the local interface • Remote client: • Is location independent • Passes arguments by value to the bean • Interacts with the EJB through methods defined in the remote interface
EJB Interfaces and Classes • Interfaces: • Remote interface/Local interface • Home interface/Local home interface • Classes: • Bean class • Primary key class (entity beans)
Remote Interface and Remote Object • Remote interface: • Extends the javax.ejb.EJBObject interface that extends the java.rmi.Remote interface • Describes the client view of an EJB • Declares the business methods that are accessible to remote clients • EJB object: • Is a container-generated implementation of a remote interface • Is a reference object that a client receives • Delegates the method calls to a bean class after doing some infrastructure work • The remote interface and remote object are used by session and entity beans.
Home Interface and Home Object • Home interface: • Extends the javax.ejb.EJBHome interface that extends the java.rmi.Remote interface • Contains the life-cycle methods for creating, removing, and locating the instances of a bean class • Contains home methods • Are accessed by remote clients • Home object: • Is a container-generated implementation of the home interface • Uses callback methods on a bean class to perform its functions
Local Interface and Local Home Interface • Local interface: • Extends the javax.ejb.EJBLocalObject interface • Declares the business methods of the bean that are accessible by a local client • Improves performance because the bean resides in the same JVM, and parameters are passed by reference • Local home interface: • Extends the javax.ejb.EJBLocalHome interface • Defines the life-cycle methods that are accessible by local clients • These interfaces are used by session and entity beans. • They enable relationships between entity beans.
EJB Bean Class • A bean class extends javax.ejb.EnterpriseBean. • A session/entity bean class: • Implements javax.ejb.SessionBean / javax.ejb.EntityBean • Implements business/life-cycle methods • Contains methods to support container callbacks • Contains methods to set and unset the context of the bean • A message-driven bean class: • Implements javax.ejb.MessageDrivenBean • Must implement the MessageListener interface • Contains business logic in the onMessage() method
The EJB Deployment Process Developer’sresponsibility Home interface Remote interface Bean class Other classes Component deployer’s responsibility Deployment descriptor EJB JAR Deployment tools/ commands Jar command/ tool Deployed EJB in the Server JNDI
ejb-jar.xml File <ejb-jar> <enterprise-beans> <session>|<entity>|<message-driven> <description>Say Hello</description> <display-name>HelloWorld</display-name> <ejb-name>HelloWorld</ejb-name> <home>lesson11.HelloWorldHome</home> <remote>lesson11.HelloWorld</remote> <ejb-class>lesson11.impl.HelloWorldBean</ejb-class> </session>|</entity>|</message-driven> </enterprise-beans> <assembly-descriptor> <security-role> </security-role> <method-permission> </method-permission> <container-transaction> </container-transaction> </assembly-descriptor> </ejb-jar>
orion-ejb-jar.xml File • Oracle Application Server 10g uses theorion-ejb-jar.xml file for deployment. This file: • Specifies run-time attributes of the bean for deployment to the container • Enables customization of the run-time behavior of enterprise beans
Adding Methods to the Bean • To add methods to the bean, right-click and select Go To Bean Class:
Summary • In this lesson, you should have learned how to: • Define an EJB • Describe the EJB architecture • Describe the types of EJBs and when they are used • Explain EJB interfaces • Define the steps to deploy an EJB to Oracle Application Server 10g
Practice 11-1: Overview • This practice covers the following topics: • Creating an EJB in JDeveloper • Testing an EJB