Top Banner
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans
28

11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

Mar 27, 2015

Download

Documents

Thomas Norris
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11Copyright © 2005, Oracle. All rights reserved.

Creating the Business Tier: Enterprise JavaBeans

Page 2: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-2 Copyright © 2005, Oracle. All rights reserved.

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

Page 3: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-3 Copyright © 2005, Oracle. All rights reserved.

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

Page 4: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-4 Copyright © 2005, Oracle. All rights reserved.

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.

Page 5: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-5 Copyright © 2005, Oracle. All rights reserved.

Types of EJBs

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

Page 6: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-7 Copyright © 2005, Oracle. All rights reserved.

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

Client 1

Client 2

Pool of SLSBs

EJB container

Client 1

Client 2

SFSBs

Page 7: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-9 Copyright © 2005, Oracle. All rights reserved.

Entity BeansEntity 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).

Page 8: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-10 Copyright © 2005, Oracle. All rights reserved.

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

Clients

EJB container

Pool of MDBs

JMS queue

Page 9: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-11 Copyright © 2005, Oracle. All rights reserved.

EJB Architecture

EJB client

EJB server

Enterprise ServicesNaming, Transaction, Security,

Messaging

Deployment

descriptor

Database

EJB container

Remote/local

object

Home/local home

object

Remote/ local

interface

Home/local home

interfaceEJB

Class

Page 10: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-12 Copyright © 2005, Oracle. All rights reserved.

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

Page 11: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-13 Copyright © 2005, Oracle. All rights reserved.

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

Client

EJB container

EJBclass

home/local home object

Home/local home

interfaceContainer generated

remote/local

object

Remote/ local

interface

Page 12: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-14 Copyright © 2005, Oracle. All rights reserved.

Services Provided by the EJB Container

• Life-cycle management

• Bean instance pooling

• Client state management

• Database connection pooling

• Declarative transaction management

• Security

• Persistence

Page 13: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-16 Copyright © 2005, Oracle. All rights reserved.

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

Page 14: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-17 Copyright © 2005, Oracle. All rights reserved.

EJB Interfaces and Classes

• Interfaces: – Remote interface/Local interface– Home interface/Local home interface

• Classes:– Bean class– Primary key class (entity beans)

Page 15: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-18 Copyright © 2005, Oracle. All rights reserved.

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.

Page 16: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-19 Copyright © 2005, Oracle. All rights reserved.

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

Page 17: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-20 Copyright © 2005, Oracle. All rights reserved.

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.

Page 18: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-21 Copyright © 2005, Oracle. All rights reserved.

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

Page 19: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-22 Copyright © 2005, Oracle. All rights reserved.

The EJB Deployment Process

Jar command/

tool

EJB JAR

JNDI

Component deployer’s responsibility

Developer’sresponsibilityHome interface

Remote interfaceBean class

Other classes

Deploymentdescriptor

Deploymenttools/

commands

Deployed EJB in the Server

Page 20: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-23 Copyright © 2005, Oracle. All rights reserved.

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

ejb-jar.xml File

Page 21: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-24 Copyright © 2005, Oracle. All rights reserved.

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

Page 22: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-25 Copyright © 2005, Oracle. All rights reserved.

Creating an EJB in JDeveloper

Page 23: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-26 Copyright © 2005, Oracle. All rights reserved.

Using the EJB Wizard

Page 24: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-27 Copyright © 2005, Oracle. All rights reserved.

Using the EJB Wizard

Page 25: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-28 Copyright © 2005, Oracle. All rights reserved.

Adding Methods to the Bean

To add methods to the bean, right-click and select Go To Bean Class:

Page 26: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-29 Copyright © 2005, Oracle. All rights reserved.

Deploying to Oracle Application Server 10g from JDeveloper

Page 27: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-30 Copyright © 2005, Oracle. All rights reserved.

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

Page 28: 11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.

11-31 Copyright © 2005, Oracle. All rights reserved.

Practice 11-1: Overview

This practice covers the following topics:

• Creating an EJB in JDeveloper

• Testing an EJB