course #3036 enterprise java beans patrick w. mcmichael pillar technology group, llc

69
Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Upload: derrick-morton

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Course #3036Enterprise Java Beans

Patrick W. McMichael

Pillar Technology Group, LLC

Page 2: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

• Who are you? Where are you from? What is your role?

• Why are you here at this tutorial?

• What business challenges are you hoping J2EE can help with?

• Who are you? Where are you from? What is your role?

• Why are you here at this tutorial?

• What business challenges are you hoping J2EE can help with?

Your Expectations

Page 3: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

My Expectations

NO head-nodding -- if in doubt, ASK!

DON’T be shy! I’m going to need your help this afternoon!

Think about how the concepts presented might be applied in your domain.

Page 4: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

AgendaThe role of the containerBean BasicsA Closer Look…

– Entity Beans– Session Beans– Message Driven Beans

Bean Info You Shouldn’t Leave Home Without– JNDI– JNDI ENC– Transactions– Exceptions

Coming Soon to a Container Near You! (EJB 2.1/3.0)

Page 5: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

eFlix Online Rental System

Page 6: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

eFlix Online Rental System

Page 7: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

7 Primary Services (Monson-Haefel)

• Concurrent access to system resources

• Transaction management

• Persistence

• Object distribution / Location transparency

• Naming / Object binding

• Security

• Asynchronous messaging

Page 8: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

How have your apps depended on these services?

How have your apps depended on these services?

Tell Me About It!

Page 9: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Approach #1 -- The MACHO Way

with vi

by candlelight

on the last ounce of laptop battery juice

barefoot

using a dial-up connection

“I coded my app…”“I coded my app…”

Page 10: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Approach #2: The time-to-market way

Page 11: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

How do you configure / manage these services?

Programmatically

Declaratively

UUM Example– intranet deployment– extranet deployment

Page 12: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Questions

What’s the point of using a J2EE container? What does it buy you?

What’s the advantage of declarative configuration of an app’s behavior vs. programmatic configuration?

Page 13: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

EXAMPLE:

The Rental Manager EJB

EXAMPLE:

The Rental Manager EJB

Bean Basics

Page 14: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Break That Bean Down!

RentalManagerHome

RentalManager

RentalManagerBean

Developer Written Code vs. Generated Code

javac vs. EJB Compiler

Page 15: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

WHY All These Classes?

Remember the primary services?

Example: “Going on a Date

Page 16: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Client Calls to Session/Entity Beans

Client

Home Stub

Remote / LocalHome Interface

Object Stub

Remote / Local Interface

EJB Server

EJB Container

Remote / LocalHome Interface

Remote / Local Interface

EJB Home

EJB ObjectBean Instance

Page 17: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

The Special Case of the MDB

Hey You!

Page 18: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Remote vs. Local Interfaces

The expense of RMI, Serialization, etc.

Proprietary Optimizations

Planned Localization

Page 19: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Remote vs. Local Interfaces --Delegated Calls

Page 20: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Remote vs. Local Interfaces --Session Façade w/ Entity Beans

Page 21: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

QuestionsWhich parts of an EJB does a developer have to

create?

What’s the point of the “egg yolk/eyeball” diagram?

What’s unique about MDB’s?

What’s the difference between remote and local interfaces?

When would you use one, the other, or both?

Page 22: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity BeansEntity Beans

Bean Types

Page 23: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity BeansEncapsulate key NOUN entities in the problem domain

eFlix Entity Bean Candidates

Persistence -- CMP vs. BMP

CMR -- introduced in EJB 2.0

EJB QL -- also brought in by the 2.0 spec

* (shameless plug for 3138…)

Page 24: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity Bean ExampleTitleHome -- Local Home Interface

– create methods

– find methods

– remove

Title -- Local “Business” Interface– getters

– setters

TitleBean -- Bean Implementation Class– ejbCreate counterparts to Home Interface

– lifecycle/callback method implementation

– getters/setters and the abstract persistence schema

Page 25: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity Bean CMR Example

Page 26: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity Bean Example -- EJB QL

EJB 2.0’s EJB QL provides a non-proprietary, declarative

way of mapping custom finders (i.e. not by primary key) to

their implementation.

findUnitsForTitleByStatus method on TitleUnitHome

– SELECT OBJECT(u) FROM TitleUnit AS u WHERE u.title.productNumber = ?1 AND u.status = ?2

Page 27: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity Bean Example -- EJB QL

SELECT OBJECT(u) FROM TitleUnit AS u WHERE

u.title.productNumber = ?1 AND u.status = ?2

– “TitleUnit” -- Abstract Schema Name

– u -- AS clause, object usage, references

– u.status -- drill down to field value

– u.title.productNumber -- carry across CMR objects

Page 28: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

The Entity Bean Lifecycle

Page 29: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Entity Beans vs. Straight JDBCSimultaneous access to large volumes of entities -- POOL

EXAMPLE -- NEED MORE OR LESS WILLING VOLUNTEERS!

Simultaneous access to same data at the same time by multiple clients (potential blocking issues)

Optimistic Concurrency

DB Schema -- stable or still in flux?

Session Façade Pattern -- clients shouldn’t know or care if entity beans JDBC, JDO, Hibernate, etc. was used.

Page 30: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Session BeansSession Beans

Bean Types

Page 31: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Session BeansHandle things like...

– business logic– workflow

Come in two varieties– Stateless

– Stateful

Page 32: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Session Beans

Page 33: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Stateless Session Bean Lifecycle

Page 34: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Stateful Session Bean Lifecycle

Page 35: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Stateless Session Bean Advantages

NO Activation/Passivation

Instance Swapping between clients– fewer beans can service more clients!

Simple, Efficient Failover in a clustered environment

Page 36: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Session Bean ExampleTitleManager Home Interface

– create method

TitleManager Business Interface– business methods

TitleManager Bean Class

Session Façade Pattern– locateAvailableUnit method in façade -- more specific to a

business service– findUnitsForTitleByStatus method in Entity Bean Home (uses

EJB QL) -- more generalized

Page 37: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Message Driven BeansMessage Driven Beans

Bean Types

Page 38: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

“First, a little background...”“First, a little background...”

Messaging Basics

Page 39: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

JMS Messaging in Java --Point-to-Point

Page 40: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

JMS Messaging in Java --Pub-Sub

Page 41: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Before MDBs...Session and Entity Beans were NOT designed to handle asynchronous processing of JMS messages.

What about synchronous processing?

– What triggers the processing? A client call?

– What type of receiving would be done?

• Endless blocking?

• Timed blocking?

• Non-blocking?

There simply were NO good options before MDBs!

Page 42: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

MDB Message ProcessingAsynchronous -- senders/publishers of messages are decoupled from the processing of the messages.

Concurrent -- container-provided, multithreaded, concurrent message consumption

Automated -- No client trigger calls are needed because...

Page 43: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Client Calls to MDBs

The CONTAINER

IS

the client!

Page 44: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

The MDB Lifecycle

Page 45: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Renting Out Titles --

RentalManagerBean

Renting Out Titles --

RentalManagerBean

Example 1a -- Publishing to a Topic

Page 46: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Renting Out Titles -- CustomerRentalWorkOrderMDB

Renting Out Titles -- CustomerRentalWorkOrderMDB

Example 1b --An Example Subscriber

Page 47: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

onMessage -- CustomerRentalWorkOrderMDB &

CustomerRentalNotificationMDB

onMessage -- CustomerRentalWorkOrderMDB &

CustomerRentalNotificationMDB

Example 1c --Run It!

Page 48: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

MDB Message ProcessingImplements MessageDrivenBeanImplements MessageListener

– EJB 2.1 allows other options (I.e. JAXM)

– This is why EJB 2.0 did NOT have MessageDrivenBean extend MessageListener

onMessage method (JMS)

The bean pool and MDBs

ejb-jar.xml

ejb-borland.xml

jndi-definitions.xml

Page 49: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

QuestionsWhat are the 3 bean types?

Which was new to EJB 2.0? Why was it added?

What are the two types of session beans?

What are the peformance differences b/t stateful and stateless and why?

What’s an alternative to a Stateful session bean in a web application?

Page 50: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Bean Info You Shouldn’t Leave Home Without

Bean Info You Shouldn’t Leave Home Without

Does your head hurt yet?

Page 51: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

JNDI Lookups of EJBsJava Naming and Directory Interface

– Object Binding– LDAP

Remote EJB Lookup Example -- RUN IT!– initialize method in TestRentalManager– InitialContext object– properties (parms vs. jndi.properties)– lookup(<JNDI Name>)– narrowing remote references (the joy of RMI)

Page 52: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

JNDI ENC & Bean Resource Dependencies

Page 53: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

JNDI ENCJNDI Environment Naming Context

– each EJB’s own “private Idaho” namespace

– relative to “java:comp/env”

– subcontexts recommended by spec (ejb, jdbc, jms, mail, url)

Example of JNDI ENC ref’s in RentalManager– local ejb ref’s (TitleManager, Payment Manager)

– DataSource for connections used in DAO class

– JMSConnectionFactory objects and Destinations

– Differences in lookups (JNDI vs. JNDI ENC, remote vs. local)

Page 54: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Transactions -- Title Rental Example

Process each title the customer wants to rent– find an available unit for the title– reserve the unit– determine the rental fee and factor into the running total

Create a rental record for each title that was reserved

Charge the customer for the rentals

Publish a JMS message to the Topic for further (asynchronous) processing

Page 55: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Transactions -- Title Rental Example

ACID Properties– Atomic unit of work

– Consistent state (DB, JMS Destinations)

– Isolation of work from other system activity

– Durable end result

Page 56: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Transactions -- Title Rental Example

Possible Failure points in workflow– 2/3 titles reserved, third is unavailable

– 3 titles reserved, rental records created, but charge to customer’s credit card fails

– All work up to the JMS publish works, but the message never hits the Topic

• no notification to customer• no work order to get product out the door

Page 57: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Transactions -- Declarative CMT

NotSupported -- “We don’t do that here.”

Supports -- “If you got one, cool, otherwise, who cares!”

Required -- “I need one, and if you don’t provide one, I’ll take care of business myself.”

RequiresNew -- “I don’t care if you have one or not. I want

my own!”

Mandatory -- “I want one, and you better give it to me!”Never -- “I don’t want one, and don’t even think of calling me

with one!”

Page 58: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

rentOutTitlesrentOutTitles

Example 2 --Run It!

Page 59: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Exceptions -- Application Exceptions

Do NOT extend java.lang.RuntimeException– or subclasses like EJBException

Do NOT extend java.rmi.RemoteException or its subclasses

Do NOT automatically trigger a rollback by the container

A rollback can be initiated in code (setRollbackOnly -- ex. charging customer)

Page 60: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Exceptions -- System Exceptions

Any RuntimeException(s)

java.rmi.RemoteException or its subclasses

DO automatically trigger a rollback by the container

Exceptions logged by container

EJB instance is discarded– May be more or less transparent to the user– Depends on state (or lack thereof) of the bean– Depends on vendor provisions (i.e. BES stubs make things

VERY transparent in a clustered environment)

Page 61: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Exceptions -- RentalManager EJB

Application Exception -- rollback only set, exception rethrown

SQLException (System) converted to EJBException and thrown

Page 62: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

“Coming Soon to a Container Near You!”“Coming Soon to a Container Near You!”

EJB 2.1 / 3.0

Page 63: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

What’s New in EJB 2.1Better web services support

– web services endpoints for SLSBs -- easy access for external clients

– EJBs can tap into external web services more easily– ex. .NET and J2EE interoperability

Container-managed timer service via javax.ejb.TimedObject interface and the ejbTimeout method– Polling for events to trigger business logic– CRON or Autosys job replacement potential

Page 64: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

What’s New in EJB 2.1

EJB QL enhancements– ORDER BY (ascending or descending)– AVG, MAX, MIN, SUM, COUNT, MOD

The door has been opened for MDBs to use messaging protocols other than JMS (i.e. JAXM)

Page 65: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

What’s New in EJB 3.0http://www.jcp.org/en/jsr/detail?id=220

“It’s all about the beans, baby!”– Bye, bye interfaces– Hello annotations

Example:@Session public class MyEJBThreeOBean { public void youCanDoIt(boolean really) { ... } }

Page 66: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

What’s New in EJB 3.0http://www.martinfowler.com/articles/injection.html

Dependency Injection vs. JNDI lookups

Example:@Session public class MyEJBThreeOBean {

private DataSource myDS;

@Inject private void setMyDS(DataSource theDS) { myDS = theDS; } public void methodCallingMyDAO() { ... Connection c = myDS.getConnection(); ... } }

Page 67: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

If there’s time...

Deployment Issues (JARs, EARs, etc.)

JUnit w/ EJBs

Debugging Example

Profiling Example

Page 68: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Questions?

Page 69: Course #3036 Enterprise Java Beans Patrick W. McMichael Pillar Technology Group, LLC

Thank You

3036Enterprise Java Beans

Please fill out the speaker evaluation

You can contact me further at …[email protected]