Title: Course
1Course 3036Enterprise Java Beans
- Patrick W. McMichael
- Pillar Technology Group, LLC
2Your Expectations
- 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?
3My Expectations
- NO head-nodding -- if in doubt, ASK!
- DONT be shy! Im going to need your help this
afternoon! - Think about how the concepts presented might be
applied in your domain.
4Agenda
- The role of the container
- Bean Basics
- A Closer Look
- Entity Beans
- Session Beans
- Message Driven Beans
- Bean Info You Shouldnt Leave Home Without
- JNDI
- JNDI ENC
- Transactions
- Exceptions
- Coming Soon to a Container Near You! (EJB 2.1/3.0)
5eFlix Online Rental System
6eFlix Online Rental System
77 Primary Services (Monson-Haefel)
- Concurrent access to system resources
- Transaction management
- Persistence
- Object distribution / Location transparency
- Naming / Object binding
- Security
- Asynchronous messaging
8Tell Me About It!
How have your apps depended on these services?
9Approach 1 -- The MACHO Way
I coded my app
- with vi
- by candlelight
- on the last ounce of laptop battery juice
- barefoot
- using a dial-up connection
10Approach 2 The time-to-market way
11How do you configure / manage these services?
- Programmatically
- Declaratively
- UUM Example
- intranet deployment
- extranet deployment
12Questions
- Whats the point of using a J2EE container? What
does it buy you? - Whats the advantage of declarative configuration
of an apps behavior vs. programmatic
configuration?
13Bean Basics
EXAMPLE The Rental Manager EJB
14Break That Bean Down!
- RentalManagerHome
- RentalManager
- RentalManagerBean
- Developer Written Code vs. Generated Code
- javac vs. EJB Compiler
15WHY All These Classes?
- Remember the primary services?
- Example Going on a Date
16Client Calls to Session/Entity Beans
17The Special Case of the MDB
Hey You!
18Remote vs. Local Interfaces
- The expense of RMI, Serialization, etc.
- Proprietary Optimizations
- Planned Localization
19Remote vs. Local Interfaces --Delegated Calls
20Remote vs. Local Interfaces --Session Façade w/
Entity Beans
21Questions
- Which parts of an EJB does a developer have to
create? - Whats the point of the egg yolk/eyeball
diagram? - Whats unique about MDBs?
- Whats the difference between remote and local
interfaces? - When would you use one, the other, or both?
22Bean Types
Entity Beans
23Entity Beans
- Encapsulate 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)
24Entity Bean Example
- TitleHome -- 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
25Entity Bean CMR Example
26Entity Bean Example -- EJB QL
- EJB 2.0s 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
27Entity 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
28The Entity Bean Lifecycle
29Entity Beans vs. Straight JDBC
- Simultaneous 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 shouldnt know
or care if entity beans JDBC, JDO, Hibernate,
etc. was used.
30Bean Types
Session Beans
31Session Beans
- Handle things like...
- business logic
- workflow
- Come in two varieties
- Stateless
- Stateful
32Session Beans
33Stateless Session Bean Lifecycle
34Stateful Session Bean Lifecycle
35Stateless Session Bean Advantages
- NO Activation/Passivation
- Instance Swapping between clients
- fewer beans can service more clients!
- Simple, Efficient Failover in a clustered
environment
36Session Bean Example
- TitleManager 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
37Bean Types
Message Driven Beans
38Messaging Basics
First, a little background...
39JMS Messaging in Java --Point-to-Point
40JMS Messaging in Java --Pub-Sub
41Before 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!
42MDB Message Processing
- Asynchronous -- 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...
43Client Calls to MDBs
The CONTAINER IS the client!
44The MDB Lifecycle
45Example 1a -- Publishing to a Topic
Renting Out Titles -- RentalManagerBean
46Example 1b --An Example Subscriber
Renting Out Titles -- CustomerRentalWorkOrderMDB
47Example 1c --Run It!
onMessage -- CustomerRentalWorkOrderMDB
CustomerRentalNotificationMDB
48MDB Message Processing
- Implements MessageDrivenBean
- Implements 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
49Questions
- What 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? - Whats an alternative to a Stateful session bean
in a web application?
50Does your head hurt yet?
Bean Info You Shouldnt Leave Home Without
51JNDI Lookups of EJBs
- Java 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(ltJNDI Namegt)
- narrowing remote references (the joy of RMI)
52JNDI ENC Bean Resource Dependencies
53JNDI ENC
- JNDI Environment Naming Context
- each EJBs own private Idaho namespace
- relative to javacomp/env
- subcontexts recommended by spec (ejb, jdbc, jms,
mail, url) - Example of JNDI ENC refs in RentalManager
- local ejb refs (TitleManager, Payment Manager)
- DataSource for connections used in DAO class
- JMSConnectionFactory objects and Destinations
- Differences in lookups (JNDI vs. JNDI ENC, remote
vs. local)
54Transactions -- 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
55Transactions -- Title Rental Example
- ACID Properties
- Atomic unit of work
- Consistent state (DB, JMS Destinations)
- Isolation of work from other system activity
- Durable end result
56Transactions -- Title Rental Example
- Possible Failure points in workflow
- 2/3 titles reserved, third is unavailable
- 3 titles reserved, rental records created, but
charge to customers 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
57Transactions -- Declarative CMT
- NotSupported -- We dont do that here.
- Supports -- If you got one, cool, otherwise, who
cares! - Required -- I need one, and if you dont provide
one, Ill take care of business myself. - RequiresNew -- I dont care if you have one or
not. I want my own! - Mandatory -- I want one, and you better give it
to me! - Never -- I dont want one, and dont even think
of calling me with one!
58Example 2 --Run It!
rentOutTitles
59Exceptions -- 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)
60Exceptions -- 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)
61Exceptions -- RentalManager EJB
- Application Exception -- rollback only set,
exception rethrown - SQLException (System) converted to EJBException
and thrown
62EJB 2.1 / 3.0
Coming Soon to a Container Near You!
63Whats New in EJB 2.1
- Better 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
64Whats 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)
65Whats New in EJB 3.0
- http//www.jcp.org/en/jsr/detail?id220
- Its all about the beans, baby!
- Bye, bye interfaces
- Hello annotations
- Example
- _at_Session public class MyEJBThreeOBean
- public void youCanDoIt(boolean really) ...
-
66Whats New in EJB 3.0
- http//www.martinfowler.com/articles/injection.htm
l - Dependency Injection vs. JNDI lookups
- Example
- _at_Session public class MyEJBThreeOBean
- private DataSource myDS
- _at_Inject private void setMyDS(DataSource
theDS) - myDS theDS
-
- public void methodCallingMyDAO()
- ...
- Connection c myDS.getConnection()
- ...
-
67If theres time...
- Deployment Issues (JARs, EARs, etc.)
- JUnit w/ EJBs
- Debugging Example
- Profiling Example
68Questions?
69Thank You
- 3036
- Enterprise Java Beans
- Please fill out the speaker evaluation
- You can contact me further at pmcmichael_at_pillart
echnology.com