97
Copyright 2002, Cyberdyne Software, Inc. Patterns for EJB Development Bobby Woolf Independent Consultant Cyberdyne Software, Inc. [email protected]

EJB Patterns Slides

Embed Size (px)

Citation preview

Page 1: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 1/97

Copyright 2002, Cyberdyne Software, Inc.

Patterns for EJB Development

Bobby Woolf Independent Consultant

Cyberdyne Software, [email protected]

Page 2: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 2/97

Copyright 2002, Cyberdyne Software, Inc. 2

Contents

• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns• Related Trends in EJB

Page 3: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 3/97

Copyright 2002, Cyberdyne Software, Inc. 3

Contents: Layered Application

Architecture• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns• Related Trends in EJB

Page 4: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 4/97

Copyright 2002, Cyberdyne Software, Inc. 4

Layered Application Architecture

• Early Layered Architectures

• Four Layered Architecture• Service Layer Architecture

• Distributed Architecture• Tiered Architecture

• J2EE Architecture

Page 5: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 5/97

Page 6: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 6/97

Copyright 2002, Cyberdyne Software, Inc. 6

Four Layer Architecture

• Next architecture: Four 

layers – View/Presentation

• Swing, HTML, etc.

 – Application (Controller)• View’s Model

 – Domain (Model)• Business Model

 – Infrastructure/Persistence

• Database, legacy, etc.

Infrastructure

DomainApplication

View

Page 7: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 7/97

Copyright 2002, Cyberdyne Software, Inc. 7

Service Layer Architecture

• Latest architecture:

Five layers• Service Layer 

 – Menu of services

 – Encapsulates use cases – Controls transactions

 – Channels network 

chatter  – Coordinates domain

objects

Infrastructure

Domain

Application

View

Service

Page 8: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 8/97

Copyright 2002, Cyberdyne Software, Inc. 8

Distributed Architecture

• Layers enable distributed

architectures – View

• HTML, JavaScript, web

 – Application• Servlets/JSP

 – Service• Session, message beans

 – Domain, Infrastructure• Entity beans

Infrastructure

Domain

Application

View

Service

Network (IIOP)

Internet (HTTP)

Page 9: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 9/97

Copyright 2002, Cyberdyne Software, Inc. 9

Tiered Architecture

• Layers deploy in tiers

 – Client tier • Web browser or J2SE

 – Web tier • Servlet/JSP/SOAP engine

 – Business tier • EJB/J2EE container 

(application server)

 – EIS tier (not shown)• Enterprise information

systems

Browser Application

Web

EJB

Session

Beans

Message-Driven

Beans

Entity

BeansJDO

HTTP SOAP

EJB JMS

EJB

Page 10: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 10/97

Copyright 2002, Cyberdyne Software, Inc. 10

J2EE Architecture

Page 11: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 11/97

Copyright 2002, Cyberdyne Software, Inc. 11

Layered Application

Architecture: Review• Early Layered Architectures

• Four Layered Architecture• Service Layer Architecture

• Distributed Architecture• Tiered Architecture

• J2EE Architecture

Page 12: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 12/97

Copyright 2002, Cyberdyne Software, Inc. 12

Contents: The Patterns

• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns• Related Trends in EJB

Page 13: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 13/97

Copyright 2002, Cyberdyne Software, Inc. 13

The Patterns

• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Page 14: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 14/97

Copyright 2002, Cyberdyne Software, Inc. 14

Basic EJB Patterns

• Remote Interface

 – What behavior does the bean provide a client? – Example: Item

• Home Interface

 – How do clients access bean instances?

 – Example: ItemHome

• Bean Class – How does a bean implement its interface?

 – Example: ItemBean

Page 15: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 15/97

Copyright 2002, Cyberdyne Software, Inc. 15

The Patterns: Service Bean

• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Page 16: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 16/97

Page 17: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 17/97

Copyright 2002, Cyberdyne Software, Inc. 17

Service Bean: Solution

• Structure the EJB as a Service Bean

 – A set of services the bean can perform – Each service a use case

 – Related use cases provided by a single bean

 – Avoid getters and setters

Page 18: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 18/97

Copyright 2002, Cyberdyne Software, Inc. 18

Service Bean: Consequences

• Client makes a single call across the

network • A single security check is made

• Unit of work is performed in a singletransaction

• Implementation is hidden from the client

 – Implementation can easily be changed

Page 19: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 19/97

Copyright 2002, Cyberdyne Software, Inc. 19

Service Bean: Examples

• AddressBookService

 – public void createPerson(fName, lName, …) – public Person find(fName, lName)

 – public void update(fName, lName, …)

 – public void delete(fName, lName)

• BankService

 – public void transferMoney(amt, acct1, acct2) – public Money getBalance(acct)

Page 20: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 20/97

Copyright 2002, Cyberdyne Software, Inc. 20

Service Bean: AKA

• Also known as: Service Layer 

 – A common set of application operationsavailable to multiple kinds of clients

 – Coordinates the application’s response in each

operation• Also known as: Session Façade

 – Separates presentation and business logic – Business-tier controller 

Page 21: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 21/97

Copyright 2002, Cyberdyne Software, Inc. 21

The Patterns: Services Package

• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Page 22: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 22/97

Copyright 2002, Cyberdyne Software, Inc. 22

Services Package: Problem

• What package should Service Beans go in?

• Forces: – Service beans promote client/server code

 – Put separate layers of code in separate packages

 – Separate client and server packages will helpcreate separate client and server jar files

Page 23: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 23/97

Copyright 2002, Cyberdyne Software, Inc. 23

Services Package: Solution

• Create a Services Package

• Make it a subpackage named “services” – Example: addressbook.services

• Put the service beans in the service package – AddressBook , AddressBookService,AddressBookHome, AddressBookBean

• Next patterns: – Bean Service Interface, Remote Interface,

Home Interface, and Bean Class

Page 24: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 24/97

Copyright 2002, Cyberdyne Software, Inc. 24

Services Package: Example

• com.myco.myproj.addressbook.services

 – AddressBook  – AddressBookService

 – AddressBookHome

 – AddressBookBean

• Javadoc: Services package shows the

services available to the client – Just browse addressbook.services

Page 25: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 25/97

Page 26: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 26/97

Copyright 2002, Cyberdyne Software, Inc. 26

Bean Service Interface: Problem

• How can the compiler ensure that the

Remote Interface and Bean Class areconsistent?

• Forces:

 – Class must implement interface’s methods

 – Class can not implement EJBObject methods

 – Class does not implement the Remote Interface – Compiler needs relationship between class and

interface

Page 27: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 27/97

Copyright 2002, Cyberdyne Software, Inc. 27

Bean Service Interface: Solution

• Declare the remote bean messages in a Bean

Service Interface – Remote Interface will extend this and

EJBObject

 – Bean Class will implement this interface• As well as SessionBean or EntityBean

 – Compiler will ensure consistency

Page 28: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 28/97

Page 29: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 29/97

Copyright 2002, Cyberdyne Software, Inc. 29

Bean Service Interface: AKA

• Also known as:

 – Implementing a Common Interface – Business Interface

• Next Patterns:

 – Bean Service Interface ia a RemotifiedInterface

 – Bean Service Interface messages throw aService Failed Exception

The Patterns: Remotified

Page 30: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 30/97

Copyright 2002, Cyberdyne Software, Inc. 30

The Patterns: Remotified

Interface• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Page 31: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 31/97

Page 32: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 32/97

Copyright 2002, Cyberdyne Software, Inc. 32

Remotified Interface: Solution

• Implement interface as a Remotified 

 Interface – Each method throws, at least, RemoteException

• Implement a business interface as usual

 – Add RemoteException to each methodsignature

• No need to extend Remote – The Remote Interface already does

Remotified Interface:

Page 33: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 33/97

Copyright 2002, Cyberdyne Software, Inc. 33

Remotified Interface:

Consequences• Remotified Interface is not quite like a

 business interface – Must throw RemoteException, a checked

exception

 – Clients must catch and handle Remote-Exception, even when client is not remote

• Only remotify when required

 – RMI, CORBA, and EJB require it

Page 34: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 34/97

Copyright 2002, Cyberdyne Software, Inc. 34

Remotified Interface: Example

• AddressBookService

 – createPerson(…) throws RemoteException – find(…) throws RemoteException

 – update(…) throws RemoteException

 – delete(…) throws RemoteException

The Patterns: Unremotified Bean

Page 35: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 35/97

Copyright 2002, Cyberdyne Software, Inc. 35

The Patterns: Unremotified Bean

Class• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Unremotified Bean Class:

Page 36: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 36/97

Copyright 2002, Cyberdyne Software, Inc. 36

Unremotified Bean Class:

Problem• Should bean class methods be remotified?

• Forces: – Class can throw fewer exceptions than interface

 – Only exceptions this implementation can throw

 – Bean Class should not throw RemoteException

• Only containers throw RemoteException

 – Don’t use RemoteException for server logic problems

• Only for communications problems

Page 37: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 37/97

Unremotified Bean Class:

Page 38: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 38/97

Copyright 2002, Cyberdyne Software, Inc. 38

Unremotified Bean Class:

Example• AddressBookBean

 – createPerson(…) throws ServiceFailedException, RemoteException

 – find(…) throws  ServiceFailedException,

RemoteException – update(…) throws  ServiceFailedException,

RemoteException

 – delete(…) throws  ServiceFailedException,RemoteException

The Patterns: Service Failed

Page 39: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 39/97

Copyright 2002, Cyberdyne Software, Inc. 39

The Patterns: Service Failed

Exception• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Page 40: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 40/97

Page 41: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 41/97

Service Failed Exception: Single

Page 42: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 42/97

Copyright 2002, Cyberdyne Software, Inc. 42

p g

Exception Example• Don’t do this:

 – AddressBookBean• createPerson(…) throws IOException,

SQLException, SAXException,ClassNotFoundException,CloneNotSupportedException

• Instead, do this:

 – AddressBookBean• createPerson(…) throws CreatePersonException

Service Failed Exception: Service

Page 43: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 43/97

Copyright 2002, Cyberdyne Software, Inc. 43

p

Bean Example• AddressBookService

 – createPerson(…) throwsCreatePersonException, RemoteException

 – find(…) throws FindPersonException,

RemoteException – update(…) throws UpdatePersonException,

RemoteException

 – delete(…) throws DeletePersonException,RemoteException

Service Failed Exception: Bean

Page 44: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 44/97

Copyright 2002, Cyberdyne Software, Inc. 44

p

Class Example• AddressBookBean

 – createPerson(…) throwsCreatePersonException

 – find(…) throws FindPersonException

 – update(…) throws UpdatePersonException – delete(…) throws DeletePersonException

Page 45: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 45/97

Service Failed Exception:

Page 46: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 46/97

Copyright 2002, Cyberdyne Software, Inc. 46

p

Wrapped Exception Example• AddressBookBean

createPerson(...) throws CreatePersonException {try {

}catch (Exception ex) {

throw new CreatePersonException(ex);

}}

Patterns in EJB Home

Page 47: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 47/97

Copyright 2002, Cyberdyne Software, Inc. 47

Patterns in EJB Home

• EJB Home

 – Service Bean for managing beans – EJB Home Interface is a Remotified Interface

• Each method throws RemoteException

 – EJB Home Interface throws Service FailedExceptions

• create throws CreateException

• find throws FinderException• remove throws RemoveException

Page 48: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 48/97

Application Package: Problem

Page 49: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 49/97

Copyright 2002, Cyberdyne Software, Inc. 49

Application Package: Problem

• What package do Service Bean clients go

in?• Forces:

 – Same package as the beans they use?

 – Put separate layers of code in separate packages

 – Client code should be separate from server code

 – Separate client and server packages will helpcreate separate client and server jar files

Application Package: Solution

Page 50: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 50/97

Copyright 2002, Cyberdyne Software, Inc. 50

Application Package: Solution

• Create an Application Package

• Make it a subpackage named “application” – Example: addressbook.application

• Put the service beans in the service package

 – AddressBook , AddressBookEjbClient

• Next patterns:

 – Application Service Interface• One per Service Bean

Application Package: Example

Page 51: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 51/97

Copyright 2002, Cyberdyne Software, Inc. 51

Application Package: Example

• com.myco.myproj.addressbook.application

 – AddressBook  – AddressBookEjbClient

• Javadoc: Application package shows the

services available on the client – Just browse addressbook.application

 – Client-side services that are alreadyimplemented

The Patterns: Application Service

Page 52: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 52/97

Copyright 2002, Cyberdyne Software, Inc. 52

Interface• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Application Service Interface:

Page 53: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 53/97

Copyright 2002, Cyberdyne Software, Inc. 53

Problem• How should clients access a service bean?

• Forces: – Simple way to access a Service Bean’s services

 – An EJB client has to know about EJB

• Use Remote Interface

• Handle RemoteExceptions

 – Encapsulate/hide details of EJB, remoteness

Application Service Interface:

Page 54: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 54/97

Copyright 2002, Cyberdyne Software, Inc. 54

Solution• Represent the service on the client as an

Application Service Interface• Implementors encapsulate EJB details

 – Hide whether EJB is even being used

 – Hide home lookup, bean access

 – Hide remoteness issues, RemoteException

 – Allow for alternative implementations• Stand-alone, EJB, SOAP/web services, etc.

Application Service Interface:

Page 55: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 55/97

Copyright 2002, Cyberdyne Software, Inc. 55

Consequences• Application Service Interface is very much

like Bean Service Interface – Same methods

 – App interface doesn’t throw RemoteException

 – Does throw Service Failed Exceptions

• Application Service Interface is

unremotified – Like Unremotified Bean Class

Page 56: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 56/97

Service Interfaces Comparison

Page 57: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 57/97

Copyright 2002, Cyberdyne Software, Inc. 57

Service Interfaces Comparison

• Bean Service Interface

 – addressboook.services.AddressBookService• createPerson(…) throws CreatePersonException,

RemoteException

• find(…) throws FindPersonException,RemoteException

• Application Service Interface

 – addressbook.application.AddressBook • createPerson(…) throws CreatePersonException

• find(…) throws FindPersonException

The Patterns: Review

Page 58: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 58/97

Copyright 2002, Cyberdyne Software, Inc. 58

The Patterns: Review

• Service Bean

• Services Package• Bean Service Interface

• Remotified Interface

• Unremotified Bean Class

• Service Failed Exception

• Application Package

• Application Service Interface

Contents: Cookbook

Page 59: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 59/97

Copyright 2002, Cyberdyne Software, Inc. 59

Contents: Cookbook 

• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns

• Related Trends in EJB

Cookbook 

Page 60: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 60/97

Copyright 2002, Cyberdyne Software, Inc. 60

• Create Exceptions

• Define Application Service Interface• Define Bean Service Interface

• Define Remote Interface

• Implement Bean Class

• Implement Application Service Interface

Cookbook: Exceptions

Page 61: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 61/97

Copyright 2002, Cyberdyne Software, Inc. 61

p

• Create Exceptions first

 – Service Failed Exceptions public class AddressBookExceptionextends WrappedException {// constructors

}

 public class CreatePersonExceptionextends AddressBookException {

// constructors}

Cookbook: Application Service

Page 62: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 62/97

Copyright 2002, Cyberdyne Software, Inc. 62

Interface• Goes in Application Package

• Each method: Service Failed Exception – No RemoteException

 package addressbook.application;

import addressbook.services.CreatePersonException; public interface AddressBook {

 public void createPerson

(String firstName, String lastName)throws CreatePersonException;

}

Cookbook: Bean Service

Page 63: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 63/97

Copyright 2002, Cyberdyne Software, Inc. 63

Interface• Goes in Services Package

 – Name: App. Service Interface + “Service” – Service Bean, Remotified Interface

 package addressbook.services;

import java.rmi.RemoteException; public interface AddressBookService { public void createPerson

(String firstName, String lastName)

throws CreatePersonException,RemoteException;

}

Page 64: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 64/97

Cookbook: Bean Class

Page 65: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 65/97

Copyright 2002, Cyberdyne Software, Inc. 65

• Goes in Services Package

• Implements – Bean Service Interface

 – Appropriate extension of EnterpriseBean

• Unremotified Bean Class

 – No RemoteExceptions

Cookbook: Bean Class

Page 66: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 66/97

Copyright 2002, Cyberdyne Software, Inc. 66

 package addressbook.services;import javax.ejb.SessionBean;

 public class AddressBookBean implementsAddressBookService, SessionBean { public void createPerson

(String firstName, String lastName)

throws CreatePersonException {// code to create the Person in the// Address Book 

// any exceptions are wrapped as// CreatePersonExceptions}

}

Cookbook: Application Service

Page 67: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 67/97

Copyright 2002, Cyberdyne Software, Inc. 67

Implementor • Implement the Application Service

Interface

 – Implement as an EJB client

• Goes in Application Package

 package addressbook.application;import addressbook.services.AddressBook;import addressbook.services.AddressBookHome;import addressbook.services.CreatePersonException

 public class AddressBookEjbClient implementsaddressbook.application.AddressBook 

Cookbook: Application Service

Page 68: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 68/97

Copyright 2002, Cyberdyne Software, Inc. 68

Implementor  public void createPerson

(String firstName, String lastName)

throws CreatePersonException {try {

AddressBook book = getService();book.createPerson (firstName, lastName);

book.remove();}catch (Exception ex) {

throw new CreatePersonException(ex);}

}

Cookbook: Application Service

Page 69: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 69/97

Copyright 2002, Cyberdyne Software, Inc. 69

Implementor  protected AddressBook getService()

throws <Exceptions> {

return getServiceHome.create();}

 protected AddressBookHome getServiceHome()throws <Exceptions> {

// code to obtain the home}

Cookbook: Review

Page 70: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 70/97

Copyright 2002, Cyberdyne Software, Inc. 70

• Create Exceptions

• Define Application Service Interface• Define Bean Service Interface

• Define Remote Interface

• Implement Bean Class

• Implement Application Service Interface

Contents: Bonus Patterns

Page 71: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 71/97

Copyright 2002, Cyberdyne Software, Inc. 71

• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns

• Related Trends in EJB

Bonus Patterns

Page 72: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 72/97

Copyright 2002, Cyberdyne Software, Inc. 72

• Data Transfer Object

• Identity Object• Data Transfer Object Assembler 

• Bonus Strategy

 – Stateful vs. Stateless Session Beans

Bonus Patterns: Data Transfer 

Page 73: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 73/97

Copyright 2002, Cyberdyne Software, Inc. 73

Object• Data Transfer Object

• Identity Object• Data Transfer Object Assembler 

• Bonus Strategy:

 – Stateful vs. Stateless Session Beans

Data Transfer Object: Problem

Page 74: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 74/97

Copyright 2002, Cyberdyne Software, Inc. 74

• Also known as

 – Value Object• How can client get bulk data from server 

without multiple network calls?

• Forces: – Multiple network calls hurt performance

 – Multiple parameters awkward; multiple returnvalues impossible

 – Entity beans cannot be copied to the client

Data Transfer Object: Solution

Page 75: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 75/97

Copyright 2002, Cyberdyne Software, Inc. 75

• Transfer bulk data between the client andserver using a Data Transfer Object 

 – Plain Java object

 – Dumb data object

 – Often aggregates data from multiple server objects

• Custom designed for a particular client

 – Don’t overload with unnecessary state

• Must be serializable

Data Transfer Object: Example

Page 76: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 76/97

Copyright 2002, Cyberdyne Software, Inc. 76

• Person

 – instance variables

• firstName, lastName, street, city, state, zip

 – getters and setters

• Scenario – Client requests Person

 – Server creates Person, sends to client

 – Client views, changes data, sends back to server 

 – Server gets changes and commits them

Bonus Patterns: Identity Object

Page 77: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 77/97

Copyright 2002, Cyberdyne Software, Inc. 77

• Data Transfer Object

• Identity Object• Data Transfer Object Assembler 

• Bonus Strategy:

 – Stateful vs. Stateless Session Beans

Identity Object: Problem

Page 78: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 78/97

Copyright 2002, Cyberdyne Software, Inc. 78

• How can client choose from many domainobjects?

• Forces:

 – Domain objects cannot be copied to client

 – Most choices will never be used – Server must know which choice was chosen

 – Minimize bandwidth, client memoryconsumption

Identity Object: Solution

Page 79: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 79/97

Copyright 2002, Cyberdyne Software, Inc. 79

• Use an Identity Object to specify an objecton the server 

• Very lightweight

 – A name/identifier the user recognizes

 – A UID/primary key/foreign key the server recognizes

• Must be serializable

• Specialization of Data Transfer Object

Identity Object: Example

Page 80: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 80/97

Copyright 2002, Cyberdyne Software, Inc. 80

• PersonIdentity

 – instance variables

• name, personPrimaryKey

 – getters and setters

• Scenario – Client requests list of Persons

 – Server creates PersonIdentities, sends to client

 – Client views list, sends selection back to server 

 – Server gets selection, returns DTO

Bonus Patterns: Data Transfer 

Page 81: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 81/97

Copyright 2002, Cyberdyne Software, Inc. 81

Object Assembler • Data Transfer Object

• Identity Object• Data Transfer Object Assembler

• Bonus Strategy:

 – Stateful vs. Stateless Session Beans

Data Transfer Object Assembler 

Page 82: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 82/97

Copyright 2002, Cyberdyne Software, Inc. 82

• Problem

 – How to create Data Transfer Objects, convertIdentity Objects into DTOs, etc.?

• Solution

 – Use a Data Transfer Object Assembler • Also known as

 – Value Object Assembler 

Bonus Patterns: Stateful vs.

Page 83: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 83/97

Copyright 2002, Cyberdyne Software, Inc. 83

Stateless• Data Transfer Object

• Identity Object• Data Transfer Object Assembler 

• Bonus Strategy:

 – Stateful vs. Stateless Session Beans

Stateful vs. Stateless: Problem

Page 84: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 84/97

Copyright 2002, Cyberdyne Software, Inc. 84

• Problem:

 – Should session beans be stateful or stateless?

 – If stateless, where does the state go?

• Problem also applies to:

 – Servlets• Servlets must be stateless

 – SOAP/Web Services

• Services must be stateless

Stateful vs. Stateless:

Considerations

Page 85: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 85/97

Copyright 2002, Cyberdyne Software, Inc. 85

• Issue:

 – When client invokes bean multiple times, clientassumes bean remembers state from last time

• Stateful Session Beans

 – Stateful session beans make this easier  – Bean per client; each client must get its bean

 – Beans must be passivated and activated

 – Beans aren’t persistent

 – When can they be garbage collected?

Stateful vs. Stateless:

Considerations

Page 86: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 86/97

Copyright 2002, Cyberdyne Software, Inc. 86

• Stateless Session Beans

 – Better performance, scalability than statefulsession beans

 – State must be stored in client, such as

HttpSessionState• Keep state small and serializable

• Make HTTP client identify its HttpSessionState:Cookie, URL rewriting

 – Still not persistent

• Use persistent HTTP session support (via JDBC)

Bonus Patterns: Review

Page 87: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 87/97

Copyright 2002, Cyberdyne Software, Inc. 87

• Data Transfer Object

• Identity Object• Data Transfer Object Assembler 

• Bonus Strategy:

 – Stateful vs. Stateless Session Beans

Contents: Related Trends in EJB

Page 88: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 88/97

Copyright 2002, Cyberdyne Software, Inc. 88

• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns

• Related Trends in EJB

Related Trends in EJB

Page 89: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 89/97

Copyright 2002, Cyberdyne Software, Inc. 89

• EJB 2.0

 – Local Interfaces

• EJB 2.1

 – Web Service Endpoint Interfaces

Local Interface

Page 90: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 90/97

Copyright 2002, Cyberdyne Software, Inc. 90

• Local Interface

 – EJB client in same JVM as EJB bean

 – Parameters passed by reference, not value

 – Parameters are not serialized

 – Methods don’t throw RemoteException

Local Interface: Advice

Page 91: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 91/97

Copyright 2002, Cyberdyne Software, Inc. 91

• Consequences

 – Not a Remotified Interface

 – Bean Service Interface need not be remotified

• Advice

 – Entity beans should have local interfaces only – Access entity beans remotely through session

 beans (i.e., Service Beans)

Web Service Endpoint Interface

Page 92: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 92/97

Copyright 2002, Cyberdyne Software, Inc. 92

• Web Service Endpoint Interface

 – Only applies to stateless session beans

 – Makes the bean a JAX-RPC service endpointinterface

 – Remote interface• Methods must declare to throw RemoteException

 – Parameters and return types must be

• JAX-RPC types• Serializable

Web Service Endpoint Interface:

Advice

Page 93: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 93/97

Copyright 2002, Cyberdyne Software, Inc. 93

• Consequences

 – A Remotified Interface

 – Bean Service Interface must be remotified

• Advice

 – See stateful vs. stateless strategy – Service Bean concept still applies to web

services

 – Restrictions on parameter and return types

Related Trends in EJB: Review

Page 94: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 94/97

Copyright 2002, Cyberdyne Software, Inc. 94

• EJB 2.0

 – Local Interfaces

• EJB 2.1

 – Web Service Endpoint Interfaces

Contents: Review

Page 95: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 95/97

Copyright 2002, Cyberdyne Software, Inc. 95

• Layered Application Architecture

• The Patterns• Cookbook 

• Bonus Patterns

• Related Trends in EJB

Page 96: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 96/97

Questions

Page 97: EJB Patterns Slides

8/8/2019 EJB Patterns Slides

http://slidepdf.com/reader/full/ejb-patterns-slides 97/97

Copyright 2002, Cyberdyne Software, Inc. 97