11 Creating the Business Tier Enterprise Java Beans

  • Slides: 30
Download presentation
11 Creating the Business Tier: Enterprise Java. Beans Copyright © 2004, Oracle. All rights

11 Creating the Business Tier: Enterprise Java. Beans Copyright © 2004, Oracle. All rights reserved.

Objectives After completing this lesson, you should be able to do the following: •

Objectives After completing this lesson, you should be able to do the following: • Define an Enterprise Java. Bean • Describe the Enterprise Java. Beans (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 10 g 11 -2 Copyright © 2004, Oracle. All rights reserved.

Enterprise Java. Beans (EJB) Enterprise Java. Beans are portable components, which: • Enable faster

Enterprise Java. Beans (EJB) Enterprise Java. Beans 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 11 -3 Copyright © 2004, Oracle. All rights reserved.

When to Use EJBs When developing a J 2 EE application, decide whether to

When to Use EJBs When developing a J 2 EE 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. 11 -4 Copyright © 2004, Oracle. All rights reserved.

Types of EJBs 11 -5 EJB Type Purpose Session Beans Performs a task for

Types of EJBs 11 -5 EJB Type Purpose Session Beans Performs a task for a client Entity Beans Represents a business object that exists in a database Message-Driven Beans Receives asynchronous Java Message Service (JMS) messages Copyright © 2004, Oracle. All rights reserved.

Session Beans Session beans invoke methods for a single client. There are two types

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 Pool of SLSBs 11 -7 EJB container SFSBs Client 1 Client 2 Copyright © 2004, Oracle. All rights reserved.

Entity Beans Entity beans represent a business object in the database. They are: •

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). 11 -9 Copyright © 2004, Oracle. All rights reserved.

Message-Driven Beans • • Provide a facility for asynchronous communication Exist within a pool,

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 11 -10 Copyright © 2004, Oracle. All rights reserved.

EJB Architecture EJB server EJB container Home/local home interface Home/ local home object EJB

EJB Architecture EJB server EJB container Home/local home interface Home/ local home object EJB client Remote/ local interface Deployment descriptor 11 -11 EJB Class Remote/ local object Enterprise Services Naming, Transaction, Security, Messaging Copyright © 2004, Oracle. All rights reserved. Database

EJB Server • • • Manages the EJB container Provides a deployment and execution

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 • 11 -12 Can provide vendor-specific features such as connection pooling Copyright © 2004, Oracle. All rights reserved.

EJB Container • • • Manages the life cycle of the enterprise beans Isolates

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 EJB container Home/local home interface home/local home object Remote/ local interface remote/l ocal object Client 11 -13 EJB class Copyright © 2004, Oracle. All rights reserved. Container generated

Services Provided by the EJB Container • • 11 -14 Life-cycle management Bean instance

Services Provided by the EJB Container • • 11 -14 Life-cycle management Bean instance pooling Client state management Database connection pooling Declarative transaction management Security Persistence Copyright © 2004, Oracle. All rights reserved.

EJB Client An EJB client is a stand-alone application, servlet, JSP, or another EJB

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 11 -16 Copyright © 2004, Oracle. All rights reserved.

EJB Interfaces and Classes • Interfaces: – Remote interface/Local interface – Home interface/Local home

EJB Interfaces and Classes • Interfaces: – Remote interface/Local interface – Home interface/Local home interface • Classes: – Bean class – Primary key class (entity beans) 11 -17 Copyright © 2004, Oracle. All rights reserved.

Remote Interface and Remote Object • Remote interface: – Extends the javax. ejb. EJBObject

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 • 11 -18 The remote interface and remote object are used by session and entity beans. Copyright © 2004, Oracle. All rights reserved.

Home Interface and Home Object • Home interface: – Extends the javax. ejb. EJBHome

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 11 -19 Copyright © 2004, Oracle. All rights reserved.

Local Interface and Local Home Interface • Local interface: – Extends the javax. ejb.

Local Interface and Local Home Interface • Local interface: – Extends the javax. ejb. EJBLocal. Object 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. EJBLocal. Home interface – Defines the life-cycle methods that are accessible by local clients • • 11 -20 These interfaces are used by session and entity beans. They enable relationships between entity beans. Copyright © 2004, Oracle. All rights reserved.

EJB Bean Class • A bean class extends javax. ejb. Enterprise. Bean. • A

EJB Bean Class • A bean class extends javax. ejb. Enterprise. Bean. • A session/entity bean class: – Implements javax. ejb. Session. Bean / javax. ejb. Entity. Bean – 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. Message. Driven. Bean – Must implement the Message. Listener interface – Contains business logic in the on. Message() method 11 -21 Copyright © 2004, Oracle. All rights reserved.

The EJB Deployment Process Home interface Remote interface Bean class Other classes Deployment descriptor

The EJB Deployment Process Home interface Remote interface Bean class Other classes Deployment descriptor Developer’s responsibility Component deployer’s responsibility Jar command/ tool EJB JAR Deployment tools/ commands Deployed EJB in the Server JNDI 11 -22 Copyright © 2004, Oracle. All rights reserved.

ejb-jar. xml File <ejb-jar> <enterprise-beans> <session>|<entity>|<message-driven> <description>Say Hello</description> <display-name>Hello. World</display-name> <ejb-name>Hello. World</ejb-name> <home>lesson 11.

ejb-jar. xml File <ejb-jar> <enterprise-beans> <session>|<entity>|<message-driven> <description>Say Hello</description> <display-name>Hello. World</display-name> <ejb-name>Hello. World</ejb-name> <home>lesson 11. Hello. World. Home</home> <remote>lesson 11. Hello. World</remote> <ejb-class>lesson 11. impl. Hello. World. Bean</ejb-class> </session>|</entity>|</message-driven> </enterprise-beans> <assembly-descriptor> <security-role> </security-role> <method-permission> </method-permission> <container-transaction> </assembly-descriptor> </ejb-jar> 11 -23 Copyright © 2004, Oracle. All rights reserved.

orion-ejb-jar. xml File Oracle Application Server 10 g uses the orion-ejbjar. xml file for

orion-ejb-jar. xml File Oracle Application Server 10 g uses the orion-ejbjar. xml file for deployment. This file: • • 11 -24 Specifies run-time attributes of the bean for deployment to the container Enables customization of the run-time behavior of enterprise beans Copyright © 2004, Oracle. All rights reserved.

Creating an EJB in JDeveloper 11 -25 Copyright © 2004, Oracle. All rights reserved.

Creating an EJB in JDeveloper 11 -25 Copyright © 2004, Oracle. All rights reserved.

Using the EJB Wizard 11 -26 Copyright © 2004, Oracle. All rights reserved.

Using the EJB Wizard 11 -26 Copyright © 2004, Oracle. All rights reserved.

Using the EJB Wizard 11 -27 Copyright © 2004, Oracle. All rights reserved.

Using the EJB Wizard 11 -27 Copyright © 2004, Oracle. All rights reserved.

Adding Methods to the Bean To add methods to the bean, right-click and select

Adding Methods to the Bean To add methods to the bean, right-click and select Go To Bean Class: 11 -28 Copyright © 2004, Oracle. All rights reserved.

Deploying to Oracle Application Server 10 g from JDeveloper 11 -29 Copyright © 2004,

Deploying to Oracle Application Server 10 g from JDeveloper 11 -29 Copyright © 2004, Oracle. All rights reserved.

Summary In this lesson, you should have learned how to: • Define an EJB

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 10 g 11 -30 Copyright © 2004, Oracle. All rights reserved.

Practice 11 -1: Overview This practice covers the following topics: • Creating an EJB

Practice 11 -1: Overview This practice covers the following topics: • Creating an EJB in JDeveloper • Testing an EJB 11 -31 Copyright © 2004, Oracle. All rights reserved.

11 -33 Copyright © 2004, Oracle. All rights reserved.

11 -33 Copyright © 2004, Oracle. All rights reserved.

11 -34 Copyright © 2004, Oracle. All rights reserved.

11 -34 Copyright © 2004, Oracle. All rights reserved.