head first ejb.pdf

(22563 KB) Pobierz
Head First EJB™
425761645.038.png
Head First EJB
by Kathy Sierra and Bert Bates
Copyright © 2003 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles ( safari.oreilly.com ). For more information, contact our corporate/institutional sales
department: (800) 998-9938 or corporate@oreilly.com .
Editor: Mike Loukides
Cover Designer: Edie Freedman
Interior Decorators: Kathy Sierra and Bert Bates
Anthropomorphizer: Bert Bates
Bean Wrangler:
Kathy Sierra
Printing History:
October 2003: First Edition.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Java and all Java-based trademarks and logos
are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries.
O’Reilly Media, Inc. is independent of Sun Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks.
Where those designations appear in this book, and O’Reilly was aware of a trademark claim, the designations
have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
In other words, if you use anything in Head First EJB™ to, say, run a nuclear power plant or air trafic control
system, you’re on your own.
And although some people have been able to pass the exam simply by placing this book under their pillow each
night for three consecutive weeks, we generally don’t recommend it. Most people ind it helpful to actually read
the book or at least look at the pictures.
ISBN-10: 0-596-00571-7
ISBN-13: 978-0-596-00571-9
[M]
[8/07]
425761645.039.png 425761645.040.png 425761645.041.png 425761645.001.png 425761645.002.png 425761645.003.png 425761645.004.png 425761645.005.png 425761645.006.png 425761645.007.png 425761645.008.png 425761645.009.png
Table of Contents (summary)
Intro
xix
1 Welcome to EJB: an introduction
1
2 EJB Architecture: architectural overview
61
3 Exposing Yourself: the client view
111
4 Being a Session Bean: session bean lifecycle
173
5 Entities are Persistent: entity bean intro
259
6 Being an Entity Bean: bean/entity synchronization
295
7 When Beans Relate: entity bean relationships
373
8 Getting the Message: message-driven beans
437
9 The Atomic Age: EJB transactions
469
10 When Beans Go Bad: exceptions in EJB
525
11 Protect Your Secrets: security in EJB
569
12 The Joy of Deployment: a bean’s environment
599
A Appendix A: Final Mock Exam
637
Table of Contents (the real thing)
Your brain on EJB. Here you are trying to learn something, while here your brain
is doing you a favor by making sure the learning doesn’t stick . Your brain’s thinking, “Better
leave room for more important things, like which wild animals to avoid and whether naked
snowboarding is a bad idea.” So how do you trick your brain into thinking that your life
depends on knowing EJB?
Who is this book for?
xviii
We know what your brain is thinking
xix
Metacognition
xxi
Bend your brain into submission
xxiii
What you need for this book
xxiv
Passing the certiication exam
xxvi
Technical reviewers
xxviii
Acknowledgements
xxix
ix
i Intro
425761645.010.png 425761645.011.png 425761645.012.png 425761645.013.png
1 Welcome to EJB
Enterprise JavaBeans are easy. Well, at least when you compare EJB
to what you’d have to do to write your own scalable, transactional, secure, persistent,
concurrent enterprise component server. In this chapter, we’ll develop, deploy, and run an
EJB application, and then dive into the details. Before we’re done, we’ll look at the use,
benei ts, and characteristics of EJB, and we’ll look (briel y) at how EJB containers work.
Exam objectives
2
Server
What is EJB all about?
3
No more vendor lock-in!
5
How does it all work?
7
r
Behind the scenes...
8
EJB Container
DB
Beans come in three l avors
11
The Advice Guy bean
15
Five things you do to build a bean
16
EJB Roles and Responsibilities
26
Tutorial
28
Coffee Cram
59
2 EJB Architecture
EJB is about infrastructure. Your components are the building blocks. With
EJB, you can build big applications. The kind of applications that could run everything
from the Victoria’s Secret back-end to document-handling systems at CERN. But an
architecture with this much l exibility, power, and scalability isn’t simple. It all begins with a
distributed programming model...
Exam objectives
62
<<interface>>
EnterpriseBean
no methods
Making a Remote method call
64
<<interface>>
Remote
(J2SE API)
(J2EE API)
What about arguments and return values?
67
no methods
The client calls business methods through the Remote interface
79
(J2EE API)
<<interface>>
EJBObject
// several methods
(J2EE API)
<<interface>>
SessionBean
// several methods
EJB uses RMI
81
The Remote object is not the bean, it’s the bean’s bodyguard
82
YOU write this
interface
(the remote
component
interface)
<<interface>>
BookCart
addBook()
removeBook()
showBooksInCart()
doCheckout()
YOU write this
class
(the bean class)
BookCartBean
addBook()
removeBook()
showBooksInCart()
doCheckout()
// other methods
Architectural overview: Session beans
98
Architectural overview: Entity beans
99
Architectural overview: Creating a Stateful Session bean
100
Architectural overview: Creating a Stateless Session bean
101
Architectural overview: Message-driven beans
106
Organize your beans
108
x
425761645.014.png 425761645.015.png 425761645.016.png 425761645.017.png 425761645.018.png 425761645.019.png 425761645.020.png 425761645.021.png 425761645.022.png 425761645.023.png 425761645.024.png 425761645.025.png 425761645.026.png 425761645.027.png 425761645.028.png 425761645.029.png 425761645.030.png 425761645.031.png
3 Exposing Yourself
You can’t keep your bean private. Clients need to see what you’ve got.
(Except for message-driven beans, which don’t have a client view). The Advice Bean
exposes the getAdvice() method in its Component interface—the place where you declare
business methods. But that’s not all the client sees. Remember, the Advice interface
extends EJBObject, an interface with methods of its own . Methods the client can see.
Methods the client can call . And it works the same way with the Home interface.
Stateless
beans
Exam objectives
112
What the client really wants
113
What’s JNDI?
116
PortableRemoteObject.narrow() (exotic casting)
121
Writing the Remote home interface for a session bean
125
bean
bean
Thankfully, we’ve got handles (online shopping takes time)
139
bean
Which methods make sense for the local client interfaces?
148
Why so many remove methods?
151
For stateless session beans from
the same home, isIdentical() always
returns true, even for different beans.
Comparing Remote vs. Local interfaces
154
Arguments to Remote vs. local methods
163
Coffee Cram
168
4 Being a Session Bean
Session beans are created and removed. If you’re lucky, you’re a
state less bean. Because the life of a state ful bean is tied to the whims of a heartless
client. Stateful beans are created at the client’s insistence, and live and die only to serve
that one client. But ahhhh, the life of a stateless bean is fabulous! Pools, those little
umbrella drinks, and no boredom since you get to meet so many different clients.
Exam objectives
174
For me? This is
such a special moment!
Once in a lifetime...
Container callbacks, for the special moments in a bean’s life
181
Bean Creation
188
Bean things you can do within business methods
199
Passivation: a stateful bean’s chance at scalability
200
Bean Removal
208
Writing a Session Bean: your job as Bean Provider
230
SessionContext: you need it more than it needs you
240
Coffee Cram
244
xi
425761645.032.png 425761645.033.png 425761645.034.png 425761645.035.png 425761645.036.png 425761645.037.png
Zgłoś jeśli naruszono regulamin